GameObjectPool Class
A reusable object pool for GameObjects. More...
Declaration
Private Member Typedefs Index
| using | Factory = std::function< std::unique_ptr< helios::engine::game::GameObject >()> |
|
Factory function type for creating new GameObjects. More... | |
Public Constructors Index
| GameObjectPool (helios::engine::game::GameWorld &gameWorld, Factory gameObjectFactory, size_t poolSize) | |
|
Constructs a new GameObjectPool. More... | |
Public Member Functions Index
| helios::util::Guid | guid () const noexcept |
|
Returns the unique identifier of this pool. More... | |
| helios::engine::game::GameObject * | acquire () |
|
Acquires the next available GameObject from the pool. More... | |
| bool | release (const helios::engine::game::GameObject *gameObject) |
|
Releases a GameObject back to the pool. More... | |
| bool | release (const helios::util::Guid &guid) |
|
Releases a GameObject back to the pool by its Guid. More... | |
| std::vector< helios::engine::game::GameObject * > | inactiveGameObjects () |
|
Retrieves all currently inactive GameObjects. More... | |
| size_t | activeCount () const noexcept |
|
Returns the number of active game objects. More... | |
| size_t | inactiveCount () const noexcept |
|
Returns the number of inactive game objects. More... | |
Protected Member Functions Index
| void | warmup () |
|
Pre-allocates all pooled objects and adds them to the GameWorld. More... | |
Protected Member Attributes Index
| std::unordered_map< helios::util::Guid, size_t > | activeIndex_ |
|
Maps active GameObject Guids to their index in activeGameObjects_. More... | |
| std::vector< helios::util::Guid > | activeGameObjects_ |
|
List of Guids for currently active (in-use) GameObjects. More... | |
| std::vector< helios::util::Guid > | inactiveGameObjects_ |
|
List of Guids for currently inactive (available) GameObjects. More... | |
| size_t | poolSize_ = 0 |
|
The maximum number of objects this pool manages. More... | |
| helios::engine::game::GameWorld & | gameWorld_ |
|
Reference to the GameWorld that owns the pooled GameObjects. More... | |
| Factory | gameObjectFactory_ |
|
Factory function used to create new GameObjects during warmup. More... | |
| const helios::util::Guid | guid_ |
|
Unique identifier for this pool instance. More... | |
Description
A reusable object pool for GameObjects.
GameObjectPool implements the Object Pool pattern to efficiently manage GameObjects that are frequently created and destroyed. Instead of allocating new objects at runtime, the pool pre-allocates a fixed number of objects during warmup and recycles them.
The pool tracks objects by their Guid and validates them against the GameWorld on acquire to handle external removal gracefully.
Typical use cases:
- Projectile/bullet spawning
- Particle systems
- Enemy wave spawning
Example usage: ```cpp auto factory = []() { auto bullet = std::make_unique<GameObject>(); bullet->addComponent<Move2DComponent>(); return bullet; };
GameObjectPool bulletPool(gameWorld, factory, 100);
// Acquire an object from the pool if (auto* bullet = bulletPool.acquire()) { bullet->get<Move2DComponent>()->setVelocity({0, 1, 0}); }
// Release the object back to the pool bulletPool.release(bullet); ```
Definition at line 72 of file GameObjectPool.ixx.
Private Member Typedefs
Factory
|
Factory function type for creating new GameObjects.
The factory is invoked during pool warmup to create all pooled objects. It should return a fully configured GameObject ready for use.
Definition at line 80 of file GameObjectPool.ixx.
Public Constructors
GameObjectPool()
| inline explicit |
Constructs a new GameObjectPool.
- Parameters
-
gameWorld Reference to the GameWorld that will own the pooled objects.
gameObjectFactory Factory function to create new GameObjects.
poolSize Number of objects to pre-allocate in the pool.
The pool immediately calls warmup() to pre-allocate all objects.
Definition at line 150 of file GameObjectPool.ixx.
References activeGameObjects_, gameObjectFactory_, gameWorld_, guid_, inactiveGameObjects_, poolSize_ and warmup.
Public Member Functions
acquire()
| inline nodiscard |
Acquires the next available GameObject from the pool.
Retrieves an inactive GameObject, validates it against the GameWorld, marks it as active, and returns it. If a Guid references an object that no longer exists in the GameWorld (e.g., externally removed), it is silently discarded and the next available object is tried.
- Returns
Pointer to an activated GameObject, or nullptr if the pool is exhausted.
The returned object is marked as active and tracked internally. Call release() to return it to the pool when done.
Definition at line 190 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, gameWorld_, guid and inactiveGameObjects_.
activeCount()
| inline nodiscard noexcept |
Returns the number of active game objects.
- Returns
The number of active game objects.
Definition at line 313 of file GameObjectPool.ixx.
Reference activeGameObjects_.
guid()
| inline nodiscard noexcept |
Returns the unique identifier of this pool.
- Returns
The Guid assigned to this pool instance.
Definition at line 171 of file GameObjectPool.ixx.
Reference guid_.
Referenced by acquire, inactiveGameObjects, release and warmup.
inactiveCount()
| inline nodiscard noexcept |
Returns the number of inactive game objects.
- Returns
The number of inactive game objects.
Definition at line 322 of file GameObjectPool.ixx.
Reference inactiveGameObjects_.
inactiveGameObjects()
| inline |
Retrieves all currently inactive GameObjects.
Iterates the inactive Guid list and resolves each to a GameObject pointer. Objects that no longer exist in the GameWorld are silently skipped.
- Returns
Vector of pointers to inactive GameObjects. The vector may be smaller than the inactive count if some objects were externally removed.
This operation has O(n) complexity where n is the number of inactive objects.
Definition at line 288 of file GameObjectPool.ixx.
References gameWorld_, guid and inactiveGameObjects_.
release()
| inline |
Releases a GameObject back to the pool.
- Parameters
-
gameObject Pointer to the GameObject to release. May be nullptr.
- Returns
True if the object was successfully released, false if gameObject was nullptr or not tracked by this pool.
Definition at line 226 of file GameObjectPool.ixx.
References helios::engine::game::GameObject::guid and release.
Referenced by release.
release()
| inline |
Releases a GameObject back to the pool by its Guid.
Validates the Guid against both the GameWorld and the active tracking list. Uses swap-and-pop for O(1) removal from the active list. The object is marked inactive and added to the inactive list for future acquisition.
- Parameters
-
guid The unique identifier of the GameObject to release.
- Returns
True if the object was successfully released, false if the Guid was not found in the GameWorld or not tracked as active.
Definition at line 243 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, gameWorld_, guid and inactiveGameObjects_.
Protected Member Functions
warmup()
| inline protected |
Pre-allocates all pooled objects and adds them to the GameWorld.
Creates poolSize_ GameObjects using the factory, sets them inactive, adds them to the GameWorld, and tracks their Guids for later acquisition. Called automatically during construction.
Definition at line 129 of file GameObjectPool.ixx.
References gameObjectFactory_, gameWorld_, guid, inactiveGameObjects_ and poolSize_.
Referenced by GameObjectPool.
Protected Member Attributes
activeGameObjects_
| protected |
List of Guids for currently active (in-use) GameObjects.
Definition at line 94 of file GameObjectPool.ixx.
Referenced by acquire, activeCount, GameObjectPool and release.
activeIndex_
| protected |
Maps active GameObject Guids to their index in activeGameObjects_.
Enables O(1) lookup for release operations.
Definition at line 89 of file GameObjectPool.ixx.
gameObjectFactory_
| protected |
Factory function used to create new GameObjects during warmup.
Definition at line 114 of file GameObjectPool.ixx.
Referenced by GameObjectPool and warmup.
gameWorld_
| protected |
Reference to the GameWorld that owns the pooled GameObjects.
Definition at line 109 of file GameObjectPool.ixx.
Referenced by acquire, GameObjectPool, inactiveGameObjects, release and warmup.
guid_
| protected |
Unique identifier for this pool instance.
Definition at line 119 of file GameObjectPool.ixx.
Referenced by GameObjectPool and guid.
inactiveGameObjects_
| protected |
List of Guids for currently inactive (available) GameObjects.
Definition at line 99 of file GameObjectPool.ixx.
Referenced by acquire, GameObjectPool, inactiveCount, inactiveGameObjects, release and warmup.
poolSize_
| protected |
The maximum number of objects this pool manages.
Definition at line 104 of file GameObjectPool.ixx.
Referenced by GameObjectPool and warmup.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.