GameObject Class
Container for components that represents an entity in the game world. More...
Declaration
Public Constructors Index
| GameObject () | |
|
Constructs a new GameObject with a unique Guid. More... | |
| GameObject (bool isActive) | |
|
Constructs a new GameObject with a specified active state. More... | |
Public Destructor Index
| ~GameObject ()=default | |
|
Virtual destructor for proper polymorphic cleanup. More... | |
Public Member Functions Index
template <typename T, typename... Args> | |
| T & | add (Args &&... args) |
|
Adds a new component of type T to the GameObject. More... | |
| helios::engine::ecs::Component * | getOrAdd (std::unique_ptr< helios::engine::ecs::Component > component) |
|
Adds a pre-constructed component if no component of the same type exists. More... | |
template <typename T, typename... Args> | |
| auto | getOrAdd (Args &&... args) -> std::remove_cvref_t< T > & |
|
Retrieves an existing component of type T, or creates and adds it if not present. More... | |
template <typename T> | |
| auto | get () const -> std::remove_cvref_t< T > * |
|
Retrieves a component of type T. More... | |
| bool | has (const std::type_index idx) const noexcept |
|
Checks if a component with the given type index exists. More... | |
template <typename T> | |
| bool | has () const |
|
Checks if the GameObject has a component of type T. More... | |
template <typename T> | |
| bool | hasEnabledComponent () const |
|
Checks if the GameObject has a component of type T and returns true if this component is enabled. More... | |
template <typename T> | |
| bool | hasDisabledComponent () const |
|
Checks if the GameObject has a component of type T and returns true if this component is disabled. More... | |
| void | update (helios::engine::runtime::world::UpdateContext &updateContext) |
|
Updates all updatable components attached to this GameObject. More... | |
| const helios::util::Guid & | guid () const noexcept |
|
Returns the unique identifier for this GameObject. More... | |
| bool | isActive () const noexcept |
|
Checks if this GameObject is currently active. More... | |
| std::vector< std::unique_ptr< helios::engine::ecs::Component > > & | components () noexcept |
|
Returns a reference to all attached components. More... | |
| void | setActive (bool active) noexcept |
|
Sets the active state of this GameObject. More... | |
| void | onAcquire () |
|
Called when this GameObject is acquired from a pool. More... | |
| void | onRelease () |
|
Called when this GameObject is released back to a pool. More... | |
| void | finalizeAttach (helios::engine::ecs::Component *component=nullptr) |
|
Finalizes attachment of components by calling lifecycle callbacks. More... | |
Protected Member Attributes Index
| helios::util::Guid | guid_ |
|
Unique identifier for this GameObject. More... | |
| std::vector< std::unique_ptr< Component > > | components_ |
|
List of components attached to this GameObject. More... | |
| std::unordered_map< std::type_index, helios::engine::ecs::Component * > | componentIndex_ |
|
Type-indexed map for O(1) amortized component lookups. More... | |
| std::vector< Updatable * > | updatables_ |
|
Cached list of components that implement the Updatable interface. More... | |
| bool | isActive_ = true |
|
Active state flag for this GameObject. More... | |
Description
Container for components that represents an entity in the game world.
A GameObject is the fundamental entity type in helios. It serves as a container for Component instances that define its behavior and data. Each GameObject has a unique Guid for identification and lookup within the GameWorld.
## Component Management
Components are added via `add<T>()` and retrieved via `get<T>()`. The GameObject uses a type-indexed map (`std::unordered_map<std::type_index, Component*>`) for **O(1) amortized** component lookup by type.
```cpp auto entity = std::make_unique<GameObject>(); entity->add<SceneNodeComponent>(sceneNode); entity->add<Move2DComponent>();
auto* move = entity->get<Move2DComponent>(); // O(1) lookup ```
## Active State
Each GameObject has an `isActive()` flag that controls participation in the game loop:
| State | Behavior | |-------|----------| | `isActive() == true` | Processed by systems, rendered, collides | | `isActive() == false` | Skipped by systems, exists but dormant |
When the active state changes, `onActivate()` or `onDeactivate()` is called on all attached components.
## Lifecycle Callbacks
Components receive lifecycle notifications:
- `onAttach()` — Called when a component is added to the GameObject
- `onActivate()` / `onDeactivate()` — Called when active state changes
- `onAcquire()` / `onRelease()` — Called when acquired from/released to a pool
## Ownership
GameObjects are typically owned by a GameWorld via `std::unique_ptr`. The GameWorld provides lookup by Guid with amortized O(1) complexity.
- See Also
- See Also
GameWorld
- See Also
GameObjectFilter
Definition at line 73 of file GameObject.ixx.
Public Constructors
GameObject()
| inline |
Constructs a new GameObject with a unique Guid.
The Guid is automatically generated and remains constant for the lifetime of this object.
Definition at line 125 of file GameObject.ixx.
Reference guid_.
GameObject()
| inline explicit |
Constructs a new GameObject with a specified active state.
- Parameters
-
isActive Initial active state. If false, the object starts inactive.
Definition at line 132 of file GameObject.ixx.
Public Destructor
~GameObject()
| virtual default |
Virtual destructor for proper polymorphic cleanup.
Definition at line 137 of file GameObject.ixx.
Public Member Functions
add()
| inline |
Adds a new component of type T to the GameObject.
The component is created, attached to this GameObject (onAttach is called), and if it implements Updatable, it is added to the update list. Normalizes T and uses the type-indexed componentIndex_ for O(1) lookup complexity.
- Template Parameters
-
T The type of component to add. Must derive from Component.
Args Argument types for the component's constructor.
- Parameters
-
args Arguments forwarded to the component's constructor.
- Returns
Reference to the newly created component.
Definition at line 153 of file GameObject.ixx.
References componentIndex_, components_, finalizeAttach, has and updatables_.
Referenced by getOrAdd.
components()
| inline nodiscard noexcept |
Returns a reference to all attached components.
Provides direct access to the component container. Useful for iteration over all components (e.g., during cloning).
- Returns
Reference to the vector of component unique_ptrs.
Modifying the vector directly bypasses lifecycle callbacks. Use add() and remove() methods for proper component management.
Definition at line 374 of file GameObject.ixx.
Reference components_.
Referenced by helios::engine::runtime::world::GameWorld::clone.
finalizeAttach()
| inline |
Finalizes attachment of components by calling lifecycle callbacks.
If a specific component is passed, only that component is finalized. Otherwise, all unattached components are finalized. Calls `onAttach()` followed by `onActivate()` or `onDeactivate()` based on the GameObject's active state.
- Parameters
-
component Optional pointer to a specific component to finalize. If nullptr, all components are processed.
Definition at line 447 of file GameObject.ixx.
References componentIndex_ and isActive_.
get()
| inline |
Retrieves a component of type T.
Normalizes T and uses the type-indexed componentIndex_ for O(1) lookup complexity.
- Template Parameters
-
T The type of component to retrieve.
- Returns
Pointer to the component if found, nullptr otherwise.
Definition at line 250 of file GameObject.ixx.
Reference componentIndex_.
Referenced by getOrAdd, hasDisabledComponent and hasEnabledComponent.
getOrAdd()
| inline |
Adds a pre-constructed component if no component of the same type exists.
If a component of the same runtime type already exists, returns the existing component and does not take ownership of the passed pointer. Otherwise, takes ownership and attaches the component.
Uses the type-indexed componentIndex_ for O(1) amortized lookup complexity.
- Parameters
-
component Unique pointer to the component to add. Ownership is transferred only if no component of the same type exists.
- Returns
Pointer to the existing or newly added component, or nullptr if the input was nullptr.
Definition at line 190 of file GameObject.ixx.
References componentIndex_, components_, finalizeAttach and has.
getOrAdd()
| inline |
Retrieves an existing component of type T, or creates and adds it if not present.
Normalizes T and uses the type-indexed componentIndex_ for O(1) lookup complexity. This is a convenience method that combines get() and add(). If the component already exists, the arguments are ignored.
- Template Parameters
-
T The type of component to retrieve or add. Must derive from Component.
Args Argument types for the component's constructor.
- Parameters
-
args Arguments forwarded to the component's constructor if it needs to be created.
- Returns
Reference to the existing or newly created component.
Definition at line 230 of file GameObject.ixx.
guid()
| inline nodiscard noexcept |
Returns the unique identifier for this GameObject.
- Returns
Const reference to the Guid, valid for the lifetime of this object.
Definition at line 348 of file GameObject.ixx.
Reference guid_.
Referenced by helios::engine::runtime::factory::GameObjectFactory::fillPool and helios::engine::modules::physics::collision::systems::GridCollisionDetectionSystem::solveCell.
has()
| inline nodiscard noexcept |
Checks if a component with the given type index exists.
Uses the type-indexed componentIndex_ for O(1) amortized lookup.
- Parameters
-
idx The type_index of the component type to check for.
- Returns
True if the component exists, false otherwise.
Definition at line 271 of file GameObject.ixx.
Reference componentIndex_.
Referenced by add.
has()
| inline nodiscard |
Checks if the GameObject has a component of type T.
Normalizes T and uses the type-indexed componentIndex_ for O(1) lookup complexity.
- Template Parameters
-
T The type of component to check for.
- Returns
true if the component exists, false otherwise.
Definition at line 286 of file GameObject.ixx.
Reference componentIndex_.
Referenced by getOrAdd.
hasDisabledComponent()
| inline nodiscard |
Checks if the GameObject has a component of type T and returns true if this component is disabled.
- Template Parameters
-
T The type of component to check for existence and if it's disabled.
- Returns
true if the component exists and is disabled, false otherwise.
Definition at line 320 of file GameObject.ixx.
Reference get.
hasEnabledComponent()
| inline nodiscard |
Checks if the GameObject has a component of type T and returns true if this component is enabled.
- Template Parameters
-
T The type of component to check for existence and if it's enabled.
- Returns
true if the component exists and is enabled, false otherwise.
Definition at line 300 of file GameObject.ixx.
Reference get.
isActive()
| inline nodiscard noexcept |
Checks if this GameObject is currently active.
- Returns
True if the GameObject is active, false otherwise.
Inactive GameObjects are typically skipped during updates and rendering.
Definition at line 359 of file GameObject.ixx.
Reference isActive_.
Referenced by GameObject.
onAcquire()
| inline |
Called when this GameObject is acquired from a pool.
Notifies all attached components by calling their `onAcquire()` method. Used by the pool system to reset or initialize component state before reuse.
- See Also
GameObjectPool
- See Also
Definition at line 416 of file GameObject.ixx.
Reference componentIndex_.
onRelease()
| inline |
Called when this GameObject is released back to a pool.
Notifies all attached components by calling their `onRelease()` method. Used by the pool system to clean up component state before pooling.
- See Also
GameObjectPool
- See Also
Definition at line 431 of file GameObject.ixx.
Reference componentIndex_.
setActive()
| inline noexcept |
Sets the active state of this GameObject.
When the active state changes, this method notifies all attached components by calling onActivate() or onDeactivate() accordingly. If the new state equals the current state, no action is taken. (
- See Also
- Parameters
-
active The new active state.
Definition at line 387 of file GameObject.ixx.
References components_ and isActive_.
Referenced by helios::engine::runtime::factory::GameObjectFactory::fillPool and helios::engine::runtime::pooling::GameObjectPoolFacade::release.
update()
| inline |
Updates all updatable components attached to this GameObject.
- Parameters
-
updateContext Context containing frame delta time and other update data.
Definition at line 337 of file GameObject.ixx.
Reference updatables_.
Protected Member Attributes
componentIndex_
| protected |
Type-indexed map for O(1) amortized component lookups.
Maps `std::type_index` to raw component pointers for efficient retrieval by type. The map is updated when components are added. Worst-case is O(n) due to hash collisions, but typical lookups are O(1).
Definition at line 99 of file GameObject.ixx.
Referenced by add, finalizeAttach, get, getOrAdd, has, has, onAcquire and onRelease.
components_
| protected |
List of components attached to this GameObject.
Components are owned by the GameObject via std::unique_ptr.
Definition at line 90 of file GameObject.ixx.
Referenced by add, components, getOrAdd and setActive.
guid_
| protected |
Unique identifier for this GameObject.
Generated during construction and remains constant for the lifetime of the object. Used for lookups in GameWorld and command targeting.
Definition at line 83 of file GameObject.ixx.
Referenced by GameObject, GameObject and guid.
isActive_
| protected |
Active state flag for this GameObject.
Inactive GameObjects are skipped during iteration and updates. When the state changes, onActivate() or onDeactivate() is called on all components.
Definition at line 115 of file GameObject.ixx.
Referenced by finalizeAttach, GameObject, isActive and setActive.
updatables_
| protected |
Cached list of components that implement the Updatable interface.
This list is maintained for efficient iteration during the update phase. The pointers are non-owning references to elements within components_.
Definition at line 107 of file GameObject.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.