VR-Engage  2.2
Loading...
Searching...
No Matches
DtPlayerStateAttributeSampleComponent Class Reference

Detailed Description

This component shows how to use the VR-Engage player attribute store callback system to monitor state changes using event-driven patterns. It specifically monitors the "set-gear" attribute and logs changes when the player's gear selection changes, demonstrating both polling (in tick()) and event-driven (via callbacks) approaches.

Key VR-Engage patterns demonstrated:

  • Subscribing to attribute changes via attribute callbacks
  • Accessing the player attribute store for centralized state management
  • Using DtAttributeConnectionList for automatic callback lifetime management
  • Component lifecycle (initialize, postInitialize, tick, shutdown)
  • Proper callback cleanup during shutdown to prevent dangling pointers
  • Comparing polling (tick) vs. event-driven (callback) approaches

This class inherits from DtPlayerComponent and is registered with the player station's component factory for role-based instantiation.

#include <sampleComponent.h>

Inheritance diagram for DtPlayerStateAttributeSampleComponent:
[legend]

Public Member Functions

 DtPlayerStateAttributeSampleComponent ()
 
virtual ~DtPlayerStateAttributeSampleComponent () override=default
 
virtual bool initialize (makVre::DtPlayerStation *player, makVre::DtInitTable &config) override
 
virtual bool postInitialize () override
 
virtual void tick (double dt) override
 
virtual void shutdown () override
 
virtual const char * type () const override
 
- Public Member Functions inherited from makVre::DtPlayerComponent
 DtPlayerComponent ()
 
virtual ~DtPlayerComponent ()
 
virtual void setName (const std::string &name)
 
virtual const std::string & name ()
 
virtual DtPlayerStationplayer ()
 
virtual DtAttributeHandleplayerAttributeStore ()
 
virtual const DtAttributeHandleplayerAttributeStore () const
 

Protected Member Functions

virtual void onGearChanged (const std::string &newGear)
 
- Protected Member Functions inherited from makVre::DtPlayerComponent
virtual void initializeStateAttributes ()
 

Protected Attributes

makVre::DtAttributeHandle myGearAttributeHandle
 
- Protected Attributes inherited from makVre::DtPlayerComponent
std::string myName
 
DtInitTable myConfig
 
DtPlayerStationmyPlayer
 
makVrv::DtDe * myDe
 
DtEntityResolvermyResolver
 
DtAttributeCallbackManager myAttributeCallbacks
 

Constructor & Destructor Documentation

◆ DtPlayerStateAttributeSampleComponent()

DtPlayerStateAttributeSampleComponent::DtPlayerStateAttributeSampleComponent ( )

Default constructor.

Initializes the player state attribute sample component. The actual setup is performed in initialize() following the VR-Engage component lifecycle pattern.

◆ ~DtPlayerStateAttributeSampleComponent()

virtual DtPlayerStateAttributeSampleComponent::~DtPlayerStateAttributeSampleComponent ( )
overridevirtualdefault

Virtual destructor.

Cleanup is performed in shutdown() rather than the destructor to ensure proper framework ordering during component destruction.

Member Function Documentation

◆ initialize()

virtual bool DtPlayerStateAttributeSampleComponent::initialize ( makVre::DtPlayerStation * player,
makVre::DtInitTable & config )
overridevirtual

Initializes the component with player station and configuration.

This method demonstrates the VR-Engage pattern for component initialization:

  1. Call base class initialize() first to set up inherited members
  2. Access player attribute store to retrieve attribute handles

The "set-gear" attribute is accessed from the player attribute store, which is a centralized repository for player-specific state. The attribute handle is stored for later use in postInitialize() when callbacks are registered.

PATTERN: Attribute handles should be retrieved in initialize(), but callbacks should be registered in postInitialize() to ensure all components and attributes are fully initialized.

Parameters
playerThe player station instance this component belongs to
configConfiguration table containing role parameters (not used in this example)
Returns
true if initialization succeeds, false otherwise

Reimplemented from makVre::DtPlayerComponent.

References makVre::DtPlayerComponent::player().

◆ postInitialize()

virtual bool DtPlayerStateAttributeSampleComponent::postInitialize ( )
overridevirtual

Performs post-initialization after all components are initialized.

This method demonstrates the VR-Engage pattern for registering attribute callbacks:

  1. Call base class postInitialize() first
  2. Register callbacks using the connection manager

PATTERN: Register attribute callbacks in postInitialize() rather than initialize() to ensure all components are fully initialized and all attributes exist. This prevents race conditions where callbacks might be invoked before the component is fully ready.

The myAttributeCallbacks connection manager automatically tracks all registered callbacks and provides disconnectAll() for cleanup during shutdown.

Returns
true if post-initialization succeeded, false otherwise

Reimplemented from makVre::DtPlayerComponent.

◆ tick()

virtual void DtPlayerStateAttributeSampleComponent::tick ( double dt)
overridevirtual

Per-frame update callback.

Called every frame by the player station framework. This example demonstrates the polling approach to attribute access by retrieving and logging the current gear value every frame using get<T>().

PATTERN COMPARISON:

  • Polling (this method): Retrieves attribute value every frame, even if unchanged
  • Event-driven (onGearChanged): Callback invoked only when value actually changes

For attributes that change infrequently, callbacks are more efficient. For attributes that need to be sampled every frame (like analog inputs), polling in tick() is appropriate.

Parameters
dtDelta time in seconds since last update

Implements makVre::DtPlayerComponent.

◆ shutdown()

virtual void DtPlayerStateAttributeSampleComponent::shutdown ( )
overridevirtual

Shutdown callback for cleanup.

Performs cleanup before component destruction. This method demonstrates the VR-Engage pattern for proper callback cleanup:

  1. Call base class shutdown() first
  2. Disconnect all attribute callbacks to prevent dangling pointers

The myAttributeCallbacks.disconnectAll() call ensures all registered callbacks are properly removed from the attribute store before the component is destroyed.

Reimplemented from makVre::DtPlayerComponent.

◆ type()

virtual const char * DtPlayerStateAttributeSampleComponent::type ( ) const
overridevirtual

Returns the component type identifier.

This string is used by the component factory for instantiation and must match the registration in initPlayerStationModule().

Returns
The component type string "DtPlayerStateAttributeSampleComponent"

Implements makVre::DtPlayerComponent.

◆ onGearChanged()

virtual void DtPlayerStateAttributeSampleComponent::onGearChanged ( const std::string & newGear)
protectedvirtual

Callback invoked when the "set-gear" attribute changes.

This method demonstrates the event-driven approach to attribute monitoring. It is automatically called by the attribute store whenever the gear attribute changes, receiving the new value as a parameter.

PATTERN: Attribute callbacks receive the new value as a parameter with a type matching the attribute's type. The signature must match: void methodName(const AttributeType& newValue)

This is more efficient than polling in tick() for infrequent changes, as it only executes when the value actually changes.

Parameters
newGearThe new gear value (e.g., "Neutral", "1st", "2nd", "Reverse")

Member Data Documentation

◆ myGearAttributeHandle

makVre::DtAttributeHandle DtPlayerStateAttributeSampleComponent::myGearAttributeHandle
protected

Handle to the "set-gear" player attribute.

This handle provides typed access to the gear attribute value via get<T>() and is used to register callbacks via the connection manager. Attribute handles are lightweight value types that can be safely stored as member variables.

PATTERN: Store attribute handles as members for repeated access rather than looking up attributes by name each time they're needed.


The documentation for this class was generated from the following file: