|
VR-Engage
2.3
|
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: The component polls Ownship.stateProperties.set-gear in the player attribute store each tick and logs changes delivered to its DtString callback as the vehicle's gear state is reflected.
Prerequisites:
std::function semanticsinitialize(), postInitialize(), tick(), shutdown())Related Examples:
This example demonstrates:
DtAttributeCallbackManager owns the callback connectioninitialize() 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:
Call the base initialize() before accessing the player attribute store. The lookup creates a node if needed; retaining its handle does not establish a value. The bundled Driver role inherits driver.lua, whose dashboard reads this same path. Published DtRwString state properties are reflected as DtString, so both polling and the typed callback must use DtString, not std::string.
The component shows critical timing requirements for callback registration:
This example connects in postInitialize() after the other components have run initialize(), when this consumer is ready for callbacks. The lookup in initialize() already creates the node; postInitialize() does not guarantee that a producer has assigned a value. Connecting in initialize() is also possible when the consumer is ready, but the callback here observes subsequent changes only. Check isSet() before reading if an unset value differs from a valid empty string. The callback manager tracks the connection for cleanup.
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 when attribute values change. The new value is provided as a parameter, eliminating the need for another attribute query. The callback signature must match the stored type: DtString for Ownship.stateProperties.set-gear. The attribute system does not convert between DtString and std::string when dispatching typed callbacks.
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 setOwnship.stateProperties.set-gear produce "Gear has changed!" messagesVerification:
*.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:
driverInfo.luaOnly polling messages, no callback messages:
Ownship.stateProperties.set-gear with DtString, and confirm that gear changes publish new valuesCrash 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 repositoryDtAttribute::get<T>() through DtAttributeHandle::operator->() - Read an attribute valueDtAttributeCallbackManager::connect() - Register attribute change callbacksDtAttributeCallbackManager::disconnectAll() - Bulk callback cleanupDtPlayerStationApp::componentFactory().addCreator<T>() - Component factory registration| Attribute Name | Type | Description | Updated By |
|---|---|---|---|
Ownship.stateProperties.set-gear | DtString | Reflected gear value monitored by the component | Reflected entity connector, from the published vehicle state property |
examplePlayerStateAttribute.dll (Windows)plugins64/vrEngage/release/playerStateAttribute.sms simulation model setRelated Documentation: