Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::ecs::World Class Reference

The World class manages entities with their components and systems. More...

#include <world.hpp>

Public Member Functions

 World ()=default
 
 World (const World &)=delete
 
 World (World &&) noexcept=default
 
 ~World ()=default
 
Worldoperator= (const World &)=delete
 
Worldoperator= (World &&) noexcept=default
 
void Update ()
 Flushes buffer of pending operations.
 
void Clear ()
 Clears the world, removing all data.
 
void ClearEntities () noexcept
 Clears all entities and components from the world.
 
void ClearAllEventQueues () noexcept
 Clears all event queues without removing event registration.
 
void MergeEventQueue (details::EventQueue &other)
 Merges events from another event queue into the main queue.
 
template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, std::unique_ptr<Command>>
void MergeCommands (R &&commands)
 Merges commands from a command range into the main queue.
 
Entity CreateEntity ()
 Creates new entity.
 
Entity ReserveEntity ()
 Reserves an entity ID for deferred creation.
 
void DestroyEntity (Entity entity)
 Destroys entity and removes it from the world.
 
void TryDestroyEntity (Entity entity)
 Tries to destroy entity if it exists in the world.
 
template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Entity>
void DestroyEntities (const R &entities)
 Destroys entities and removes them from the world.
 
template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Entity>
void TryDestroyEntities (const R &entities)
 Tries to destroy entities if they exist in the world.
 
template<ComponentTrait T>
void AddComponent (Entity entity, T &&component)
 Adds component to the entity.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void AddComponents (Entity entity, Ts &&... components)
 Adds components to the entity.
 
template<ComponentTrait T>
bool TryAddComponent (Entity entity, T &&component)
 Tries to remove component from the entity.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
auto TryAddComponents (Entity entity, Ts &&... components) -> std::array< bool, sizeof...(Ts)>
 Tries to add components to the entity if they don't exist.
 
template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void EmplaceComponent (Entity entity, Args &&... args)
 Emplaces component for the entity.
 
template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
bool TryEmplaceComponent (Entity entity, Args &&... args)
 Tries to emplace component for the entity.
 
template<ComponentTrait T>
void RemoveComponent (Entity entity)
 Removes component from the entity.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void RemoveComponents (Entity entity)
 Removes components from the entity.
 
template<ComponentTrait T>
bool TryRemoveComponent (Entity entity)
 Tries to removes component from the entity if it exists.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
auto TryRemoveComponents (Entity entity) -> std::array< bool, sizeof...(Ts)>
 Gets component from the entity (const version).
 
void ClearComponents (Entity entity)
 Removes all components from the entity.
 
template<ResourceTrait T>
void InsertResource (T &&resource)
 Inserts a resource into the world.
 
template<ResourceTrait T>
bool TryInsertResource (T &&resource)
 Tries to insert a resource if not present.
 
template<ResourceTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void EmplaceResource (Args &&... args)
 Emplaces a resource in-place.
 
template<ResourceTrait T, typename... Args>
requires std::constructible_from<T, Args...>
bool TryEmplaceResource (Args &&... args)
 Tries to emplace a resource if not present.
 
template<ResourceTrait T>
void RemoveResource ()
 Removes a resource from the world.
 
template<ResourceTrait T>
bool TryRemoveResource ()
 Tries to remove a resource.
 
template<ResourceTrait T>
T & WriteResource ()
 Gets reference to a resource.
 
template<ResourceTrait T>
const T & ReadResource () const
 Gets const reference to a resource.
 
template<ResourceTrait T>
T * TryWriteResource ()
 Tries to get mutable pointer to a resource.
 
template<ResourceTrait T>
const T * TryReadResource () const
 Tries to get const pointer to a resource.
 
template<EventTrait T>
void AddEvent ()
 Registers an event type for use.
 
template<EventTrait... Events>
requires (sizeof...(Events) > 1)
void AddEvents ()
 Registers multiple event types for use.
 
template<EventTrait T>
void ClearEvents ()
 Manually clears events of a specific type from both queues.
 
template<EventTrait T>
auto ReadEvents () const noexcept -> EventReader< T >
 Gets an event reader for type T.
 
template<EventTrait T>
auto WriteEvents () -> EventWriter< T >
 Gets an event writer for type T.
 
bool Exists (Entity entity) const
 Checks if entity exists in the world.
 
template<ComponentTrait T>
bool HasComponent (Entity entity) const
 Checks if entity has component.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
auto HasComponents (Entity entity) const -> std::array< bool, sizeof...(Ts)>
 Checks if entity has components.
 
template<ResourceTrait T>
bool HasResource () const
 Checks if a resource exists.
 
template<ResourceTrait T>
bool HasEvent () const
 Checks if a event registered.
 
template<ResourceTrait T>
bool HasEvents () const
 Checks if events of a specific type exist in event queue.
 
size_t EntityCount () const noexcept
 Gets the number of entities in the world.
 

Friends

template<WorldType WorldT, typename Allocator , ComponentTrait... Components>
class BasicQuery
 
template<WorldType WorldT, typename Allocator , ComponentTrait... Components>
class BasicQueryWithEntity
 

Detailed Description

The World class manages entities with their components and systems.

Note
Partially thread safe. All modifications to the world (adding/removing entities or components) should be done via command buffers to defer changes until the next update.

Definition at line 53 of file world.hpp.

Constructor & Destructor Documentation

◆ World() [1/3]

helios::ecs::World::World ( )
default

◆ World() [2/3]

helios::ecs::World::World ( const World )
delete

◆ World() [3/3]

helios::ecs::World::World ( World &&  )
defaultnoexcept

◆ ~World()

helios::ecs::World::~World ( )
default

Member Function Documentation

◆ AddComponent()

template<ComponentTrait T>
void helios::ecs::World::AddComponent ( Entity  entity,
T &&  component 
)
inline

Adds component to the entity.

If entity already has component of provided type then it will be replaced.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to add
Parameters
entityEntity to add component to
componentComponent to add

Definition at line 757 of file world.hpp.

757 {
758 HELIOS_ASSERT(entity.Valid(), "Failed to add component: Entity is invalid!");
759 HELIOS_ASSERT(Exists(entity), "Failed to add component: World does not own entity with index '{}'!", entity.Index());
760
761 components_.AddComponent(entity, std::forward<T>(component));
763}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140
bool Exists(Entity entity) const
Checks if entity exists in the world.
Definition world.hpp:944
void AddComponent(Entity entity, T &&component)
Adds component to entity (move version).
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ AddComponents()

template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void helios::ecs::World::AddComponents ( Entity  entity,
Ts &&...  components 
)
inline

Adds components to the entity.

If entity already has component of provided type then it will be replaced.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TsComponents types to add
Parameters
entityEntity to add components to
componentsComponents to add

Definition at line 767 of file world.hpp.

767 {
768 HELIOS_ASSERT(entity.Valid(), "Failed to add components: Entity is invalid!");
769 HELIOS_ASSERT(Exists(entity), "Failed to add components: World does not own entity with index '{}'!", entity.Index());
770
771 (components_.AddComponent(entity, std::forward<Ts>(components)), ...);
772 UpdateEntityArchetype(entity);
773}

◆ AddEvent()

template<EventTrait T>
void helios::ecs::World::AddEvent ( )
inline

Registers an event type for use.

Events must be registered before they can be written or read. Built-in events (EntitySpawnedEvent, EntityDestroyedEvent) are registered with is_builtin flag.

Note
Not thread-safe. Should be called during initialization.
Template Parameters
TEvent type
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/sub_app.hpp.

Definition at line 967 of file world.hpp.

967 {
968 if (event_manager_.IsRegistered<T>()) [[unlikely]] {
969 HELIOS_WARN("Event '{}' is already registered in the world!", EventNameOf<T>());
970 return;
971 }
972
973 event_manager_.RegisterEvent<T>();
974}
bool IsRegistered() const noexcept
Checks if an event type is registered.
void RegisterEvent()
Registers an event type for use.
#define HELIOS_WARN(...)
Definition logger.hpp:687

◆ AddEvents()

template<EventTrait... Events>
requires (sizeof...(Events) > 1)
void helios::ecs::World::AddEvents ( )
inline

Registers multiple event types for use.

Note
Not thread-safe. Should be called during initialization.
Template Parameters
EventsEvent types to register
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/sub_app.hpp.

Definition at line 451 of file world.hpp.

451 {
452 (AddEvent<Events>(), ...);
453 }

◆ Clear()

void helios::ecs::World::Clear ( )
inline

Clears the world, removing all data.

Note
Not thread-safe.
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/sub_app.hpp.

Definition at line 645 of file world.hpp.

645 {
646 entities_.Clear();
647 components_.Clear();
648 archetypes_.Clear();
649 resources_.Clear();
650 command_queue_.Clear();
651 event_manager_.Clear();
652}
void Clear() noexcept
Clears all archetypes and entity mappings.
void Clear() noexcept
Clears all pending commands from the queue.
void Clear() noexcept
Clears all component storages.
void Clear() noexcept
Clears all entities.
void Clear() noexcept
Clears all events and registration data.
void Clear()
Clears all resources.

◆ ClearAllEventQueues()

void helios::ecs::World::ClearAllEventQueues ( )
inlinenoexcept

Clears all event queues without removing event registration.

Events can still be written/read after calling this method. To completely reset the event system including registration, use Clear().

Note
Not thread-safe.

Definition at line 88 of file world.hpp.

88{ event_manager_.ClearAllQueues(); }
void ClearAllQueues() noexcept
Clears all event queues without removing registration data.

◆ ClearComponents()

void helios::ecs::World::ClearComponents ( Entity  entity)
inline

Removes all components from the entity.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Parameters
entityEntity to remove all components from

Definition at line 917 of file world.hpp.

917 {
918 HELIOS_ASSERT(entity.Valid(), "Failed to clear components: Entity is invalid!");
919 HELIOS_ASSERT(Exists(entity), "Failed to clear components: World does not own entity with index '{}'!",
920 entity.Index());
921
922 components_.RemoveAllComponents(entity);
923 archetypes_.RemoveEntity(entity);
924}
void RemoveEntity(Entity entity)
Removes entity from its current archetype.
void RemoveAllComponents(Entity entity)
Removes all components from the specified entity.

◆ ClearEntities()

void helios::ecs::World::ClearEntities ( )
inlinenoexcept

Clears all entities and components from the world.

Note
Not thread-safe.

Definition at line 654 of file world.hpp.

654 {
655 entities_.Clear();
656 components_.Clear();
657 archetypes_.Clear();
658}

◆ ClearEvents()

template<EventTrait T>
void helios::ecs::World::ClearEvents ( )
inline

Manually clears events of a specific type from both queues.

Should only be used for manually-managed events (auto_clear = false). Built-in events cannot be manually cleared.

Note
Not thread-safe.
Warning
Triggers assertion if event type is not registered.
Template Parameters
TEvent type

Definition at line 977 of file world.hpp.

977 {
978 HELIOS_ASSERT(event_manager_.IsRegistered<T>(), "Failed to clear events of type '{}': Event type is not registered!",
980
981 event_manager_.ManualClear<T>();
982}
void ManualClear()
Manually clears events of a specific type from current and previous queues.

◆ CreateEntity()

Entity helios::ecs::World::CreateEntity ( )
inline

Creates new entity.

If EntitySpawnedEvent is registered, emits the event automatically.

Note
Not thread-safe.
Returns
Newly created entity.

Definition at line 660 of file world.hpp.

660 {
661 Entity entity = entities_.CreateEntity();
662
663 // Emit EntitySpawnedEvent if registered
664 if (event_manager_.IsRegistered<EntitySpawnedEvent>()) {
665 event_manager_.Write(EntitySpawnedEvent{entity});
666 }
667
668 return entity;
669}
Entity CreateEntity()
Creates a new entity.
void Write(const T &event)
Writes a single event to the current queue.

◆ DestroyEntities()

template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Entity>
void helios::ecs::World::DestroyEntities ( const R entities)
inline

Destroys entities and removes them from the world.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
  • Any entity is invalid.
  • Any entity does not exist in the world.
Template Parameters
RRange type containing Entity elements
Parameters
entitiesEntities to destroy

Definition at line 715 of file world.hpp.

715 {
716 const bool emit_events = event_manager_.IsRegistered<EntityDestroyedEvent>();
717
718 for (const Entity& entity : entities_range) {
719 HELIOS_ASSERT(entity.Valid(), "Failed to destroy entities: Entity is invalid!");
720 HELIOS_ASSERT(Exists(entity), "Failed to destroy entities: World does not own entity with index '{}'!",
721 entity.Index());
722
723 // Emit EntityDestroyedEvent if registered
724 if (emit_events) {
725 event_manager_.Write(EntityDestroyedEvent{entity});
726 }
727
728 components_.RemoveAllComponents(entity);
729 archetypes_.RemoveEntity(entity);
730 }
731 entities_.Destroy(entities_range);
732}
void Destroy(Entity entity)
Destroys an entity by incrementing its generation.

◆ DestroyEntity()

void helios::ecs::World::DestroyEntity ( Entity  entity)
inline

Destroys entity and removes it from the world.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Parameters
entityEntity to destroy

Definition at line 683 of file world.hpp.

683 {
684 HELIOS_ASSERT(entity.Valid(), "Failed to destroy entity: Entity is invalid!");
685 HELIOS_ASSERT(Exists(entity), "Failed to destroy entity: World does not own entity with index '{}'!", entity.Index());
686
687 // Emit EntityDestroyedEvent if registered
688 if (event_manager_.IsRegistered<EntityDestroyedEvent>()) {
689 event_manager_.Write(EntityDestroyedEvent{entity});
690 }
691
692 components_.RemoveAllComponents(entity);
693 archetypes_.RemoveEntity(entity);
694 entities_.Destroy(entity);
695}

◆ EmplaceComponent()

template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void helios::ecs::World::EmplaceComponent ( Entity  entity,
Args &&...  args 
)
inline

Emplaces component for the entity.

Constructs component in-place. If entity already has component of provided type then it will be replaced.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to emplace
ArgsArgument types to forward to T's constructor
Parameters
entityEntity to emplace component to
argsArguments to forward to component constructor

Definition at line 825 of file world.hpp.

825 {
826 HELIOS_ASSERT(entity.Valid(), "Failed to emplace component: Entity is invalid!");
827 HELIOS_ASSERT(Exists(entity), "Failed to emplace component: World does not own entity with index '{}'!",
828 entity.Index());
829
830 components_.EmplaceComponent<T>(entity, std::forward<Args>(args)...);
832}
void EmplaceComponent(Entity entity, Args &&... args)
Constructs component in-place for entity.

◆ EmplaceResource()

template<ResourceTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void helios::ecs::World::EmplaceResource ( Args &&...  args)
inline

Emplaces a resource in-place.

Note
Not thread-safe.
Template Parameters
TResource type
ArgsConstructor argument types
Parameters
argsArguments to forward to resource constructor
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/sub_app.hpp.

Definition at line 352 of file world.hpp.

352 {
353 resources_.Emplace<T>(std::forward<Args>(args)...);
354 }
void Emplace(Args &&... args)
Emplaces a new resource in-place.

◆ EntityCount()

size_t helios::ecs::World::EntityCount ( ) const
inlinenoexcept

Gets the number of entities in the world.

Note
Thread-safe for read operations.
Returns
Number of entities in the world
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 565 of file world.hpp.

565{ return entities_.Count(); }
size_t Count() const noexcept
Gets the current number of living entities.

◆ Exists()

bool helios::ecs::World::Exists ( Entity  entity) const
inline

Checks if entity exists in the world.

Note
Thread-safe for read operations.
Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity to check
Returns
True if entity exists, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 944 of file world.hpp.

944 {
945 HELIOS_ASSERT(entity.Valid(), "Failed to check if entity exists: Entity is invalid!");
946 return entities_.IsValid(entity);
947}
bool IsValid(Entity entity) const noexcept
Checks if an entity exists and is valid.

◆ HasComponent()

template<ComponentTrait T>
bool helios::ecs::World::HasComponent ( Entity  entity) const
inline

Checks if entity has component.

Note
Thread-safe for read operations.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to check
Parameters
entityEntity to check
Returns
Array of bools indicating whether entity has each component
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 950 of file world.hpp.

950 {
951 HELIOS_ASSERT(entity.Valid(), "Failed to check if entity has component: Entity is invalid!");
952 HELIOS_ASSERT(Exists(entity), "Failed to check if entity has component: World does not own entity with index '{}'!",
953 entity.Index());
954 return components_.HasComponent<T>(entity);
955}
bool HasComponent(Entity entity) const
Checks if entity has the specified component type.

◆ HasComponents()

template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
auto helios::ecs::World::HasComponents ( Entity  entity) const -> std::array<bool, sizeof...(Ts)>
inline

Checks if entity has components.

Note
Thread-safe for read operations.
Warning
Triggers assertion if entity is invalid.
Template Parameters
TsComponents types
Parameters
entityEntity to check
Returns
Array of bools indicating whether entity has each component (true if entity has component, false otherwise)

Definition at line 959 of file world.hpp.

959 {
960 HELIOS_ASSERT(entity.Valid(), "Failed to check if entity has components: Entity is invalid!");
961 HELIOS_ASSERT(Exists(entity), "Failed to check if entity has components: World does not own entity with index '{}'!",
962 entity.Index());
963 return {components_.HasComponent<Ts>(entity)...};
964}

◆ HasEvent()

template<ResourceTrait T>
bool helios::ecs::World::HasEvent ( ) const
inline

Checks if a event registered.

Note
Thread-safe for read operations.
Template Parameters
TEvent type
Returns
True if event exists, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/sub_app.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 545 of file world.hpp.

545 {
546 return event_manager_.IsRegistered<T>();
547 }

◆ HasEvents()

template<ResourceTrait T>
bool helios::ecs::World::HasEvents ( ) const
inline

Checks if events of a specific type exist in event queue.

Note
Thread-safe for read operations.
Template Parameters
TEvent type
Returns
True if event exists, false otherwise

Definition at line 556 of file world.hpp.

556 {
557 return event_manager_.HasEvents<T>();
558 }
bool HasEvents() const
Checks if events of a specific type exist in either queue.

◆ HasResource()

template<ResourceTrait T>
bool helios::ecs::World::HasResource ( ) const
inline

Checks if a resource exists.

Note
Thread-safe for read operations.
Template Parameters
TResource type
Returns
True if resource exists, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/sub_app.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 534 of file world.hpp.

534 {
535 return resources_.Has<T>();
536 }
bool Has() const
Checks if a resource exists.

◆ InsertResource()

template<ResourceTrait T>
void helios::ecs::World::InsertResource ( T &&  resource)
inline

Inserts a resource into the world.

Replaces existing resource if present.

Note
Not thread-safe.
Template Parameters
TResource type
Parameters
resourceResource to insert
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/sub_app.hpp.

Definition at line 327 of file world.hpp.

327 {
328 resources_.Insert(std::forward<T>(resource));
329 }
void Insert(T &&resource)
Inserts a new resource.

◆ MergeCommands()

template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, std::unique_ptr<Command>>
void helios::ecs::World::MergeCommands ( R &&  commands)
inline

Merges commands from a command range into the main queue.

Note
Not thread-safe.
Template Parameters
RRange type containing unique_ptr<Command> elements
Parameters
commandsRange of commands to merge from

Definition at line 673 of file world.hpp.

673 {
674 if (commands.empty()) [[unlikely]] {
675 return;
676 }
677
678 // Enqueue all commands from the range to the command queue
679 command_queue_.EnqueueBulk(std::forward<R>(commands));
680 commands.clear();
681}
void EnqueueBulk(R &&commands)
Enqueues multiple commands in bulk.

◆ MergeEventQueue()

void helios::ecs::World::MergeEventQueue ( details::EventQueue other)
inline

Merges events from another event queue into the main queue.

Note
Not thread-safe.
Parameters
otherEvent queue to merge from

Definition at line 95 of file world.hpp.

95{ event_manager_.Merge(other); }
void Merge(EventQueue &other)
Merges events from another EventQueue into the current queue.

◆ operator=() [1/2]

World & helios::ecs::World::operator= ( const World )
delete

◆ operator=() [2/2]

World & helios::ecs::World::operator= ( World &&  )
defaultnoexcept

◆ ReadEvents()

template<EventTrait T>
auto helios::ecs::World::ReadEvents ( ) const -> EventReader<T>
inlinenoexcept

Gets an event reader for type T.

Provides a type-safe, ergonomic API for reading events with support for iteration, filtering, and searching. Event must be registered via AddEvent<T>() first.

Note
Thread-safe for read operations.
Warning
Triggers assertion if event type is not registered.
Template Parameters
TEvent type
Returns
EventReader for type T

Definition at line 985 of file world.hpp.

985 {
986 HELIOS_ASSERT(event_manager_.IsRegistered<T>(),
987 "Failed to get event reader for type '{}': Event type is not registered!", EventNameOf<T>());
988
989 return EventReader<T>(event_manager_);
990}

◆ ReadResource()

template<ResourceTrait T>
const T & helios::ecs::World::ReadResource ( ) const
inline

Gets const reference to a resource.

Note
Thread-safe for read operations.
Warning
Triggers assertion if resource doesn't exist.
Template Parameters
TResource type
Returns
Const reference to resource
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 939 of file world.hpp.

939 {
940 HELIOS_ASSERT(HasResource<T>(), "Failed to read resource '{}': Resource does not exist!", ResourceNameOf<T>());
941 return resources_.Get<T>();
942}
T & Get()
Gets mutable reference to a resource. Triggers assertion if resource doesn't exist.

◆ RemoveComponent()

template<ComponentTrait T>
void helios::ecs::World::RemoveComponent ( Entity  entity)
inline

Removes component from the entity.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to remove
Parameters
entityEntity to remove component from

Definition at line 851 of file world.hpp.

851 {
852 HELIOS_ASSERT(entity.Valid(), "Failed to remove component: Entity is invalid!");
853 HELIOS_ASSERT(Exists(entity), "Failed to remove component: World does not own entity with index '{}'!",
854 entity.Index());
855
856 components_.RemoveComponent<T>(entity);
858}
void RemoveComponent(Entity entity)
Removes component from entity.

◆ RemoveComponents()

template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void helios::ecs::World::RemoveComponents ( Entity  entity)
inline

Removes components from the entity.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TsComponents types to remove
Parameters
entityEntity to remove components from

Definition at line 862 of file world.hpp.

862 {
863 HELIOS_ASSERT(entity.Valid(), "Failed to remove components: Entity is invalid!");
864 HELIOS_ASSERT(Exists(entity), "Failed to remove components: World does not own entity with index '{}'!",
865 entity.Index());
866
867 (components_.RemoveComponent<Ts>(entity), ...);
868 UpdateEntityArchetype(entity);
869}

◆ RemoveResource()

template<ResourceTrait T>
void helios::ecs::World::RemoveResource ( )
inline

Removes a resource from the world.

Note
Not thread-safe.
Warning
Triggers assertion if resource does not exist.
Template Parameters
TResource type

Definition at line 927 of file world.hpp.

927 {
928 HELIOS_ASSERT(HasResource<T>(), "Failed to remove resource '{}': Resource does not exist!", ResourceNameOf<T>());
929 resources_.Remove<T>();
930}
void Remove()
Removes a resource.

◆ ReserveEntity()

Entity helios::ecs::World::ReserveEntity ( )
inline

Reserves an entity ID for deferred creation.

The actual entity creation is deferred until Update() is called.

Note
Thread-safe.
Returns
Reserved entity ID
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 121 of file world.hpp.

121{ return entities_.ReserveEntity(); }
Entity ReserveEntity()
Reserves an entity ID that can be used immediately.

◆ TryAddComponent()

template<ComponentTrait T>
bool helios::ecs::World::TryAddComponent ( Entity  entity,
T &&  component 
)
inline

Tries to remove component from the entity.

Returns false if component is not on the entity.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to remove
Parameters
entityEntity to remove component from
Returns
True if component was removed, false otherwise

Definition at line 776 of file world.hpp.

776 {
777 HELIOS_ASSERT(entity.Valid(), "Failed to try add component: Entity is invalid!");
778 HELIOS_ASSERT(Exists(entity), "Failed to try add component: World does not own entity with index '{}'!",
779 entity.Index());
780
781 using ComponentType = std::decay_t<T>;
782 if (components_.HasComponent<ComponentType>(entity)) {
783 return false;
784 }
785
786 components_.AddComponent(entity, std::forward<T>(component));
788 return true;
789}

◆ TryAddComponents()

template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
auto helios::ecs::World::TryAddComponents ( Entity  entity,
Ts &&...  components 
) -> std::array<bool, sizeof...(Ts)>
inline

Tries to add components to the entity if they don't exist.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TsComponents types to add
Parameters
entityEntity to add components to
componentsComponents to add
Returns
Array of bools indicating whether each component was added (true if added, false otherwise)

Definition at line 793 of file world.hpp.

793 {
794 HELIOS_ASSERT(entity.Valid(), "Failed to try add components: Entity is invalid!");
795 HELIOS_ASSERT(Exists(entity), "Failed to try add components: World does not own entity with index '{}'!",
796 entity.Index());
797
798 std::array<bool, sizeof...(Ts)> result = {!components_.HasComponent<std::decay_t<Ts>>(entity)...};
799 bool any_added = false;
800
801 size_t idx = 0;
802 auto add_if_missing = [this, &result, &any_added, &idx, entity](auto&& component) {
803 using ComponentType = std::decay_t<decltype(component)>;
804 if (!components_.HasComponent<ComponentType>(entity)) {
805 components_.AddComponent(entity, std::forward<decltype(component)>(component));
806 result[idx] = true;
807 any_added = true;
808 } else {
809 result[idx] = false;
810 }
811 ++idx;
812 };
813
814 (add_if_missing(std::forward<Ts>(components)), ...);
815
816 if (any_added) {
817 UpdateEntityArchetype(entity);
818 }
819
820 return result;
821}

◆ TryDestroyEntities()

template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Entity>
void helios::ecs::World::TryDestroyEntities ( const R entities)
inline

Tries to destroy entities if they exist in the world.

Note
Not thread-safe.
Warning
Triggers assertion only if any entity is invalid (non-existing entities are skipped).
Template Parameters
RRange type containing Entity elements
Parameters
entitiesEntities to destroy

Definition at line 736 of file world.hpp.

736 {
737 const bool emit_events = event_manager_.IsRegistered<EntityDestroyedEvent>();
738
739 for (const Entity& entity : entities_range) {
740 HELIOS_ASSERT(entity.Valid(), "Failed to try destroy entities: Entity is invalid!");
741 if (!Exists(entity)) [[unlikely]] {
742 continue;
743 }
744
745 // Emit EntityDestroyedEvent if registered
746 if (emit_events) {
747 event_manager_.Write(EntityDestroyedEvent{entity});
748 }
749
750 components_.RemoveAllComponents(entity);
751 archetypes_.RemoveEntity(entity);
752 }
753 entities_.Destroy(entities_range);
754}

◆ TryDestroyEntity()

void helios::ecs::World::TryDestroyEntity ( Entity  entity)
inline

Tries to destroy entity if it exists in the world.

Note
Not thread-safe.
Warning
Triggers assertion only if entity is invalid (does not assert when entity does not exist in the world).
Parameters
entityEntity to destroy

Definition at line 697 of file world.hpp.

697 {
698 HELIOS_ASSERT(entity.Valid(), "Failed to try destroy entity: Entity is invalid!");
699 if (!Exists(entity)) {
700 return;
701 }
702
703 // Emit EntityDestroyedEvent if registered
704 if (event_manager_.IsRegistered<EntityDestroyedEvent>()) {
705 event_manager_.Write(EntityDestroyedEvent{entity});
706 }
707
708 components_.RemoveAllComponents(entity);
709 archetypes_.RemoveEntity(entity);
710 entities_.Destroy(entity);
711}

◆ TryEmplaceComponent()

template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
bool helios::ecs::World::TryEmplaceComponent ( Entity  entity,
Args &&...  args 
)
inline

Tries to emplace component for the entity.

Constructs component in-place. Returns false if component is already on the entity.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to emplace
ArgsArgument types to forward to T's constructor
Parameters
entityEntity to emplace component to
argsArguments to forward to component constructor
Returns
True if component was emplaced, false otherwise

Definition at line 836 of file world.hpp.

836 {
837 HELIOS_ASSERT(entity.Valid(), "Failed to try emplace component: Entity is invalid!");
838 HELIOS_ASSERT(Exists(entity), "Failed to try emplace component: World does not own entity with index '{}'!",
839 entity.Index());
840
841 if (components_.HasComponent<T>(entity)) {
842 return false;
843 }
844
845 components_.EmplaceComponent<T>(entity, std::forward<Args>(args)...);
847 return true;
848}

◆ TryEmplaceResource()

template<ResourceTrait T, typename... Args>
requires std::constructible_from<T, Args...>
bool helios::ecs::World::TryEmplaceResource ( Args &&...  args)
inline

Tries to emplace a resource if not present.

Note
Not thread-safe.
Template Parameters
TResource type
ArgsConstructor argument types
Parameters
argsArguments to forward to resource constructor
Returns
True if emplaced, false if resource already exists

Definition at line 366 of file world.hpp.

366 {
367 return resources_.TryEmplace<T>(std::forward<Args>(args)...);
368 }
bool TryEmplace(Args &&... args)
Tries to emplace a resource if not present.

◆ TryInsertResource()

template<ResourceTrait T>
bool helios::ecs::World::TryInsertResource ( T &&  resource)
inline

Tries to insert a resource if not present.

Note
Not thread-safe.
Template Parameters
TResource type
Parameters
resourceResource to insert
Returns
True if inserted, false if resource already exists

Definition at line 339 of file world.hpp.

339 {
340 return resources_.TryInsert(std::forward<T>(resource));
341 }
bool TryInsert(T &&resource)
Tries to insert a resource if not present.

◆ TryReadResource()

template<ResourceTrait T>
const T * helios::ecs::World::TryReadResource ( ) const
inline

Tries to get const pointer to a resource.

Note
Thread-safe for read operations.
Template Parameters
TResource type
Returns
Const pointer to resource, or nullptr if not found
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 428 of file world.hpp.

428 {
429 return resources_.TryGet<T>();
430 }
T * TryGet()
Tries to get mutable pointer to a resource.

◆ TryRemoveComponent()

template<ComponentTrait T>
bool helios::ecs::World::TryRemoveComponent ( Entity  entity)
inline

Tries to removes component from the entity if it exists.

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to remove
Parameters
entityEntity to remove component from
Returns
True if component will be removed, false otherwise

Definition at line 872 of file world.hpp.

872 {
873 HELIOS_ASSERT(entity.Valid(), "Failed to try remove component: Entity is invalid!");
874 HELIOS_ASSERT(Exists(entity), "Failed to try remove component: World does not own entity with index '{}'!",
875 entity.Index());
876
877 const bool had_component = components_.HasComponent<T>(entity);
878 if (had_component) {
879 components_.RemoveComponent<T>(entity);
881 }
882 return had_component;
883}

◆ TryRemoveComponents()

template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
auto helios::ecs::World::TryRemoveComponents ( Entity  entity) -> std::array<bool, sizeof...(Ts)>
inline

Gets component from the entity (const version).

Note
Not thread-safe.
Warning
Triggers assertion in next cases:
Template Parameters
TComponent type to get
Parameters
entityEntity to get component from
Returns
Const reference to component

Definition at line 887 of file world.hpp.

887 {
888 HELIOS_ASSERT(entity.Valid(), "Failed to try remove components: Entity is invalid!");
889 HELIOS_ASSERT(Exists(entity), "Failed to try remove components: World does not own entity with index '{}'!",
890 entity.Index());
891
892 std::array<bool, sizeof...(Ts)> result = {};
893 bool any_removed = false;
894
895 size_t index = 0;
896 auto try_remove_single = [this, &index, &result, &any_removed, entity](auto type_identity_tag) {
897 using ComponentType = typename decltype(type_identity_tag)::type;
898 if (components_.HasComponent<ComponentType>(entity)) {
899 components_.RemoveComponent<ComponentType>(entity);
900 result[index] = true;
901 any_removed = true;
902 } else {
903 result[index] = false;
904 }
905 ++index;
906 };
907
908 (try_remove_single(std::type_identity<Ts>{}), ...);
909
910 if (any_removed) {
911 UpdateEntityArchetype(entity);
912 }
913
914 return result;
915}

◆ TryRemoveResource()

template<ResourceTrait T>
bool helios::ecs::World::TryRemoveResource ( )
inline

Tries to remove a resource.

Note
Not thread-safe.
Template Parameters
TResource type
Returns
True if removed, false if resource didn't exist

Definition at line 386 of file world.hpp.

386 {
387 return resources_.TryRemove<T>();
388 }
bool TryRemove()
Tries to remove a resource.

◆ TryWriteResource()

template<ResourceTrait T>
T * helios::ecs::World::TryWriteResource ( )
inline

Tries to get mutable pointer to a resource.

Note
Thread-safe for read operations.
Template Parameters
TResource type
Returns
Pointer to resource, or nullptr if not found
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 417 of file world.hpp.

417 {
418 return resources_.TryGet<T>();
419 }

◆ Update()

void helios::ecs::World::Update ( )
inline

Flushes buffer of pending operations.

Note
This method should be called once per frame after all systems have been updated. Not thread-safe.

Definition at line 627 of file world.hpp.

627 {
628 // First, flush reserved entities to ensure all reserved IDs are created.
629 entities_.FlushReservedEntities();
630
631 // Then, execute all commands in the command queue.
632 if (!command_queue_.Empty()) [[likely]] {
633 auto commands = command_queue_.DequeueAll();
634 for (auto& command : commands) {
635 if (command) [[likely]] {
636 command->Execute(*this);
637 }
638 }
639 }
640
641 // Update event lifecycle - swap buffers and clear old events
642 event_manager_.Update();
643}
bool Empty() const noexcept
Checks if the queue is empty.
auto DequeueAll() noexcept -> std::vector< std::unique_ptr< Command > >
Gets all commands and clears the queue.
void FlushReservedEntities()
Creates reserved entities in the metadata.
void Update()
Updates event lifecycle - swaps buffers and clears old events.

◆ WriteEvents()

template<EventTrait T>
auto helios::ecs::World::WriteEvents ( ) -> EventWriter<T>
inline

Gets an event writer for type T.

Provides a type-safe, ergonomic API for writing events with support for bulk operations and in-place construction. Event must be registered via AddEvent<T>() first.

Note
Not thread-safe.
Warning
Triggers assertion if event type is not registered.
Template Parameters
TEvent type
Returns
EventWriter for type T

Definition at line 993 of file world.hpp.

993 {
994 HELIOS_ASSERT(event_manager_.IsRegistered<T>(),
995 "Failed to get event writer for type '{}': Event type is not registered!", EventNameOf<T>());
996
997 return EventWriter<T>(event_manager_);
998}

◆ WriteResource()

template<ResourceTrait T>
T & helios::ecs::World::WriteResource ( )
inline

Gets reference to a resource.

Note
Not thread-safe.
Warning
Triggers assertion if resource does not exist.
Template Parameters
TResource type
Returns
Reference to resource
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 933 of file world.hpp.

933 {
934 HELIOS_ASSERT(HasResource<T>(), "Failed to write resource '{}': Resource does not exist!", ResourceNameOf<T>());
935 return resources_.Get<T>();
936}

Friends And Related Symbol Documentation

◆ BasicQuery

template<WorldType WorldT, typename Allocator , ComponentTrait... Components>
friend class BasicQuery
friend

Definition at line 620 of file world.hpp.

◆ BasicQueryWithEntity

template<WorldType WorldT, typename Allocator , ComponentTrait... Components>
friend class BasicQueryWithEntity
friend

Definition at line 624 of file world.hpp.