VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::Registry Class Reference

Central registry for managing entities and their components in the ECS system. More...

#include <Registry.hpp>

Public Member Functions

template<typename T>
ComponentStorage< T > * try_get_storage_internal ()
 Attempts to retrieve the storage for component type T without creating it.
Entity create ()
 Creates a new entity.
void destroy (Entity entity)
 Destroys an entity and all its associated components.
template<typename T, typename... Args>
T & add_or_replace (Entity entity, Args &&... args)
 Adds or replaces a component for an entity.
template<typename T>
void remove (Entity entity)
 Removes a component from an entity.
template<typename T>
T & get (Entity entity)
 Retrieves a component from an entity.
template<typename T>
T * try_get (Entity entity)
 Safely retrieves a component from an entity if it exists.
template<typename T>
bool has (Entity entity)
 Checks if an entity has a component of type T.
template<typename T>
void on_create (std::function< void(Entity, T &)> cb)
 Registers a callback to be invoked when a component of type T is created.
template<typename T>
void on_destroy (std::function< void(Entity, T &)> cb)
 Registers a callback to be invoked when a component of type T is destroyed.

Private Member Functions

template<typename T>
ComponentStorage< T > * get_storage ()
 Retrieves or creates the storage for component type T.

Private Attributes

std::vector< Entitym_availableEntities
 Pool of reusable entity IDs from destroyed entities.
Entity m_nextEntity = 0
 Counter for generating new entity IDs when the pool is empty.
std::vector< std::unique_ptr< IComponentStorage > > m_componentStorages
 Array of component storages indexed by component type ID.

Detailed Description

Central registry for managing entities and their components in the ECS system.

The Registry handles entity creation and destruction, component storage management, and provides a unified interface for component access. It maintains a pool of available entity IDs for reuse and dynamically creates storage for each component type as needed.

Examples
/home/devu/Documents/Projects/VEX/Engine/Core/include/components/ECS/ECS.hpp.

Definition at line 29 of file Registry.hpp.

Member Function Documentation

◆ add_or_replace()

template<typename T, typename... Args>
T & vex::Registry::add_or_replace ( Entity entity,
Args &&... args )
inline

Adds or replaces a component for an entity.

Template Parameters
TThe component type.
ArgsParameter types for the component constructor.
Parameters
entityThe entity to add/replace the component for.
argsArguments forwarded to the component constructor.
Returns
Reference to the newly added or replaced component.
Examples
/home/devu/Documents/Projects/VEX/Engine/Core/include/components/ECS/ECS.hpp.

Definition at line 100 of file Registry.hpp.

◆ create()

Entity vex::Registry::create ( )
inline

Creates a new entity.

Returns
The ID of the newly created entity.

Reuses entity IDs from destroyed entities if available, otherwise generates a new ID from the counter. This provides stable entity IDs even with entity recycling.

Examples
/home/devu/Documents/Projects/VEX/Engine/Core/include/components/ECS/ECS.hpp.

Definition at line 73 of file Registry.hpp.

◆ destroy()

void vex::Registry::destroy ( Entity entity)
inline

Destroys an entity and all its associated components.

Parameters
entityThe entity to destroy.

Notifies all component storages of the entity destruction, triggering onDestroy callbacks and cleanup. The entity ID is returned to the pool for reuse.

Examples
/home/devu/Documents/Projects/VEX/Engine/Core/include/components/ECS/ECS.hpp.

Definition at line 86 of file Registry.hpp.

◆ get()

template<typename T>
T & vex::Registry::get ( Entity entity)
inline

Retrieves a component from an entity.

Template Parameters
TThe component type.
Parameters
entityThe entity to get the component from.
Returns
Mutable reference to the component.
Precondition
The entity must have the component.

Definition at line 118 of file Registry.hpp.

◆ get_storage()

template<typename T>
ComponentStorage< T > * vex::Registry::get_storage ( )
inlineprivate

Retrieves or creates the storage for component type T.

Template Parameters
TThe component type.
Returns
Pointer to the ComponentStorage for type T.

Lazily initializes storage on first access and resizes the storage array as needed to accommodate new component types.

Definition at line 44 of file Registry.hpp.

◆ has()

template<typename T>
bool vex::Registry::has ( Entity entity)
inline

Checks if an entity has a component of type T.

Template Parameters
TThe component type to check for.
Parameters
entityThe entity to check.
Returns
True if the entity has the component, false otherwise.

Definition at line 137 of file Registry.hpp.

◆ on_create()

template<typename T>
void vex::Registry::on_create ( std::function< void(Entity, T &)> cb)
inline

Registers a callback to be invoked when a component of type T is created.

Template Parameters
TThe component type.
Parameters
cbThe callback function to register.
Examples
/home/devu/Documents/Projects/VEX/Engine/Core/include/components/ECS/ECS.hpp.

Definition at line 146 of file Registry.hpp.

◆ on_destroy()

template<typename T>
void vex::Registry::on_destroy ( std::function< void(Entity, T &)> cb)
inline

Registers a callback to be invoked when a component of type T is destroyed.

Template Parameters
TThe component type.
Parameters
cbThe callback function to register.

Definition at line 152 of file Registry.hpp.

◆ remove()

template<typename T>
void vex::Registry::remove ( Entity entity)
inline

Removes a component from an entity.

Template Parameters
TThe component type to remove.
Parameters
entityThe entity to remove the component from.

Definition at line 108 of file Registry.hpp.

◆ try_get()

template<typename T>
T * vex::Registry::try_get ( Entity entity)
inline

Safely retrieves a component from an entity if it exists.

Template Parameters
TThe component type.
Parameters
entityThe entity to get the component from.
Returns
Pointer to the component if it exists, nullptr otherwise.

Definition at line 127 of file Registry.hpp.

◆ try_get_storage_internal()

template<typename T>
ComponentStorage< T > * vex::Registry::try_get_storage_internal ( )
inline

Attempts to retrieve the storage for component type T without creating it.

Template Parameters
TThe component type.
Returns
Pointer to the ComponentStorage if it exists, nullptr otherwise.

Used internally to check if a component type has been stored before.

Definition at line 61 of file Registry.hpp.

Member Data Documentation

◆ m_availableEntities

std::vector<Entity> vex::Registry::m_availableEntities
private

Pool of reusable entity IDs from destroyed entities.

Definition at line 32 of file Registry.hpp.

◆ m_componentStorages

std::vector<std::unique_ptr<IComponentStorage> > vex::Registry::m_componentStorages
private

Array of component storages indexed by component type ID.

Definition at line 36 of file Registry.hpp.

◆ m_nextEntity

Entity vex::Registry::m_nextEntity = 0
private

Counter for generating new entity IDs when the pool is empty.

Definition at line 34 of file Registry.hpp.


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