EntityRegistry Class
Central registry for creating and managing entity handles. More...
Declaration
Public Constructors Index
| EntityRegistry ()=default | |
|
Default constructor. Creates an empty registry. More... | |
| EntityRegistry (const size_t capacity) | |
|
Constructs an EntityRegistry with pre-allocated capacity. More... | |
Public Member Functions Index
| EntityHandle | create () |
|
Creates a new entity and returns its handle. More... | |
| helios::engine::common::types::VersionId | version (const helios::engine::ecs::types::EntityId entityId) const |
| bool | isValid (const EntityHandle handle) const noexcept |
|
Checks whether the given handle refers to a currently alive entity. More... | |
| bool | destroy (const EntityHandle handle) |
|
Destroys the entity referenced by the given handle. More... | |
| std::span< helios::engine::common::types::VersionId > | version () noexcept |
|
Returns a view of the version vector. More... | |
Private Member Attributes Index
| std::vector< helios::engine::ecs::types::EntityId > | freeList_ |
|
Free list of recycled entity indices available for reuse. More... | |
| std::vector< helios::engine::common::types::VersionId > | versions_ |
|
Version numbers for each entity slot. More... | |
Description
Central registry for creating and managing entity handles.
The `EntityRegistry` serves as the authoritative source for entity lifecycle management in the ECS architecture. It is responsible for:
- **Handle Creation:** Generates unique `EntityHandle` instances with versioned IDs.
- **Validation:** Determines whether a given handle refers to a currently alive entity.
- **Destruction:** Marks entities as destroyed by incrementing their version and recycling the index for future use.
## Versioning
Each entity slot maintains a version number. When an entity is destroyed, its version is incremented. This allows the registry to detect **stale handles** - handles that reference an entity that has been destroyed and potentially replaced by a new entity at the same index.
## Index Recycling
Destroyed entity indices are added to a free list and reused when creating new entities. This keeps the dense version array compact and minimizes memory growth.
## Usage Example
```cpp helios::engine::ecs::EntityRegistry registry;
// Create a new entity auto handle = registry.create();
// Check if the handle is valid if (registry.isValid(handle)) { // Entity is alive, safe to use }
// Destroy the entity registry.destroy(handle);
// The handle is now stale assert(!registry.isValid(handle)); ```
- See Also
- See Also
Definition at line 77 of file EntityRegistry.ixx.
Public Constructors
EntityRegistry()
| default |
Default constructor. Creates an empty registry.
Definition at line 97 of file EntityRegistry.ixx.
EntityRegistry()
| inline explicit |
Constructs an EntityRegistry with pre-allocated capacity.
Reserving capacity upfront can improve performance when the approximate number of entities is known in advance.
- Parameters
-
capacity The initial capacity to reserve for the version vector.
Definition at line 107 of file EntityRegistry.ixx.
Public Member Functions
create()
| inline |
Creates a new entity and returns its handle.
If recycled indices are available in the free list, one is reused. Otherwise, a new index is allocated at the end of the version vector.
- Returns
A valid `EntityHandle` for the newly created entity.
Definition at line 119 of file EntityRegistry.ixx.
References helios::engine::ecs::InitialVersion and version.
destroy()
| inline |
Destroys the entity referenced by the given handle.
If the handle is valid, the entity's version is incremented (invalidating all existing handles to it) and its index is added to the free list for recycling.
- Parameters
-
handle The handle of the entity to destroy.
- Returns
`true` if the entity was successfully destroyed, `false` if the handle was already invalid.
Definition at line 194 of file EntityRegistry.ixx.
References helios::engine::ecs::EntityHandle::entityId and isValid.
isValid()
| inline nodiscard noexcept |
Checks whether the given handle refers to a currently alive entity.
A handle is valid if its index is within bounds and its version matches the current version stored in the registry.
- Parameters
-
handle The handle to validate.
- Returns
`true` if the handle is valid and the entity is alive, `false` otherwise.
Definition at line 171 of file EntityRegistry.ixx.
Referenced by destroy.
version()
| inline nodiscard |
looks up the version for an EntityId.
This method takes an EntityId as the argument and looks up the corresponding version. If the EntityId is not part of the registry, 0 will be returned.
- Parameters
-
entityId The entity to retrieve the version for.
- Returns
The version for the EntityId, or 0 if not found.
Definition at line 153 of file EntityRegistry.ixx.
Referenced by create.
version()
| inline noexcept |
Returns a view of the version vector.
Provides read-write access to the internal version data. Useful for debugging or advanced use cases where direct version inspection is needed.
- Returns
A span over the version vector.
Definition at line 217 of file EntityRegistry.ixx.
Private Member Attributes
freeList_
|
Free list of recycled entity indices available for reuse.
Definition at line 82 of file EntityRegistry.ixx.
versions_
|
Version numbers for each entity slot.
The version is incremented when an entity at that index is destroyed, allowing detection of stale handles.
Definition at line 90 of file EntityRegistry.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.