Nui Engine
A game engine framework
Loading...
Searching...
No Matches
Nui::ECS::Context Class Reference

The ECS context class manages entities, systems, and events in the ECS framework. More...

#include <Context.h>

Inheritance diagram for Nui::ECS::Context:
Nui::World

Public Types

using SubscriberMap = std::unordered_map<TypeIndex, std::vector<Internal::EventSubscriberBase*>, std::hash<TypeIndex>, std::equal_to<TypeIndex>>
 Unordered map of subscribers to array of events subscriber base pointers.
 

Public Member Functions

 Context ()
 Constructs a new ECS context.
 
virtual ~Context ()
 Destroys the ECS context.
 
U64 GetEntityCount () const noexcept
 Gets the total count of entities in the ECS context.
 
EntityGetEntityById (U64 id)
 Retrieves an entity by its unique identifier.
 
EntityGetEntityByIndex (U64 index)
 Retrieves an entity by its index.
 
EntityCreateEntity ()
 Creates a new entity in the ECS context.
 
void DestroyEntity (Entity *e, bool immediate=false)
 Destroys an entity in the ECS context.
 
bool ClearPending ()
 Clears all entities pending destruction in the ECS context.
 
void Reset ()
 Resets the ECS context, clearing all entities but keeping the systems.
 
template<typename T , typename... Args>
requires IsSystem<T>
T * RegisterSystem (Args &&... args)
 Registers a new system in the ECS context.
 
template<typename T >
requires IsSystem<T>
void UnregisterSystem ()
 Unregisters a system from the ECS context.
 
void UnregisterAllSystems ()
 Un-registers all systems from the ECS context.
 
template<typename T >
requires IsSystem<T>
T * GetSystem ()
 Gets a system from the ECS context.
 
template<typename T >
requires IsSystem<T>
void EnableSystem ()
 Enables a system in the ECS context.
 
template<typename T >
requires IsSystem<T>
void DisableSystem ()
 Disables a system in the ECS context.
 
template<typename T >
requires IsSystem<T>
bool IsSystmEnabled ()
 Checks if a system is enabled in the ECS context.
 
template<typename T >
void SubscribeEvent (EventSubscriber< T > *subscriber)
 Subscribes to an event in the ECS context.
 
template<typename T >
void UnsubscribeEvent (EventSubscriber< T > *subscriber)
 Unsubscribes from an event in the ECS context.
 
void UnsubscribeAll (void *subscriber)
 Un-subscribes from all events in the ECS context (usually called in the system destructor)
 
template<typename T >
void EmitEvent (const T &event)
 Emits an event in the ECS context to all subscribers.
 
template<typename... Types>
Internal::EntityComponentView< Types... > Each (bool includePendingDestroy=false)
 Iterates over all entities in the ECS context (used in ranged loops)
 
template<typename... Types>
void Each (typename std::common_type< std::function< void(Entity *, ComponentHandle< Types >...)> >::type viewFunc, bool includePendingDestroy=false)
 Iterates over all entities in the ECS context (using a lambda/function)
 
Internal::EntityView All (bool includePendingDestroy=false)
 Iterates over all entities in the ECS context (used in ranged loops)
 
void All (std::function< void(Entity *)> viewFunc, bool includePendingDestroy=false)
 Iterates over all entities in the ECS context (using a lambda/function)
 
void Tick (const F64 dt)
 Updates the ECS context (updates all the systems)
 

Detailed Description

The ECS context class manages entities, systems, and events in the ECS framework.

Member Function Documentation

◆ All() [1/2]

Internal::EntityView Nui::ECS::Context::All ( bool includePendingDestroy = false)
inline

Iterates over all entities in the ECS context (used in ranged loops)

Parameters
includePendingDestroyFlag indicating whether to include entities pending destruction
Returns
An entity view

◆ All() [2/2]

void Nui::ECS::Context::All ( std::function< void(Entity *)> viewFunc,
bool includePendingDestroy = false )
inline

Iterates over all entities in the ECS context (using a lambda/function)

Parameters
viewFuncFunction to call for each entity
includePendingDestroyFlag indicating whether to include entities pending destruction

◆ ClearPending()

bool Nui::ECS::Context::ClearPending ( )
inline

Clears all entities pending destruction in the ECS context.

Returns
True if any entities were cleared, false otherwise.

◆ CreateEntity()

Entity * Nui::ECS::Context::CreateEntity ( )
inline

Creates a new entity in the ECS context.

Returns
A pointer to the newly created entity.

◆ DestroyEntity()

void Nui::ECS::Context::DestroyEntity ( Entity * e,
bool immediate = false )
inline

Destroys an entity in the ECS context.

Parameters
ePointer to the entity to destroy.
immediateFlag indicating whether to destroy the entity immediately.

◆ DisableSystem()

template<typename T >
requires IsSystem<T>
void Nui::ECS::Context::DisableSystem ( )
inline

Disables a system in the ECS context.

Template Parameters
TClass derived from SystemBase to disable

◆ Each() [1/2]

template<typename ... Types>
Internal::EntityComponentView< Types... > Nui::ECS::Context::Each ( bool includePendingDestroy = false)
inline

Iterates over all entities in the ECS context (used in ranged loops)

Template Parameters
...TypesTypes of components
Parameters
includePendingDestroyFlag indicating whether to include entities pending destruction
Returns
An entity component view

◆ Each() [2/2]

template<typename ... Types>
void Nui::ECS::Context::Each ( typename std::common_type< std::function< void(Entity *, ComponentHandle< Types >...)> ,
::type viewFunc,
bool includePendingDestroy = false )
inline

Iterates over all entities in the ECS context (using a lambda/function)

Template Parameters
...TypesTypes of components
Parameters
viewFuncFunction to call for each entity
includePendingDestroyFlag indicating whether to include entities pending destruction

◆ EmitEvent()

template<typename T >
void Nui::ECS::Context::EmitEvent ( const T & event)
inline

Emits an event in the ECS context to all subscribers.

Template Parameters
TType of event
Parameters
eventEvent data

◆ EnableSystem()

template<typename T >
requires IsSystem<T>
void Nui::ECS::Context::EnableSystem ( )
inline

Enables a system in the ECS context.

Template Parameters
TClass derived from SystemBase to enable

◆ GetEntityById()

Entity * Nui::ECS::Context::GetEntityById ( U64 id)
inline

Retrieves an entity by its unique identifier.

Parameters
idThe unique identifier of the entity.
Returns
A pointer to the entity, or nullptr if not found.

◆ GetEntityByIndex()

Entity * Nui::ECS::Context::GetEntityByIndex ( U64 index)
inline

Retrieves an entity by its index.

Parameters
indexThe index of the entity.
Returns
A pointer to the entity, or nullptr if out of bounds.

◆ GetEntityCount()

U64 Nui::ECS::Context::GetEntityCount ( ) const
inlinenoexcept

Gets the total count of entities in the ECS context.

Returns
The count of entities.

◆ GetSystem()

template<typename T >
requires IsSystem<T>
T * Nui::ECS::Context::GetSystem ( )
inline

Gets a system from the ECS context.

Template Parameters
TClass derived from SystemBase
Returns
A pointer to the system

◆ IsSystmEnabled()

template<typename T >
requires IsSystem<T>
bool Nui::ECS::Context::IsSystmEnabled ( )
inline

Checks if a system is enabled in the ECS context.

Template Parameters
TClass derived from SystemBase to check
Returns
True if the system is enabled, false otherwise

◆ RegisterSystem()

template<typename T , typename... Args>
requires IsSystem<T>
T * Nui::ECS::Context::RegisterSystem ( Args &&... args)
inline

Registers a new system in the ECS context.

Template Parameters
TClass derived from SystemBase to register as a system
...ArgsArguments to pass to the constructor
Parameters
...argsArguments to pass to the constructor
Returns
A pointer to the newly created system

◆ SubscribeEvent()

template<typename T >
void Nui::ECS::Context::SubscribeEvent ( EventSubscriber< T > * subscriber)
inline

Subscribes to an event in the ECS context.

Template Parameters
TType of event to subscribe to
Parameters
subscriberA pointer to the subscriber (usually the system itself)

◆ Tick()

void Nui::ECS::Context::Tick ( const F64 dt)
inline

Updates the ECS context (updates all the systems)

Parameters
dtDelta time

◆ UnregisterSystem()

template<typename T >
requires IsSystem<T>
void Nui::ECS::Context::UnregisterSystem ( )
inline

Unregisters a system from the ECS context.

Template Parameters
TClass derived from SystemBase to unregister

◆ UnsubscribeAll()

void Nui::ECS::Context::UnsubscribeAll ( void * subscriber)
inline

Un-subscribes from all events in the ECS context (usually called in the system destructor)

Parameters
subscriberA pointer to the subscriber (usually the system itself)

◆ UnsubscribeEvent()

template<typename T >
void Nui::ECS::Context::UnsubscribeEvent ( EventSubscriber< T > * subscriber)
inline

Unsubscribes from an event in the ECS context.

Template Parameters
TType of event to unsubscribe from
Parameters
subscriberA pointer to the subscriber (usually the system itself)

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