Engine
Raylib based game framework
Loading...
Searching...
No Matches
Registry Class Reference

Wraps entt::registry with a safer, callback-aware API. More...

#include <Registry.hpp>

Public Member Functions

Entity CreateEntity ()
 Creates a new entity.
void DestroyEntity (const Entity entity)
 Destroys an entity if it is valid.
bool EntityValid (const Entity entity)
 Checks whether an entity is alive in the registry.
template<typename Component, typename... Args>
const Component * Emplace (const Entity entity, Args &&... args)
 Adds a component to an entity.
template<typename Component, typename... Args>
const Component & EmplaceOrReplace (const Entity entity, Args &&... args)
 Adds or replaces a component on an entity.
template<typename Component>
bool Patch (const Entity entity, const std::function< void(Component &)> &func)
 Applies a function to an existing component in place.
template<typename Component>
const Component * Get (const Entity entity)
 Gets a pointer to a component on an entity.
template<typename... Components>
bool HasAny (const Entity entity)
 Checks whether an entity has at least one of the given components.
template<typename... Components>
bool HasAll (const Entity entity)
 Checks whether an entity has all of the given components.
template<typename Component, typename... Args>
const Component * Replace (const Entity entity, Args &&... args)
 Replaces an existing component on an entity.
template<typename Component>
void Remove (const Entity entity)
 Removes a component from an entity.
template<typename Component>
void ClearComponents ()
 Removes all instances of a component type from every entity.
template<typename... Components>
auto GetView ()
 Returns a view over all entities that have the given components.
template<typename... Owned, typename... Get, typename... Exclude>
auto GetGroup (entt::get_t< Get... > get=entt::get_t{}, entt::exclude_t< Exclude... > exclude=entt::exclude_t{})
 Returns a group over entities with the specified owned and non-owned components.
template<typename Component>
void Sort (const std::function< bool(const Component &a, const Component &b)> &comparitor)
 Sorts a component pool using a comparator.
template<typename... Owned, typename... Get, typename... Exclude>
void SortGroup (const std::function< bool(const Entity &a, const Entity &b)> &comparitor, entt::get_t< Get... > get=entt::get_t{}, entt::exclude_t< Exclude... > exclude=entt::exclude_t{})
 Sorts a group's entities using an entity comparator.
template<typename Component>
u32 OnConstruct (const std::function< void(Component &component, const Entity entity)> &callback)
 Registers a callback invoked when a component of the given type is constructed.
template<typename Component>
u32 OnUpdate (const std::function< void(Component &component, const Entity entity)> &callback)
 Registers a callback invoked when a component of the given type is updated.
template<typename Component>
u32 OnDestroy (const std::function< void(Component &component, const Entity entity)> &callback)
 Registers a callback invoked just before a component of the given type is destroyed.
template<typename Component>
void RemoveConstructCallback (const u32 callbackId)
 Removes a previously registered construction callback.
template<typename Component>
void RemoveUpdateCallback (const u32 callbackId)
 Removes a previously registered update callback.
template<typename Component>
void RemoveDestroyCallback (const u32 callbackId)
 Removes a previously registered destruction callback.
entt::registry & GetRegistry ()
 Returns a reference to the underlying entt registry.

Detailed Description

Wraps entt::registry with a safer, callback-aware API.

Provides typed component operations and supports registering callbacks for component construction, update, and destruction events. All callbacks are identified by a u32 ID so they can be individually removed.

Member Function Documentation

◆ ClearComponents()

template<typename Component>
void Registry::ClearComponents ( )
inline

Removes all instances of a component type from every entity.

Template Parameters
ComponentComponent type to clear

◆ CreateEntity()

Entity Registry::CreateEntity ( )

Creates a new entity.

Returns
The newly created entity

◆ DestroyEntity()

void Registry::DestroyEntity ( const Entity entity)

Destroys an entity if it is valid.

Does nothing if the entity is not valid.

Parameters
entityEntity to destroy

◆ Emplace()

template<typename Component, typename... Args>
const Component * Registry::Emplace ( const Entity entity,
Args &&... args )
inline

Adds a component to an entity.

Does nothing and returns nullptr if the entity already has this component.

Template Parameters
ComponentComponent type to add
ArgsComponent constructor argument types
Parameters
entityTarget entity
argsComponent constructor arguments
Returns
Pointer to the new component, or nullptr if it already existed

◆ EmplaceOrReplace()

template<typename Component, typename... Args>
const Component & Registry::EmplaceOrReplace ( const Entity entity,
Args &&... args )
inline

Adds or replaces a component on an entity.

If the component already exists it is replaced in place; otherwise it is created.

Template Parameters
ComponentComponent type
ArgsComponent constructor argument types
Parameters
entityTarget entity
argsComponent constructor arguments
Returns
Reference to the component

◆ EntityValid()

bool Registry::EntityValid ( const Entity entity)

Checks whether an entity is alive in the registry.

Parameters
entityEntity to check
Returns
True if the entity is valid

◆ Get()

template<typename Component>
const Component * Registry::Get ( const Entity entity)
inline

Gets a pointer to a component on an entity.

Template Parameters
ComponentComponent type
Parameters
entityTarget entity
Returns
Pointer to the component, or nullptr if the entity does not have it

◆ GetGroup()

template<typename... Owned, typename... Get, typename... Exclude>
auto Registry::GetGroup ( entt::get_t< Get... > get = entt::get_t{},
entt::exclude_t< Exclude... > exclude = entt::exclude_t{} )
inline

Returns a group over entities with the specified owned and non-owned components.

Owned components are stored contiguously for cache-friendly iteration. Non-owned components are fetched via Get. Excluded components filter entities out.

Template Parameters
OwnedComponent types owned (and sorted) by the group
GetAdditional component types to fetch but not own
ExcludeComponent types that disqualify an entity from the group
Parameters
getentt::get_t list of non-owned components to include
excludeentt::exclude_t list of components to exclude
Returns
entt group over the matching entities

◆ GetRegistry()

entt::registry & Registry::GetRegistry ( )

Returns a reference to the underlying entt registry.

Prefer the typed API above. This escape hatch exists for interop with entt APIs not yet wrapped by Registry.

Returns
Reference to the raw entt::registry

◆ GetView()

template<typename... Components>
auto Registry::GetView ( )
inline

Returns a view over all entities that have the given components.

Components are exposed as const references inside the view.

Template Parameters
ComponentsComponent types to include in the view
Returns
entt view over the matching entities

◆ HasAll()

template<typename... Components>
bool Registry::HasAll ( const Entity entity)
inline

Checks whether an entity has all of the given components.

Template Parameters
ComponentsComponent types to check
Parameters
entityTarget entity
Returns
True if the entity has every specified component

◆ HasAny()

template<typename... Components>
bool Registry::HasAny ( const Entity entity)
inline

Checks whether an entity has at least one of the given components.

Template Parameters
ComponentsComponent types to check
Parameters
entityTarget entity
Returns
True if the entity has any of the specified components

◆ OnConstruct()

template<typename Component>
u32 Registry::OnConstruct ( const std::function< void(Component &component, const Entity entity)> & callback)
inline

Registers a callback invoked when a component of the given type is constructed.

Multiple callbacks can be registered for the same component type. The returned ID can be used with RemoveConstructCallback to unregister.

Template Parameters
ComponentComponent type to observe
Parameters
callbackFunction receiving the new component and the owning entity
Returns
Callback ID

◆ OnDestroy()

template<typename Component>
u32 Registry::OnDestroy ( const std::function< void(Component &component, const Entity entity)> & callback)
inline

Registers a callback invoked just before a component of the given type is destroyed.

Multiple callbacks can be registered for the same component type. The returned ID can be used with RemoveDestroyCallback.

Template Parameters
ComponentComponent type to observe
Parameters
callbackFunction receiving the component about to be destroyed and its entity
Returns
Callback ID

◆ OnUpdate()

template<typename Component>
u32 Registry::OnUpdate ( const std::function< void(Component &component, const Entity entity)> & callback)
inline

Registers a callback invoked when a component of the given type is updated.

Triggered by Patch and Replace. Multiple callbacks can be registered for the same component type. The returned ID can be used with RemoveUpdateCallback.

Template Parameters
ComponentComponent type to observe
Parameters
callbackFunction receiving the updated component and the owning entity
Returns
Callback ID

◆ Patch()

template<typename Component>
bool Registry::Patch ( const Entity entity,
const std::function< void(Component &)> & func )
inline

Applies a function to an existing component in place.

Triggers update callbacks. Does nothing and returns false if the entity does not have the component.

Template Parameters
ComponentComponent type
Parameters
entityTarget entity
funcFunction to apply to the component
Returns
True if the component existed and was patched

◆ Remove()

template<typename Component>
void Registry::Remove ( const Entity entity)
inline

Removes a component from an entity.

Does nothing if the entity does not have the component.

Template Parameters
ComponentComponent type to remove
Parameters
entityTarget entity

◆ RemoveConstructCallback()

template<typename Component>
void Registry::RemoveConstructCallback ( const u32 callbackId)
inline

Removes a previously registered construction callback.

Disconnects the underlying signal if no more callbacks remain for this type.

Template Parameters
ComponentComponent type the callback was registered for
Parameters
callbackIdID returned by OnConstruct

◆ RemoveDestroyCallback()

template<typename Component>
void Registry::RemoveDestroyCallback ( const u32 callbackId)
inline

Removes a previously registered destruction callback.

Disconnects the underlying signal if no more callbacks remain for this type.

Template Parameters
ComponentComponent type the callback was registered for
Parameters
callbackIdID returned by OnDestroy

◆ RemoveUpdateCallback()

template<typename Component>
void Registry::RemoveUpdateCallback ( const u32 callbackId)
inline

Removes a previously registered update callback.

Disconnects the underlying signal if no more callbacks remain for this type.

Template Parameters
ComponentComponent type the callback was registered for
Parameters
callbackIdID returned by OnUpdate

◆ Replace()

template<typename Component, typename... Args>
const Component * Registry::Replace ( const Entity entity,
Args &&... args )
inline

Replaces an existing component on an entity.

Does nothing and returns nullptr if the entity does not have the component.

Template Parameters
ComponentComponent type
ArgsComponent constructor argument types
Parameters
entityTarget entity
argsComponent constructor arguments
Returns
Pointer to the replaced component, or nullptr if it did not exist

◆ Sort()

template<typename Component>
void Registry::Sort ( const std::function< bool(const Component &a, const Component &b)> & comparitor)
inline

Sorts a component pool using a comparator.

The sort order affects the iteration order of views over this component.

Template Parameters
ComponentComponent type whose pool is sorted
Parameters
comparitorComparator receiving two const component references

◆ SortGroup()

template<typename... Owned, typename... Get, typename... Exclude>
void Registry::SortGroup ( const std::function< bool(const Entity &a, const Entity &b)> & comparitor,
entt::get_t< Get... > get = entt::get_t{},
entt::exclude_t< Exclude... > exclude = entt::exclude_t{} )
inline

Sorts a group's entities using an entity comparator.

Affects the iteration order of the group. The group is identified by the same owned/get/exclude template parameters used to create it.

Template Parameters
OwnedOwned component types of the group
GetNon-owned component types of the group
ExcludeExcluded component types of the group
Parameters
comparitorComparator receiving two entity references
getentt::get_t matching the group's get list
excludeentt::exclude_t matching the group's exclude list

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