EntityManager Class
Manages entities and their associated components. More...
Declaration
Public Constructors Index
| EntityManager (EntityRegistry ®istry, size_t capacity=ENTITY_MANAGER_DEFAULT_CAPACITY) | |
|
Constructs an EntityManager with the given registry. More... | |
Public Member Functions Index
| EntityHandle | create () |
|
Creates a new entity. More... | |
| bool | isValid (const EntityHandle handle) const noexcept |
|
Checks if an entity handle is valid. More... | |
| bool | isValid (const EntityId entityId) const noexcept |
|
Checks if an entity ID is valid. More... | |
| bool | destroy (const EntityHandle &handle) |
|
Destroys an entity and invalidates its handle. More... | |
template <typename T> | |
| T * | get (const EntityHandle handle) const |
|
Retrieves a component for the given entity. More... | |
template <typename T> | |
| auto | getSparseSet () -> SparseSet< T > * |
template <typename T> | |
| auto | getSparseSet () const -> const SparseSet< T > * |
template <typename T> | |
| bool | has (const EntityHandle handle) const |
|
Checks whether an entity has a specific component. More... | |
| bool | has (const EntityHandle handle, const helios::engine::ecs::types::ComponentTypeId typeId) const |
|
Checks whether an entity has a component by type ID. More... | |
| void | enable (const EntityHandle handle, const helios::engine::ecs::types::ComponentTypeId typeId) const |
|
Enables a component by type ID. More... | |
| void | disable (const EntityHandle handle, const helios::engine::ecs::types::ComponentTypeId typeId) const |
|
Disables a component by type ID. More... | |
| void | enable (const EntityHandle handle, const helios::engine::ecs::types::ComponentTypeId typeId, const bool enable) const |
|
Enables or disables a component by type ID. More... | |
template <typename T, typename... Args> | |
| T * | emplace (const EntityHandle handle, Args &&...args) |
|
Constructs and attaches a component to an entity. More... | |
template <typename T, typename... Args> | |
| T * | emplaceOrGet (const EntityHandle handle, Args &&...args) |
|
Returns existing component or creates a new one. More... | |
template <typename T> | |
| bool | remove (const EntityHandle &handle) |
|
Removes a specific component from an entity. More... | |
| std::generator< helios::engine::ecs::types::ComponentTypeId > | componentTypeIds (const EntityHandle handle) const |
|
Returns a generator over all component type IDs attached to an entity. More... | |
| void | clone (const EntityHandle source, const EntityHandle target) |
|
Clones all components from source entity to target entity. More... | |
| void * | raw (const EntityHandle handle, const helios::engine::ecs::types::ComponentTypeId typeId) const |
|
Returns raw void pointer to a component. More... | |
| EntityHandle | handle (const EntityId entityId) const |
|
Reconstructs an EntityHandle from an EntityId. More... | |
Private Member Attributes Index
| std::vector< std::unique_ptr< SparseSetBase > > | components_ |
|
Component storage indexed by type ID. More... | |
| EntityRegistry & | registry_ |
|
Reference to the entity registry for handle management. More... | |
| const size_t | capacity_ |
|
initial capacity for the underlying sparsesets. More... | |
Description
Manages entities and their associated components.
`EntityManager` provides a unified interface for creating entities and attaching/retrieving components. It delegates handle management to an `EntityRegistry` and stores component data in type-specific `SparseSet` containers.
## Responsibilities
- **Entity Creation:** Delegates to `EntityRegistry` for handle allocation.
- **Entity Destruction:** Removes all components and invalidates the handle.
- **Component Storage:** Maintains a vector of `SparseSet` instances, one per component type, indexed by `TypeIndexer`.
## Usage
```cpp EntityRegistry registry; EntityManager manager(registry);
auto entity = manager.create(); auto* transform = manager.emplace<TransformComponent>(entity, glm::vec3{0.0f});
if (manager.has<TransformComponent>(entity)) { auto* t = manager.get<TransformComponent>(entity); }
manager.remove<TransformComponent>(entity); // Remove single component manager.destroy(entity); // Destroy entity and all components ```
- See Also
- See Also
- See Also
Definition at line 67 of file EntityManager.ixx.
Public Constructors
EntityManager()
| inline explicit |
Constructs an EntityManager with the given registry.
- Parameters
-
registry The EntityRegistry used for handle allocation and validation.
Definition at line 92 of file EntityManager.ixx.
Public Member Functions
clone()
| inline |
Clones all components from source entity to target entity.
Iterates through all components on the source and copies them to the target using the registered clone function. Skips components that already exist on the target.
- Parameters
-
source The entity to clone from.
target The entity to clone to.
Definition at line 467 of file EntityManager.ixx.
References componentTypeIds, has, helios::engine::ecs::ComponentOpsRegistry::ops and raw.
componentTypeIds()
| inline |
Returns a generator over all component type IDs attached to an entity.
- Parameters
-
handle The entity to query.
- Returns
Generator yielding ComponentTypeId for each attached component.
Definition at line 445 of file EntityManager.ixx.
Reference handle.
Referenced by clone.
create()
| inline nodiscard |
Creates a new entity.
Delegates to the underlying `EntityRegistry` to allocate a new handle.
- Returns
A valid `EntityHandle` for the newly created entity.
- See Also
Definition at line 104 of file EntityManager.ixx.
destroy()
| inline nodiscard |
Destroys an entity and invalidates its handle.
Increments the entity's version in the registry, making all existing handles to this entity stale. Does not automatically remove components from storage.
- Parameters
-
handle The handle of the entity to destroy.
- Returns
`true` if the entity was destroyed, `false` if already invalid.
Definition at line 141 of file EntityManager.ixx.
References handle, helios::engine::ecs::ComponentOpsRegistry::ops and raw.
disable()
| inline |
Disables a component by type ID.
Calls the component's `disable()` method if registered.
- Parameters
-
handle The entity whose component to disable.
typeId The component type identifier.
Definition at line 296 of file EntityManager.ixx.
emplace()
| inline |
Constructs and attaches a component to an entity.
If the component type has not been registered yet, a new `SparseSet` is created. The component is constructed in-place with the provided arguments. Return nullptre if the component already exists or if the handle was invalid.
- Template Parameters
-
T The component type to emplace.
Args Constructor argument types.
- Parameters
-
handle The entity to attach the component to.
args Arguments forwarded to the component constructor.
- Returns
Pointer to the newly created component, or `nullptr` if the handle is invalid.
Definition at line 344 of file EntityManager.ixx.
References handle, helios::engine::ecs::types::ComponentTypeId::id and helios::engine::ecs::types::ComponentTypeId::value.
Referenced by emplaceOrGet.
emplaceOrGet()
| inline |
Returns existing component or creates a new one.
- Template Parameters
-
T The component type.
Args Constructor argument types.
- Parameters
-
handle The entity.
args Arguments forwarded to the constructor if creating.
- Returns
Pointer to the existing or newly created component, or `nullptr` if the handle is invalid.
Definition at line 384 of file EntityManager.ixx.
enable()
| inline |
Enables a component by type ID.
Calls the component's `enable()` method if registered.
- Parameters
-
handle The entity whose component to enable.
typeId The component type identifier.
Definition at line 284 of file EntityManager.ixx.
enable()
| inline |
Enables or disables a component by type ID.
- Parameters
-
handle The entity whose component to modify.
typeId The component type identifier.
enable `true` to enable, `false` to disable.
Definition at line 307 of file EntityManager.ixx.
References enable, handle, has, helios::engine::ecs::ComponentOpsRegistry::ops and raw.
get()
| inline nodiscard |
Retrieves a component for the given entity.
- Template Parameters
-
T The component type to retrieve.
- Parameters
-
handle The entity whose component to retrieve.
- Returns
Pointer to the component, or `nullptr` if the entity is invalid or does not have the requested component.
Definition at line 178 of file EntityManager.ixx.
References handle, has, helios::engine::ecs::types::ComponentTypeId::id and helios::engine::ecs::types::ComponentTypeId::value.
Referenced by emplaceOrGet.
getSparseSet()
| inline nodiscard |
Returns the SparseSet for a component type.
- Template Parameters
-
T The component type.
- Returns
Pointer to the SparseSet, or `nullptr` if the type has no storage.
Definition at line 199 of file EntityManager.ixx.
References helios::engine::ecs::types::ComponentTypeId::id and helios::engine::ecs::types::ComponentTypeId::value.
getSparseSet()
| inline nodiscard |
Returns the SparseSet for a component type (const).
- Template Parameters
-
T The component type.
- Returns
Const pointer to the SparseSet, or `nullptr` if the type has no storage.
Definition at line 218 of file EntityManager.ixx.
References helios::engine::ecs::types::ComponentTypeId::id and helios::engine::ecs::types::ComponentTypeId::value.
handle()
| inline nodiscard |
Reconstructs an EntityHandle from an EntityId.
- Parameters
-
entityId The entity ID.
- Returns
EntityHandle with current version from the registry.
Definition at line 513 of file EntityManager.ixx.
Referenced by componentTypeIds, destroy, disable, emplace, emplaceOrGet, enable, enable, get, has, has, isValid, isValid, raw and remove.
has()
| inline nodiscard |
Checks whether an entity has a specific component.
- Template Parameters
-
T The component type to check for.
- Parameters
-
handle The entity to query.
- Returns
`true` if the entity has the component, `false` if the handle is invalid or the component is not attached.
Definition at line 240 of file EntityManager.ixx.
References handle, helios::engine::ecs::types::ComponentTypeId::id and helios::engine::ecs::types::ComponentTypeId::value.
has()
| inline nodiscard |
Checks whether an entity has a component by type ID.
- Parameters
-
handle The entity to query.
typeId The component type identifier.
- Returns
`true` if the entity has the component, `false` otherwise.
Definition at line 262 of file EntityManager.ixx.
References handle and helios::engine::ecs::types::ComponentTypeId::value.
isValid()
| inline nodiscard noexcept |
Checks if an entity handle is valid.
- Parameters
-
handle The handle to validate.
- Returns
`true` if the handle refers to a living entity.
Definition at line 115 of file EntityManager.ixx.
Reference handle.
isValid()
| inline nodiscard noexcept |
Checks if an entity ID is valid.
- Parameters
-
entityId The entity ID to validate.
- Returns
`true` if the entity ID refers to a living entity.
Definition at line 126 of file EntityManager.ixx.
Reference handle.
raw()
| inline nodiscard |
Returns raw void pointer to a component.
- Parameters
-
handle The entity.
typeId The component type identifier.
- Returns
Raw pointer to the component, or `nullptr` if not found.
Definition at line 498 of file EntityManager.ixx.
References handle, has and helios::engine::ecs::types::ComponentTypeId::value.
Referenced by clone, destroy, emplaceOrGet, enable and remove.
remove()
| inline |
Removes a specific component from an entity.
Unlike `destroy()`, this only removes a single component type while keeping the entity and other components intact.
- Template Parameters
-
T The component type to remove.
- Parameters
-
handle The entity whose component to remove.
- Returns
`true` if the component was removed, `false` if the handle was invalid, the component was not attached, or removal was blocked by `onRemove`.
- See Also
- See Also
Definition at line 417 of file EntityManager.ixx.
References handle, has, helios::engine::ecs::types::ComponentTypeId::id, helios::engine::ecs::ComponentOpsRegistry::ops and raw.
Private Member Attributes
capacity_
|
initial capacity for the underlying sparsesets.
Definition at line 83 of file EntityManager.ixx.
components_
|
Component storage indexed by type ID.
@odo sort after size()
Definition at line 73 of file EntityManager.ixx.
registry_
|
Reference to the entity registry for handle management.
Definition at line 78 of file EntityManager.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.