|
VR-Engage
2.2
|
Purpose: This example demonstrates how to monitor and respond to changes in the VR-Engage player attribute store using the attribute callback system. It illustrates event-driven component design where components react to state changes rather than polling for updates.
Observable Behavior: When running this example, you will see log messages in the VR-Engage console showing gear changes as you operate vehicle controls. The example monitors the "set-gear" attribute and logs both continuous polling updates (every frame) and event-driven change notifications (only when gear actually changes). This demonstrates the difference between polling and callback-based attribute monitoring approaches.
Prerequisites:
std::function semanticsinitialize(), postInitialize(), tick(), shutdown())Related Examples:
This example demonstrates:
DtAttributeConnectionList) provides automatic callback lifetime managementinitialize() and postInitialize() for callback registrationshutdown() to prevent dangling pointer issuestick() polling and attribute change callbacksThe plugin registers the component with VR-Engage's factory system:
The string DtPlayerStateAttributeSampleComponentType must match the component's type() method return value. This registration makes the component available for instantiation when specified in role configurations. The extern "C" linkage prevents C++ name mangling, which is required for dynamic loading of plugin entry points.
The component demonstrates proper attribute store access during initialization:
The base class initialize() must be called first to set up inherited framework members before accessing the player attribute store. Attribute handles should be retrieved here when the player station is available. The "set-gear" attribute is a standard vehicle attribute managed by VR-Engage's control systems, and handles are lightweight value types that provide type-safe access to attribute values.
The component shows critical timing requirements for callback registration:
Callbacks must be registered in postInitialize() rather than initialize() because by postInitialize(), all components are fully initialized and attributes are guaranteed to exist. The connection manager automatically tracks callback lifetimes for proper cleanup. Registering callbacks too early in initialize() could cause race conditions or reference missing attributes that other components have not yet created.
The tick method demonstrates continuous attribute monitoring:
Polling retrieves attribute values every frame regardless of whether they have changed. The get<T>() template method provides type-safe access to attribute values. This approach is suitable for continuous monitoring but can be inefficient for attributes that change infrequently, incurring approximately 60 attribute accesses per second at typical frame rates.
The callback method demonstrates efficient change notification:
Callbacks execute only when attribute values actually change, which is more efficient than polling. The new value is provided as a parameter, eliminating the need for additional attribute queries. Callback signatures must match the attribute's type (std::string for "set-gear"), and this pattern enables immediate responses to state changes without polling overhead.
The shutdown method demonstrates proper callback lifecycle management:
All attribute callbacks must be disconnected before component destruction because the attribute store may outlive individual components. Without explicit cleanup, the store could attempt to invoke callbacks on destroyed objects, causing crashes. The connection manager's disconnectAll() method provides convenient bulk cleanup for all registered callbacks.
Build the example (see Environment Setup & Build Guide):
Install the plugin to the VR-Engage installation:
This copies the plugin to the appropriate location:
<VR-Engage-Install-Dir>\plugins64\vrEngage\release\examplePlayerStateAttribute.dllVerify installation:
The simulation model set is located at <VR-Engage-Install-Dir>\data\simulationModelSets\examples\playerStateAttribute.sms and should be available in the standard VR-Engage installation.
To run VR-Engage with the playerStateAttribute example simulation model set as the default, use the --setting command-line option to override playerStationApp.defaultSimulationModelSets:
This is equivalent to the Visual Studio debugger configuration for the playerStateAttribute example and ensures that the correct simulation model set and roles are loaded when testing this example.
Option 1: Use Visual Studio debug configuration (easiest):
playerStateAttribute project in Visual Studio → Debug → Start New InstanceplayerStateAttribute.sms simulation model set with pre-configured entitiesOption 2: Manual scenario setup:
playerStateAttribute.sms simulation model settick()Verification:
*.log file in the MAK log directory, typically C:/MAK/logs) for component registration: Plugin not loading:
<VR-Engage-Install-Dir>\plugins64\vrEngage\release\*.log file in the MAK log directory) for DLL load errors or missing symbolsNo attribute messages appearing:
Only polling messages, no callback messages:
Crash on component destruction:
shutdown()myAttributeCallbacks.disconnectAll() is called in shutdown method| Class | Base Class | Purpose | Header |
|---|---|---|---|
DtPlayerStateAttributeSampleComponent | DtPlayerComponent | Attribute monitoring component | sampleComponent.h |
DtPlayerStation::playerAttributeStore() - Access centralized attribute repositoryDtAttributeHandle::get<T>() - Type-safe attribute value retrievalDtAttributeConnectionList::connect() - Register attribute change callbacksDtAttributeConnectionList::disconnectAll() - Bulk callback cleanupDtPlayerStationApp::componentFactory().addCreator<T>() - Component factory registration| Attribute Name | Type | Description | Updated By |
|---|---|---|---|
"set-gear" | std::string | Current gear selection (e.g., "Neutral", "1st", "2nd", "Reverse") | Vehicle control components |
examplePlayerStateAttribute.dll (Windows)plugins64/vrEngage/release/playerStateAttribute.sms simulation model setRelated Documentation: