Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::View< Components > Class Template Reference

Visits every entity that owns all of Components, with those components. More...

#include <aegis2d/ecs/view.hpp>

Public Member Functions

 View (SparseSet< Components > &... pools) noexcept
 Creates a view on the given pools.
 
template<typename Function >
requires std::invocable<Function&, Entity, Components&...> || std::invocable<Function&, Components&...>
void each (Function function)
 Calls a function for every entity that owns all the components.
 
bool contains (Entity entity) const noexcept
 Tells whether an entity owns all the components of the view.
 
template<typename Component >
requires (std::is_same_v<Component, Components> || ...)
Component & get (Entity entity) const
 One component of an entity of the view.
 
std::size_t size_hint () const noexcept
 Upper bound of the number of entities visited.
 

Detailed Description

template<typename... Components>
requires (sizeof...(Components) > 0) && detail::distinct_types<Components...>
class aegis2d::ecs::View< Components >

Visits every entity that owns all of Components, with those components.

A view is a cheap handle on the pools of its component types: it copies nothing and sees the pools as they are when it iterates. Usually obtained from Registry::view():

registry.view<Transform, Velocity>().each([dt](Transform& transform, const Velocity& velocity) {
transform.position += velocity.value * dt;
});
void each(Function function)
Calls a function for every entity that owns all the components.
Definition view.hpp:65
Where an entity is.
Definition components.hpp:17
Vec2 position
Center of the entity.
Definition components.hpp:19
How fast an entity moves.
Definition components.hpp:25
Vec2 value
Speed in logical pixels per second.
Definition components.hpp:27

Iteration walks the smallest pool and checks the others in O(1), so its cost follows the rarest component, not the number of entities. The order of the visit is not specified.

The callback may remove components from the entity it is visiting, or destroy it: the other entities are still visited exactly once. Adding or removing components of the viewed types on other entities during the visit is not supported, and invalidates the references the callback holds.

Template Parameters
ComponentsComponent types, at least one, all different.

Constructor & Destructor Documentation

◆ View()

template<typename... Components>
aegis2d::ecs::View< Components >::View ( SparseSet< Components > &...  pools)
inlineexplicitnoexcept

Creates a view on the given pools.

Parameters
poolsOne pool per component type, in the order of Components; they must outlive the view.

Member Function Documentation

◆ contains()

template<typename... Components>
bool aegis2d::ecs::View< Components >::contains ( Entity  entity) const
inlinenoexcept

Tells whether an entity owns all the components of the view.

Parameters
entityHandle to check; null and stale handles own nothing.
Returns
true if every pool contains the entity.

◆ each()

template<typename... Components>
template<typename Function >
requires std::invocable<Function&, Entity, Components&...> || std::invocable<Function&, Components&...>
void aegis2d::ecs::View< Components >::each ( Function  function)
inline

Calls a function for every entity that owns all the components.

Parameters
functionCalled as function(entity, components&...) if it accepts the entity first, otherwise as function(components&...); components are passed in the order of Components.

◆ get()

template<typename... Components>
template<typename Component >
requires (std::is_same_v<Component, Components> || ...)
Component & aegis2d::ecs::View< Components >::get ( Entity  entity) const
inline

One component of an entity of the view.

Template Parameters
ComponentOne of Components.
Parameters
entityHandle that owns a Component.
Returns
The component.
Exceptions
std::out_of_rangeIf the entity has no Component.

◆ size_hint()

template<typename... Components>
std::size_t aegis2d::ecs::View< Components >::size_hint ( ) const
inlinenoexcept

Upper bound of the number of entities visited.

Returns
Size of the smallest pool of the view.

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