Skip to content

Class Engine::Registry

ClassList > Engine > Registry

The core of the ECS. Manages entities, components, and systems. More...

  • #include <Registry.hpp>

Public Functions

Type Name
void AddComponent (Entity entity, T component)
Adds a component to an entity.
T & AddResource (Args &&... args)
Registers a global resource.
void ClearEntities ()
Destroys every entity and restarts entity IDs at 1.
Entity CreateEntity ()
Creates a new entity and gives it a default Position andRotation .
void DestroyEntity (const Entity entity)
Destroys an entity and its entire subtree (entities with Parent pointing here).
void FlushQueues ()
Safely executes all deferred destructions. Call at the end of the frame.
std::optional< std::reference_wrapper< T > > GetComponent (Entity entity)
Retrieves a component for a specific entity.
std::shared_ptr< ComponentArray< T > > GetComponentArray ()
Helper function to get the strongly-typed component array.
size_t GetEntityCount () const
Returns the total number of entities created (used by the Editor for iterating).
T & GetResource ()
Retrieves a global resource.
std::shared_ptr< T > GetSystem ()
Gets a registered system.
const std::vector< std::type_index > & GetSystemRenderOrder () const
Registration order for System::Render (stable across frames).
const std::vector< std::type_index > & GetSystemUpdateOrder () const
Registration order for System::Update (stable across frames).
const std::unordered_map< std::type_index, std::shared_ptr< System > > & GetSystems () const
Returns all registered systems.
bool HasComponent (Entity entity)
Checks if an entity has a specific component.
bool HasComponentByType (const std::type_index & type, Entity entity) const
Type-erased component existence check used by scripting glue.
bool HasResource () const
Checks if a resource exists.
bool IsComponentRegistered () const
Checks if a component type has been registered.
bool IsEntityAlive (Entity entity) const
Checks if an entity is alive (holds at least one component).
void QueueDestroyEntity (Entity entity)
Defers entity destruction to the end of the frame. Safe to call inside physics/collision callbacks!
Registry * RegisterComponent ()
Registers a component type with the registry.
std::shared_ptr< T > RegisterSystem ()
Registers a system type with the registry.
Registry ()
void RemoveComponent (Entity entity)
Removes a component from an entity.
void RemoveComponentByType (const std::type_index & type, Entity entity)
Type-erased component removal used by scripting glue.
T & RequireComponent (Entity entity)
Retrieves a component that must already be on the entity.
void Shutdown ()
Destroys every entity and every resource now.

Detailed Description

The registry serves as the coordinator for the entire ECS architecture, providing methods to create entities, attach components, and register systems.

Public Functions Documentation

function AddComponent

Adds a component to an entity.

template<typename T>
inline void Engine::Registry::AddComponent (
    Entity entity,
    T component
) 

Template parameters:

  • T The component type.

Parameters:

  • entity The entity ID.
  • component The component data.

function AddResource

Registers a global resource.

template<typename T, typename... Args>
inline T & Engine::Registry::AddResource (
    Args &&... args
) 

Template parameters:

  • T The resource type.
  • Args Constructor argument types.

Returns:

Reference to the created resource.


function ClearEntities

Destroys every entity and restarts entity IDs at 1.

inline void Engine::Registry::ClearEntities () 

Used when switching scenes. Component types, systems and resources stay registered: only entity data is removed. Each system's m_Entities set and the deferred destroy queue are emptied too, so nothing points at an old entity once IDs start being reused.


function CreateEntity

Creates a new entity and gives it a default Position andRotation .

inline Entity Engine::Registry::CreateEntity () 

Returns:

The ID of the newly created entity.


function DestroyEntity

Destroys an entity and its entire subtree (entities with Parent pointing here).

inline void Engine::Registry::DestroyEntity (
    const Entity entity
) 

Parameters:

  • entity The ID of the entity to destroy.

function FlushQueues

Safely executes all deferred destructions. Call at the end of the frame.

inline void Engine::Registry::FlushQueues () 


function GetComponent

Retrieves a component for a specific entity.

template<typename T>
inline std::optional< std::reference_wrapper< T > > Engine::Registry::GetComponent (
    Entity entity
) 

Missing components are empty results. Callers that require the component use RequireComponent.

Template parameters:

  • T The component type.

Parameters:

  • entity The entity ID.

Returns:

The component when the entity has it, otherwise std::nullopt. Also empty when T was never registered.


function GetComponentArray

Helper function to get the strongly-typed component array.

template<typename T>
inline std::shared_ptr< ComponentArray < T > > Engine::Registry::GetComponentArray () 

Template parameters:

  • T The component type.

Returns:

Shared pointer to the ComponentArray.


function GetEntityCount

Returns the total number of entities created (used by the Editor for iterating).

inline size_t Engine::Registry::GetEntityCount () const


function GetResource

Retrieves a global resource.

template<typename T>
inline T & Engine::Registry::GetResource () 

Template parameters:

  • T The resource type.

Returns:

Reference to the resource.


function GetSystem

Gets a registered system.

template<typename T>
inline std::shared_ptr< T > Engine::Registry::GetSystem () 

Template parameters:

  • T The system type.

Returns:

Shared pointer to the system.


function GetSystemRenderOrder

Registration order for System::Render (stable across frames).

inline const std::vector< std::type_index > & Engine::Registry::GetSystemRenderOrder () const


function GetSystemUpdateOrder

Registration order for System::Update (stable across frames).

inline const std::vector< std::type_index > & Engine::Registry::GetSystemUpdateOrder () const


function GetSystems

Returns all registered systems.

inline const std::unordered_map< std::type_index, std::shared_ptr< System > > & Engine::Registry::GetSystems () const


function HasComponent

Checks if an entity has a specific component.

template<typename T>
inline bool Engine::Registry::HasComponent (
    Entity entity
) 


function HasComponentByType

Type-erased component existence check used by scripting glue.

inline bool Engine::Registry::HasComponentByType (
    const std::type_index & type,
    Entity entity
) const

Parameters:

  • type The component type index.
  • entity The entity ID.

Returns:

True if the component is registered and attached to the entity.


function HasResource

Checks if a resource exists.

template<typename T>
inline bool Engine::Registry::HasResource () const

Template parameters:

  • T The resource type.

Returns:

True if the resource exists.


function IsComponentRegistered

Checks if a component type has been registered.

template<typename T>
inline bool Engine::Registry::IsComponentRegistered () const

Template parameters:

  • T The component type.

Returns:

True if RegisterComponent<T>() has been called.


function IsEntityAlive

Checks if an entity is alive (holds at least one component).

inline bool Engine::Registry::IsEntityAlive (
    Entity entity
) const

Entity IDs are not reused while a scene is running, so destroyed entities keep their ID but own no components. Editor panels use this to skip dead rows. Only ClearEntities restarts IDs at 1.

Parameters:

  • entity The entity ID.

Returns:

True if any component is attached to the entity.


function QueueDestroyEntity

Defers entity destruction to the end of the frame. Safe to call inside physics/collision callbacks!

inline void Engine::Registry::QueueDestroyEntity (
    Entity entity
) 


function RegisterComponent

Registers a component type with the registry.

template<typename T>
inline Registry * Engine::Registry::RegisterComponent () 

Template parameters:

  • T The component type.

function RegisterSystem

Registers a system type with the registry.

template<typename T>
inline std::shared_ptr< T > Engine::Registry::RegisterSystem () 

Template parameters:

  • T The system type.

Returns:

Shared pointer to the registered system.


function Registry

inline Engine::Registry::Registry () 

function RemoveComponent

Removes a component from an entity.

template<typename T>
inline void Engine::Registry::RemoveComponent (
    Entity entity
) 

Template parameters:

  • T The component type.

Parameters:

  • entity The entity ID.

function RemoveComponentByType

Type-erased component removal used by scripting glue.

inline void Engine::Registry::RemoveComponentByType (
    const std::type_index & type,
    Entity entity
) 

Parameters:

  • type The component type index.
  • entity The entity ID.

function RequireComponent

Retrieves a component that must already be on the entity.

template<typename T>
inline T & Engine::Registry::RequireComponent (
    Entity entity
) 

Template parameters:

  • T The component type.

Parameters:

  • entity The entity ID.

Returns:

Reference to the requested component.

Exception:

  • ErrorEcs if T is not registered or the entity does not have it.

function Shutdown

Destroys every entity and every resource now.

inline void Engine::Registry::Shutdown () 

Lets the application free GPU/audio-backed data (textures, sounds, fonts) while the window and audio device are still alive, instead of during member destruction after CloseWindow().



The documentation for this class was generated from the following file /home/runner/work/r-type/r-type/Engine/include/ECS/Registry.hpp