Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::ecs::View< Components > Class Template Reference

View for iterating entities with multiple required components. More...

#include <View.hpp>

Public Types

using Iterator = ViewIterator<Components...>
 Iterator type.
using PoolsTuple = std::tuple<SparseSet<Components>*...>
 Tuple type for pool pointers.

Public Member Functions

 View (PoolsTuple pools)
 Constructs a view from component pools.
Iterator begin () const
 Returns iterator to the first matching entity.
Iterator end () const
 Returns iterator past the last entity.
template<typename T>
T & get (Entity entity)
 Gets a specific component from an entity.
template<typename T>
const T & get (Entity entity) const
 Gets a specific component from an entity.
std::tuple< Components &... > getAll (Entity entity)
 Gets all components for an entity as a tuple.
bool empty () const
 Checks if the view is empty.
usize sizeHint () const
 Gets the estimated number of matching entities.
template<typename Func>
void each (Func &&func)
template<typename Func>
void eachLive (Func &&func)
 Like each(), but iterates the live dense array with no defensive copy.

Detailed Description

template<typename... Components>
class esengine::ecs::View< Components >

View for iterating entities with multiple required components.

Template Parameters
ComponentsThe component types that entities must have

Provides iteration over all entities that have ALL of the specified component types. Optimizes by iterating over the smallest pool and filtering by other components.

// Iterate entities with Position AND Velocity
auto view = registry.view<Position, Velocity>();
// Range-based for loop
for (Entity e : view) {
auto& pos = view.get<Position>(e);
auto& vel = view.get<Velocity>(e);
pos.x += vel.x * dt;
}
// Each with callback
view.each([&](Entity e, Position& p, Velocity& v) {
p.x += v.x * dt;
});
Entity identifier with packed index + generation.
Definition Types.hpp:135
Velocity component for simple kinematic motion.
Definition Velocity.hpp:41

Definition at line 184 of file View.hpp.

Member Typedef Documentation

◆ Iterator

template<typename... Components>
using esengine::ecs::View< Components >::Iterator = ViewIterator<Components...>

Iterator type.

Definition at line 187 of file View.hpp.

◆ PoolsTuple

template<typename... Components>
using esengine::ecs::View< Components >::PoolsTuple = std::tuple<SparseSet<Components>*...>

Tuple type for pool pointers.

Definition at line 189 of file View.hpp.

Constructor & Destructor Documentation

◆ View()

template<typename... Components>
esengine::ecs::View< Components >::View ( PoolsTuple pools)
inline

Constructs a view from component pools.

Parameters
poolsTuple of pointers to component pools

Definition at line 195 of file View.hpp.

Member Function Documentation

◆ begin()

template<typename... Components>
Iterator esengine::ecs::View< Components >::begin ( ) const
inline

Returns iterator to the first matching entity.

Returns
Begin iterator

Definition at line 204 of file View.hpp.

References end().

◆ each()

template<typename... Components>
template<typename Func>
void esengine::ecs::View< Components >::each ( Func && func)
inline

Definition at line 277 of file View.hpp.

◆ eachLive()

template<typename... Components>
template<typename Func>
void esengine::ecs::View< Components >::eachLive ( Func && func)
inline

Like each(), but iterates the live dense array with no defensive copy.

Warning
The callback MUST NOT emplace/remove ANY of the iterated components — that reallocates or swap-pops the array being walked. Use for the read-only per-frame pass; use each() when the callback may mutate.

Definition at line 304 of file View.hpp.

◆ empty()

template<typename... Components>
bool esengine::ecs::View< Components >::empty ( ) const
inline

Checks if the view is empty.

Returns
True if no entities match

Definition at line 257 of file View.hpp.

◆ end()

template<typename... Components>
Iterator esengine::ecs::View< Components >::end ( ) const
inline

Returns iterator past the last entity.

Returns
End iterator

Definition at line 213 of file View.hpp.

Referenced by begin().

◆ get() [1/2]

template<typename... Components>
template<typename T>
T & esengine::ecs::View< Components >::get ( Entity entity)
inline

Gets a specific component from an entity.

Template Parameters
TThe component type to get
Parameters
entityThe entity (must be valid in view)
Returns
Reference to the component

Definition at line 229 of file View.hpp.

References get().

Referenced by get(), and get().

◆ get() [2/2]

template<typename... Components>
template<typename T>
const T & esengine::ecs::View< Components >::get ( Entity entity) const
inline

Gets a specific component from an entity.

Template Parameters
TThe component type to get
Parameters
entityThe entity (must be valid in view)
Returns
Reference to the component

Definition at line 235 of file View.hpp.

References get().

◆ getAll()

template<typename... Components>
std::tuple< Components &... > esengine::ecs::View< Components >::getAll ( Entity entity)
inline

Gets all components for an entity as a tuple.

Parameters
entityThe entity
Returns
Tuple of component references

Definition at line 244 of file View.hpp.

◆ sizeHint()

template<typename... Components>
usize esengine::ecs::View< Components >::sizeHint ( ) const
inline

Gets the estimated number of matching entities.

Returns
Upper bound on entity count (actual may be lower)

Returns the size of the smallest pool. Actual matches may be fewer due to filtering by other components.

Definition at line 268 of file View.hpp.


The documentation for this class was generated from the following file:
  • /home/runner/work/estella/estella/src/esengine/ecs/View.hpp