VR-Engage  2.3
Loading...
Searching...
No Matches
playerComponent.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file playerComponent.h
7//! \brief Defines the DtPlayerComponent base class for VR-Engage role components
8//!
9//! This file contains the base class that all role components in VR-Engage must
10//! inherit from. Components provide specific functionality for roles, such as
11//! movement, weapons, sensors, and user interface elements. The component system
12//! allows roles to be composed from reusable building blocks.
13
14#pragma once
15
17
18#include "vreUtil/attribute.h"
19
20#include "vreUtil/initializer.h"
21#include <vrvUtil/DtProfiler.h>
22
24
25#include <string>
26
27namespace makVrv
28{
29class DtDe;
30}
31
32namespace makVre
33{
34//! \brief Forward declaration of the initialization table class
35class DtInitTable;
36
37//! \brief Forward declaration of the player station class
38class DtPlayerStation;
39
40//! \brief Forward declaration of the entity resolver class
42
44{
45//! \defgroup RoleConfigParams Role Configuration Parameters
46//! \brief Parameters used to configure player station role components.
47//!
48//! Role configuration parameters control the behavior of components within a player role.
49//! Each component type defines its own set of parameters that can be specified in role
50//! configuration files to customize component behavior.
51//! @hidegroupgraph
52
53//! \ingroup RoleConfigParams
54//! \defgroup PlayerComponentRoleParams Player Component Role Parameters
55//! \brief Base configuration options inherited by all player components.
56//!
57//! These parameters are available to all component types that inherit from DtPlayerComponent.
58//! They provide common configuration options for component identification and initialization.
59//! Component-specific parameters are documented in each component's own parameter group.
60//! @{
61
62//! \vreRoleParam{initAttributes,string,"",playerComponent,Table of initial attribute values to set during component initialization.}
63constexpr const char* initAttributes = "initAttributes";
64
65//! \vreRoleParam{name,string,"",playerComponent,Name to assign to this component instance for identification purposes.}
66constexpr const char* name = "name";
67
68//! @}
69} // namespace PlayerComponentConfig
70
71//! \brief Status returned by DtPlayerComponent::finalizeInitTick() to indicate deferred
72//! initialization progress.
73//!
74//! Components that require asynchronous initialization (e.g., waiting for VR-Vantage
75//! agents or terrain realization) override finalizeInitTick() and return one of these values
76//! each frame until initialization completes or fails.
77//!
78//! \see DtPlayerComponent::finalizeInitTick
80{
81 Ready, //!< Component initialization is complete
82 Pending, //!< Component still needs work — finalizeInitTick() will be called again
83 Failed //!< Component initialization failed unrecoverably
84};
85
86//! \brief Base class for all role components in VR-Engage
87//!
88//! DtPlayerComponent serves as the foundation for all components that can be
89//! added to player roles. Components encapsulate specific functionality, such as
90//! movement control, weapons systems, sensor management, and user interface elements.
91//!
92//! Components are instantiated by the player station during initialization based on
93//! the role definition. They have access to the player attribute store for sharing
94//! data with other components, and they receive tick calls for regular updates.
95//!
96//! Components that require deferred initialization should set myComponentReady to
97//! false in their constructor and override finalizeInitTick(). The LOADING_PLAYER_STATE will
98//! call finalizeInitTick() each frame until the component reports Ready or Failed.
99//! Components that complete all initialization in initialize()/postInitialize()
100//! leave myComponentReady at its default value of true and do not override finalizeInitTick().
102{
103public:
104 //! \brief Constructor
105 //!
106 //! Creates a component
107 //! Called from DtPlayerStation::initializeComponents during player station initialization.
108 //! Each component has a unique type that identifies its functionality.
110
111 //! \brief Virtual destructor
112 //!
113 //! Ensures proper cleanup of derived component classes.
115
116 //! \brief Initializes the component
117 //! \param player Pointer to the player station that owns this component
118 //! \param config Configuration table with component settings
119 //! \return True if initialization was successful, false otherwise
120 //!
121 //! Called when the component is first created during player station initialization.
122 //! This method performs initial setup and calls initializeStateAttributes().
123 //! Components should override this method to perform type-specific initialization,
124 //! but must call the base class implementation first.
126
127 //! \brief Performs post-initialization setup
128 //! \return True if post-initialization was successful, false otherwise
129 //!
130 //! Called after all components have been created and initialized. This is when
131 //! required but not owned state attributes can be looked up in the state, or
132 //! dependent components can be located. Components can use this phase to establish
133 //! connections with other components in the player station.
134 virtual bool postInitialize();
135
136 //! \brief Updates the component
137 //! \param dt Time in seconds since the last tick
138 //!
139 //! Called once per frame to update the component's state.
140 //! All concrete component classes must implement this method.
141 virtual void tick(double dt) = 0;
142
143 //! \brief Shuts down the component
144 //!
145 //! Called when the player station is shutting down. Components should
146 //! override this method to release resources and perform cleanup operations.
147 virtual void shutdown();
148
149 //! \brief Returns whether this component has completed deferred initialization.
150 //!
151 //! Components that need asynchronous initialization (e.g., waiting for VR-Vantage
152 //! agents or terrain realization) set myComponentReady to false in their constructor.
153 //! The LOADING_PLAYER_STATE polls this to determine when all components are ready.
154 //!
155 //! \return True if the component is fully initialized, false if finalizeInitTick() still
156 //! needs to be called.
157 bool componentReady() const { return myComponentReady; }
158
159 //! \brief Sets the component readiness state.
160 //! \param ready True if the component is fully initialized.
161 //!
162 //! Called by DtPlayerStation::tickUnreadyComponents() when finalizeInitTick()
163 //! returns Ready. Components should not need to call this directly.
164 void setComponentReady(bool ready) { myComponentReady = ready; }
165
166 //! \brief Performs one frame of deferred initialization work.
167 //! \param dt Time in seconds since the last tick
168 //! \return Ready when initialization is complete, Pending if more frames are needed,
169 //! or Failed if initialization cannot succeed.
170 //!
171 //! Called by LOADING_PLAYER_STATE every tick for components where componentReady()
172 //! returns false. When this method returns Ready, tickUnreadyComponents() will
173 //! set myComponentReady to true automatically. The state will not transition to
174 //! ENGAGED_STATE until all components report ready.
175 //!
176 //! Override this in components that set myComponentReady = false in their
177 //! constructor. The default implementation returns Ready.
178 //!
179 //! \see componentReady
180 //! \see setComponentReady
181 //! \see DtInitTickStatus
183
184 //! \brief Gets the component type identifier
185 //! \return Reference to the component type string
186 //!
187 //! Returns the unique type identifier for this component class. The type string
188 //! is used by the component system to identify and instantiate components based
189 //! on role configuration. Each component class should return a consistent,
190 //! descriptive type name (e.g., "DtDriverControlLogic", "DtObserverUpdater").
191 //!
192 //! \note This is a pure virtual method that must be implemented by all derived classes.
193 virtual const char* type() const = 0;
194
195 //! \brief Sets the component name
196 //! \param name The name to assign to this component
197 //!
198 //! Sets a name for this component instance, which can be used to distinguish
199 //! between multiple components of the same type in a player station.
200 virtual void setName(const std::string& name);
201
202 //! \brief Gets the component name
203 //! \return Reference to the component name string
204 //!
205 //! Returns the name of this component instance, which may be empty if
206 //! no name was explicitly assigned.
207 virtual const std::string& name();
208
209 //! \brief Gets the player station
210 //! \return Reference to the player station that owns this component
211 //!
212 //! Provides access to the player station that this component belongs to.
214
215 //! \brief Gets the player attribute store
216 //! \return Reference to the attribute store
217 //!
218 //! Provides access to the attribute store of the player station, which
219 //! contains shared data that can be accessed by all components.
221
222 //! \brief Gets the player attribute store (const version)
223 //! \return Const reference to the attribute store
225
226 //! \brief Gets the ownship state attribute handle
227 //!
228 //! Convenience wrapper for player().ownshipState()
229 virtual const DtAttributeHandle& ownshipState() const;
230
231 //! \brief Sets scripted extended data on an entity.
232 //! \param key Extended-data key.
233 //! \param value Extended-data value.
234 //! \param entity Target entity id; defaults to ownship when null.
235 virtual void setExtendedData(
236 const std::string& key, const std::string& value, DtEntityIdentifier entity = DtEntityIdentifier::nullId());
237
238protected:
239 //! \brief Initializes component state attributes
240 //!
241 //! Creates and initializes any player state attributes required by this component.
242 //! Derived component classes should override this method if they need to add
243 //! custom state attributes to the player attribute store.
245
246 //! \brief Whether this component has completed deferred initialization.
247 //!
248 //! Defaults to true. Components that require asynchronous initialization
249 //! (e.g., waiting for VR-Vantage agents or terrain realization) should set
250 //! this to false in their constructor and override finalizeInitTick().
251 //!
252 //! \see finalizeInitTick
253 //! \see componentReady
254 bool myComponentReady = true;
255
256 //! \brief Component instance name
257 std::string myName;
258
259 //! \brief Component configuration table
261
262 //! \brief Pointer to the owning player station
264
265 //! \brief Pointer to the display engine (common to all components)
266 makVrv::DtDe* myDe;
267
268 //! \brief Pointer to the entity resolver (common to all components)
270
271 //! \brief Member-scope instance of Attribute callback manager
272 //!
273 //! Used to manage Attribute change callback connections and ensure that all
274 //! callbacks are unregistered when this class instance is destroyed.
276
277 //! \brief Member-scope instance of message handler group
278 //!
279 //! Used to manage message handler connections for this component. Handlers connected
280 //! through this group will be automatically disconnected when this component is destroyed.
282};
283
284} // namespace makVre
Provides attribute handling system for hierarchical data storage and manipulation.
Convenience manager that owns and organises a collection of attribute callbacks.
Definition attributeCallback.h:223
Handle class for safe access to attributes.
Definition attributeHandle.h:30
Entity resolution and mapping system.
Definition entityResolver.h:52
Table-based access to Lua state for configuration data.
Definition initializer.h:38
DtInitTable myConfig
Component configuration table.
Definition playerComponent.h:260
virtual void setName(const std::string &name)
Sets the component name.
virtual const char * type() const =0
Gets the component type identifier.
virtual DtAttributeHandle & playerAttributeStore()
Gets the player attribute store.
virtual const DtAttributeHandle & playerAttributeStore() const
Gets the player attribute store (const version)
virtual DtInitTickStatus finalizeInitTick(double dt)
Performs one frame of deferred initialization work.
virtual bool postInitialize()
Performs post-initialization setup.
virtual bool initialize(DtPlayerStation *player, DtInitTable &config)
Initializes the component.
virtual void shutdown()
Shuts down the component.
bool componentReady() const
Returns whether this component has completed deferred initialization.
Definition playerComponent.h:157
virtual DtPlayerStation & player()
Gets the player station.
std::string myName
Component instance name.
Definition playerComponent.h:257
virtual ~DtPlayerComponent()
Virtual destructor.
virtual void tick(double dt)=0
Updates the component.
virtual void initializeStateAttributes()
Initializes component state attributes.
DtPlayerStation * myPlayer
Pointer to the owning player station.
Definition playerComponent.h:263
DtEntityResolver * myResolver
Pointer to the entity resolver (common to all components)
Definition playerComponent.h:269
virtual void setExtendedData(const std::string &key, const std::string &value, DtEntityIdentifier entity=DtEntityIdentifier::nullId())
Sets scripted extended data on an entity.
DtAttributeCallbackManager myAttributeCallbacks
Member-scope instance of Attribute callback manager.
Definition playerComponent.h:275
bool myComponentReady
Whether this component has completed deferred initialization.
Definition playerComponent.h:254
makVrv::DtDe * myDe
Pointer to the display engine (common to all components)
Definition playerComponent.h:266
virtual const std::string & name()
Gets the component name.
DtPlayerComponent()
Constructor.
void setComponentReady(bool ready)
Sets the component readiness state.
Definition playerComponent.h:164
virtual const DtAttributeHandle & ownshipState() const
Gets the ownship state attribute handle.
DtVreMessageHandlerGroup myMessageHandlers
Member-scope instance of message handler group.
Definition playerComponent.h:281
Class for managing the user interface for engaged roles.
Definition playerStation.h:51
Manager class for connecting message handlers to message types.
Definition vreMessageManager.h:292
constexpr const char * initAttributes
Definition playerComponent.h:63
constexpr const char * name
Definition playerComponent.h:66
Defines export macros for the VR-Engage Player Station library.
#define PLAYERSTATION_DLL
Definition export.h:24
Provides configuration initialization for VREngage components.
Definition playerComponent.h:44
!
Definition glsVreMessageUtil.h:50
DtInitTickStatus
Status returned by DtPlayerComponent::finalizeInitTick() to indicate deferred initialization progress...
Definition playerComponent.h:80
@ Pending
Component still needs work — finalizeInitTick() will be called again.
Definition playerComponent.h:82
@ Failed
Component initialization failed unrecoverably.
Definition playerComponent.h:83
@ Ready
Component initialization is complete.
Definition playerComponent.h:81
Definition appLauncherComponent.h:28
Defines the central message manager for the VREngage messaging system.