|
VR-Engage
2.2
|
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:
This class inherits from DtPlayerComponent and is registered with the player station's component factory for role-based instantiation.
#include <sampleComponent.h>
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 DtPlayerStation & | player () |
| virtual DtAttributeHandle & | playerAttributeStore () |
| virtual const DtAttributeHandle & | playerAttributeStore () 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 |
| DtPlayerStation * | myPlayer |
| makVrv::DtDe * | myDe |
| DtEntityResolver * | myResolver |
| DtAttributeCallbackManager | myAttributeCallbacks |
| 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.
|
overridevirtualdefault |
Virtual destructor.
Cleanup is performed in shutdown() rather than the destructor to ensure proper framework ordering during component destruction.
|
overridevirtual |
Initializes the component with player station and configuration.
This method demonstrates the VR-Engage pattern for component initialization:
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.
| player | The player station instance this component belongs to |
| config | Configuration table containing role parameters (not used in this example) |
Reimplemented from makVre::DtPlayerComponent.
References makVre::DtPlayerComponent::player().
|
overridevirtual |
Performs post-initialization after all components are initialized.
This method demonstrates the VR-Engage pattern for registering attribute callbacks:
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.
Reimplemented from makVre::DtPlayerComponent.
|
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:
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.
| dt | Delta time in seconds since last update |
Implements makVre::DtPlayerComponent.
|
overridevirtual |
Shutdown callback for cleanup.
Performs cleanup before component destruction. This method demonstrates the VR-Engage pattern for proper callback cleanup:
The myAttributeCallbacks.disconnectAll() call ensures all registered callbacks are properly removed from the attribute store before the component is destroyed.
Reimplemented from makVre::DtPlayerComponent.
|
overridevirtual |
Returns the component type identifier.
This string is used by the component factory for instantiation and must match the registration in initPlayerStationModule().
Implements makVre::DtPlayerComponent.
|
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.
| newGear | The new gear value (e.g., "Neutral", "1st", "2nd", "Reverse") |
|
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.