ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
Imago::ECS::EntityManager Class Reference

Creates, destroys, and validates Entity handles. More...

#include <Imago/ECS/EntityManager.hpp>

Public Member Functions

Entity create ()
 Creates a new Entity, reusing a freed index when one is available.
void destroy (Entity)
 Destroys an Entity, freeing its index for future reuse.
bool is_valid (Entity) const
 Checks whether an Entity handle is still valid.
uint32_t get_alive_count () const
 Returns the number of currently-alive entities.

Detailed Description

Creates, destroys, and validates Entity handles.

EntityManager is the single source of truth for which entities currently exist. Destroyed indices are recycled oldest-first, with a bumped generation, so a stale Entity handle from before a Destroy() call correctly reports itself as invalid via IsValid(), even after its index has been reused by a new entity.

Example
EntityManager manager;
Entity player = manager.Create();
assert(manager.IsValid(player));
manager.Destroy(player);
assert(!manager.IsValid(player)); // stale handle, now invalid
uint32_t Entity
A packed identifier: 20 bits of index, 12 bits of generation.
Definition Entity.hpp:20

Member Function Documentation

◆ create()

Entity Imago::ECS::EntityManager::create ( )

Creates a new Entity, reusing a freed index when one is available.

Returns
A newly valid Entity handle.
Warning
Not thread-safe.

◆ destroy()

void Imago::ECS::EntityManager::destroy ( Entity e)

Destroys an Entity, freeing its index for future reuse.

Bumps the destroyed index's generation, immediately invalidating every outstanding copy of this Entity handle.

Parameters
entityThe Entity to destroy.
Note
Destroying an already-invalid Entity is a safe no-op, not an error.

◆ is_valid()

bool Imago::ECS::EntityManager::is_valid ( Entity e) const

Checks whether an Entity handle is still valid.

An Entity is valid if its index is currently allocated AND its generation matches the generation currently stored for that index.

Parameters
entityThe Entity to check. Safe to call with any value, including NULL_ENTITY.
Returns
true if the entity is currently alive.

The documentation for this class was generated from the following files: