GameObjectPoolManager Class
High-level manager for GameObject pooling operations. More...
Declaration
Public Member Typedefs Index
| using | EngineRoleTag = helios::engine::common::tags::ManagerRole |
Public Member Functions Index
| GameObjectPoolManager & | addPoolConfig (std::unique_ptr< GameObjectPoolConfig > gameObjectPoolConfig) |
|
Registers a pool configuration for later initialization. More... | |
| helios::engine::runtime::pooling::GameObjectPoolSnapshot | poolSnapshot (const helios::engine::runtime::pooling::types::GameObjectPoolId gameObjectPoolId) const |
|
Returns a snapshot of the pool's current state. More... | |
| std::optional< helios::engine::ecs::GameObject > | release (const helios::engine::runtime::pooling::types::GameObjectPoolId gameObjectPoolId, const helios::engine::ecs::EntityHandle &entityHandle) |
|
Releases a GameObject back to its pool. More... | |
| std::optional< helios::engine::ecs::GameObject > | acquire (const helios::engine::runtime::pooling::types::GameObjectPoolId gameObjectPoolId) |
|
Acquires an inactive GameObject from the pool. More... | |
| void | init (helios::engine::runtime::world::GameWorld &gameWorld) |
|
Initializes all registered pools. More... | |
| void | flush (helios::engine::runtime::world::UpdateContext &update_context) noexcept |
|
Manager flush callback (currently unused). More... | |
| GameObjectPool * | pool (const helios::engine::runtime::pooling::types::GameObjectPoolId gameObjectPoolId) const |
|
Retrieves a pool by its ID. More... | |
| void | reset () |
|
Resets all pools by releasing all active GameObjects. More... | |
| void | reset (const types::GameObjectPoolId poolId) |
|
Reset the pool with the specified poolId. More... | |
Private Member Functions Index
| void | fillPool (const helios::engine::runtime::pooling::types::GameObjectPoolId gameObjectPoolId, helios::engine::ecs::GameObject gameObjectPrefab) |
|
Populates a pool with cloned prefab instances and locks it. More... | |
Private Member Attributes Index
| helios::engine::runtime::pooling::GameObjectPoolRegistry | pools_ {} |
|
Registry of GameObjectPools for entity recycling. More... | |
| helios::engine::runtime::world::GameWorld * | gameWorld_ = nullptr |
|
Non-owning pointer to the associated GameWorld. More... | |
| std::unordered_map< helios::engine::runtime::pooling::types::GameObjectPoolId, std::unique_ptr< GameObjectPoolConfig > > | poolConfigs_ |
|
Pending pool configurations awaiting initialization. More... | |
Description
High-level manager for GameObject pooling operations.
GameObjectPoolManager provides a unified interface for managing multiple GameObjectPools. It handles pool configuration, initialization, and the acquire/release lifecycle of pooled GameObjects.
The manager integrates with the GameWorld to clone prefabs during pool initialization and to look up GameObjects by their EntityHandle during acquire/release operations.
## Lifecycle
1. **Configuration**: Add pool configurations via `addPoolConfig()`. 2. **Initialization**: Call `init()` to create pools and populate them with cloned prefab instances. This **locks** the pools. 3. **Runtime**: Use `acquire()` and `release()` to manage entity lifecycle.
## Pool Locking
After `init()` completes, each pool is **locked**. A locked pool:
- Cannot accept new EntityHandles via `addInactive()`
- Has its sparse arrays sized based on min/max EntityIds
- Is ready for O(1) acquire/release operations
This design optimizes memory layout but means the pool size is fixed at initialization time.
## Trade-offs
- **Fixed Capacity**: Pool size cannot grow after initialization. If more entities are needed, `acquire()` returns nullptr when exhausted.
- **Memory Overhead**: Sparse arrays are sized for the EntityId range, which may waste memory if EntityIds are not contiguous.
- **Initialization Cost**: All prefab clones are created upfront during `init()`, which may cause a startup delay for large pools.
## Example
```cpp GameObjectPoolManager poolManager;
// Create prefab auto bulletPrefab = gameWorld.addGameObject(); bulletPrefab.add<RenderableComponent>(mesh, material); bulletPrefab.add<Move2DComponent>(); bulletPrefab.setActive(false);
auto config = std::make_unique<GameObjectPoolConfig>( bulletPoolId, bulletPrefab, 100 ); poolManager.addPoolConfig(std::move(config));
poolManager.init(gameWorld);
// Acquire returns std::optional<GameObject> if (auto bullet = poolManager.acquire(bulletPoolId)) { bullet->get<TranslationStateComponent>()->setTranslation(spawnPos); bullet->setActive(true); }
// Release back to pool poolManager.release(bulletPoolId, bullet->entityHandle()); ```
- See Also
- See Also
- See Also
Manager
Definition at line 105 of file GameObjectPoolManager.ixx.
Public Member Typedefs
EngineRoleTag
|
Definition at line 175 of file GameObjectPoolManager.ixx.
Public Member Functions
acquire()
| inline nodiscard |
Acquires an inactive GameObject from the pool.
Retrieves the next available inactive entity and calls `onAcquire()` to trigger component initialization hooks. The caller is responsible for activating the entity via `setActive(true)`.
If an acquired entity is no longer valid in the GameWorld (stale handle), it is removed from the pool and the next available entity is tried.
- Parameters
-
gameObjectPoolId The pool to acquire from.
- Returns
Optional containing the acquired GameObject if available, std::nullopt if the pool is exhausted.
Definition at line 266 of file GameObjectPoolManager.ixx.
Reference pool.
addPoolConfig()
| inline |
Registers a pool configuration for later initialization.
The configuration specifies the pool ID, capacity, and prefab to use for cloning. Configurations are processed during `init()`.
- Parameters
-
gameObjectPoolConfig The pool configuration to register.
- Returns
Reference to this manager for method chaining.
- Exceptions
-
std::runtime_error If a pool with the same ID is already registered.
Definition at line 189 of file GameObjectPoolManager.ixx.
flush()
| inline noexcept |
Manager flush callback (currently unused).
Reserved for future use. May be used to process deferred release requests or pool maintenance operations.
- Parameters
-
gameWorld The associated GameWorld.
update_context The current frame's update context.
Definition at line 331 of file GameObjectPoolManager.ixx.
init()
| inline |
Initializes all registered pools.
Creates GameObjectPool instances from the pending configurations and populates them by cloning the specified prefabs into the GameWorld.
- Parameters
-
gameWorld The GameWorld to associate with this manager.
Definition at line 300 of file GameObjectPoolManager.ixx.
References pool and helios::engine::runtime::world::GameWorld::view.
pool()
| inline nodiscard |
Retrieves a pool by its ID.
- Parameters
-
gameObjectPoolId The pool ID to look up.
- Returns
Pointer to the GameObjectPool.
- Precondition
The pool must be registered with this manager.
Definition at line 346 of file GameObjectPoolManager.ixx.
Referenced by acquire, init, poolSnapshot, release and reset.
poolSnapshot()
| inline nodiscard |
Returns a snapshot of the pool's current state.
Provides the active and inactive counts for monitoring and debugging purposes.
- Parameters
-
gameObjectPoolId The pool to query.
- Returns
A GameObjectPoolSnapshot containing active and inactive counts.
Definition at line 210 of file GameObjectPoolManager.ixx.
Reference pool.
Referenced by helios::engine::runtime::spawn::policy::amount::SpawnAll::getAmount.
release()
| inline |
Releases a GameObject back to its pool.
Marks the entity as inactive in both the pool and the GameWorld. Calls `onRelease()` on the GameObject to trigger component cleanup hooks, then deactivates it.
- Parameters
-
gameObjectPoolId The pool that owns this entity.
entityHandle The EntityHandle of the entity to release.
- Returns
Optional containing the released GameObject if found, std::nullopt if the entity was not found in the GameWorld.
Definition at line 233 of file GameObjectPoolManager.ixx.
Reference pool.
Referenced by reset.
reset()
| inline |
Resets all pools by releasing all active GameObjects.
Iterates through all registered pools and releases every active entity back to its pool. This effectively returns all pooled objects to their inactive state without destroying them.
Useful for level transitions or game restarts where all pooled objects should be recycled.
- See Also
Definition at line 365 of file GameObjectPoolManager.ixx.
Reference reset.
Referenced by reset.
reset()
| inline |
Reset the pool with the specified poolId.
Releases every active entity back to its pool specified by poolId. This effectively returns all pooled objects to their inactive state without destroying them.
- Parameters
-
poolId The ID of the pool to reset.
Definition at line 380 of file GameObjectPoolManager.ixx.
Private Member Functions
fillPool()
| inline |
Populates a pool with cloned prefab instances and locks it.
Clones the prefab GameObject until the pool reaches its configured capacity. Each clone is: 1. Added to the GameWorld 2. Immediately deactivated (`setActive(false)`) 3. Prepared for pooling (`onRelease()`) 4. Registered as inactive in the pool
After all clones are created, the pool is **locked** via `lock()`. Locking finalizes the sparse array sizing based on the min/max EntityIds and enables O(1) acquire/release operations. Once locked, no new EntityHandles can be added to the pool.
- Parameters
-
gameObjectPoolId The pool to fill.
gameObjectPrefab The prefab to clone.
- Postcondition
The pool is locked and ready for acquire/release operations.
Definition at line 153 of file GameObjectPoolManager.ixx.
Private Member Attributes
gameWorld_
|
Non-owning pointer to the associated GameWorld.
Set during `init()`. Used for cloning prefabs and looking up GameObjects by their EntityHandle.
Definition at line 121 of file GameObjectPoolManager.ixx.
poolConfigs_
|
Pending pool configurations awaiting initialization.
Configurations are consumed during `init()` to create and populate the actual pools.
Definition at line 131 of file GameObjectPoolManager.ixx.
pools_
|
Registry of GameObjectPools for entity recycling.
Pools enable efficient reuse of GameObjects without repeated allocation/deallocation. Each pool is identified by a GameObjectPoolId.
Definition at line 113 of file GameObjectPoolManager.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.