vecs
Fast, flexible ecs in C++ with ergonomic API
Loading...
Searching...
No Matches
vecs::Observer< Components > Class Template Reference

Observer class for tracking component changes within a system. More...

#include <vecs.h>

Inheritance diagram for vecs::Observer< Components >:
vecs::TypeErasedObserver

Public Member Functions

 Observer ()
 Constructs an Observer for the specified components.
 
 Observer (const Observer &)=delete
 
Observeroperator= (const Observer &)=delete
 
 Observer (Observer &&)=delete
 
Observeroperator= (Observer &&)=delete
 
const std::vector< Entity > & added () const override
 Retrieves the entities that have been added for the first time with the observed components.
 
const std::vector< Entity > & inserted () const override
 Retrieves the entities that have been inserted with the observed components.
 
const std::vector< Entity > & removed () const override
 Retrieves the entities that have been had the observed components removed.
 
- Public Member Functions inherited from vecs::TypeErasedObserver

Detailed Description

template<typename... Components>
class vecs::Observer< Components >

Observer class for tracking component changes within a system.

The Observer class enables systems to monitor specific components, tracking entities as they are added with the specified components.

Template Parameters
ComponentsThe types of components to be observed.

Example usage:

world.add_system([](const Observer<int>& observer, Query<int>& query) {
for (const auto& e : observer.inserted()) {
auto i = query.get(e);
if (i) {
printf("Entity '%zu' had int with value inserted = %i\n", e, std::get<0>(*i));
}
}
});
world.spawn().insert(3);
world.spawn().insert(2);
world.spawn().insert(1);
Observer class for tracking component changes within a system.
Definition vecs.h:261
const std::vector< Entity > & inserted() const override
Retrieves the entities that have been inserted with the observed components.
Definition vecs.h:295
Represents a query for components within the ECS world.
Definition vecs.h:1270
Record get(Entity entity)
Retrieves the components of an entity matching the query.
Definition vecs.h:1556

Constructor & Destructor Documentation

◆ Observer()

template<typename... Components>
vecs::Observer< Components >::Observer ( )
inline

Constructs an Observer for the specified components.

Initializes the observer to monitor entities with the specified components.

Member Function Documentation

◆ added()

template<typename... Components>
const std::vector< Entity > & vecs::Observer< Components >::added ( ) const
inlineoverridevirtual

Retrieves the entities that have been added for the first time with the observed components.

Returns a reference to a vector of entities that were added with the specified components. If no entities were added, an empty vector is returned.

Returns
A reference to a vector of added entities.

Implements vecs::TypeErasedObserver.

◆ inserted()

template<typename... Components>
const std::vector< Entity > & vecs::Observer< Components >::inserted ( ) const
inlineoverridevirtual

Retrieves the entities that have been inserted with the observed components.

Returns a reference to a vector of entities that were inserted with the specified components. If no entities were inserted, an empty vector is returned.

Returns
A reference to a vector of inserted entities.

Implements vecs::TypeErasedObserver.

◆ removed()

template<typename... Components>
const std::vector< Entity > & vecs::Observer< Components >::removed ( ) const
inlineoverridevirtual

Retrieves the entities that have been had the observed components removed.

Returns a reference to a vector of entities that have had the specified components removed. If no components were inserted, an empty vector is returned.

Returns
A reference to a vector of entities with removed components.

Implements vecs::TypeErasedObserver.


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