Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::Scheduler Class Reference

Ordered list of named systems, all updated once per tick. More...

#include <aegis2d/ecs/system.hpp>

Public Member Functions

 Scheduler ()=default
 
 Scheduler (const Scheduler &)=delete
 
Scheduler & operator= (const Scheduler &)=delete
 
 Scheduler (Scheduler &&) noexcept=default
 
Scheduler & operator= (Scheduler &&) noexcept=default
 
 ~Scheduler ()=default
 
template<std::derived_from< ISystem > System, typename... Args>
requires std::constructible_from<System, Args...>
System & add (std::string name, Args &&... args)
 Builds a system in place and adds it after the others.
 
ISystem & add (std::string name, std::unique_ptr< ISystem > system)
 Adds a system after the others.
 
FunctionSystem & add (std::string name, FunctionSystem::Function function)
 Adds a function as a system after the others.
 
bool remove (std::string_view name)
 Removes a system; the others keep their order.
 
bool contains (std::string_view name) const noexcept
 Tells whether a system has this name.
 
void set_enabled (std::string_view name, bool enabled)
 Enables or disables a system; a disabled system keeps its place but is skipped by update().
 
bool enabled (std::string_view name) const
 Tells whether a system runs on update().
 
std::vector< std::string_view > names () const
 Names of the systems, in update order.
 
std::size_t size () const noexcept
 Number of systems, enabled or not.
 
bool empty () const noexcept
 Tells whether no system was added.
 
void update (Registry &registry, core::Duration step)
 Runs one tick: every enabled system, in order.
 
void set_profiler (core::Profiler *profiler) noexcept
 Times each system with a profiler, in a scope named after it.
 
std::uint64_t ticks () const noexcept
 Number of completed updates.
 
bool updating () const noexcept
 Tells whether update() is running the systems.
 

Detailed Description

Ordered list of named systems, all updated once per tick.

Systems run in the order they were added, so the order is written once, where the game registers them; e.g. on the server: inputs, movement, collisions, damage, cleanup. Each binary has its own scheduler and registers its own systems on the same engine types. update() is meant to be called by the fixed-step callback of core::MainLoop:

ecs::Registry registry;
systems.add<InputSystem>("input", inputs);
systems.add("movement", [](ecs::Registry& world, const ecs::Tick& tick) {
world.view<Transform, Velocity>().each([&tick](Transform& t, const Velocity& v) {
t.position += v.value * tick.delta();
});
});
loop.set_update([&](core::Duration step) { systems.update(registry, step); });
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
View< Components... > view()
View on the entities that own all the listed components.
Definition registry.hpp:222
Ordered list of named systems, all updated once per tick.
Definition system.hpp:100
void update(Registry &registry, core::Duration step)
Runs one tick: every enabled system, in order.
System & add(std::string name, Args &&... args)
Builds a system in place and adds it after the others.
Definition system.hpp:118
std::chrono::nanoseconds Duration
Duration type used for simulation time (nanoseconds).
Definition clock.hpp:14
One simulation step, as the systems see it.
Definition system.hpp:23
constexpr float delta() const noexcept
Duration of the step in seconds, to scale values given per second.
Definition system.hpp:32
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

A system can be disabled (a paused menu, a debug overlay) without losing its place. Systems cannot be added or removed while update() runs. Not thread-safe: use the scheduler from the game thread only.

Constructor & Destructor Documentation

◆ Scheduler() [1/3]

aegis2d::ecs::Scheduler::Scheduler ( )
default

◆ Scheduler() [2/3]

aegis2d::ecs::Scheduler::Scheduler ( const Scheduler &  )
delete

◆ Scheduler() [3/3]

aegis2d::ecs::Scheduler::Scheduler ( Scheduler &&  )
defaultnoexcept

◆ ~Scheduler()

aegis2d::ecs::Scheduler::~Scheduler ( )
default

Member Function Documentation

◆ add() [1/3]

template<std::derived_from< ISystem > System, typename... Args>
requires std::constructible_from<System, Args...>
System & aegis2d::ecs::Scheduler::add ( std::string  name,
Args &&...  args 
)
inline

Builds a system in place and adds it after the others.

Template Parameters
SystemClass derived from ISystem.
Parameters
nameUnique, non-empty name, used to remove or disable the system.
argsArguments of the system's constructor.
Returns
The new system, owned by the scheduler.
Exceptions
std::invalid_argumentIf name is empty or already used.
std::logic_errorIf called while update() runs.

◆ add() [2/3]

FunctionSystem & aegis2d::ecs::Scheduler::add ( std::string  name,
FunctionSystem::Function  function 
)

Adds a function as a system after the others.

Parameters
nameUnique, non-empty name, used to remove or disable the system.
functionCalled with the registry and the tick by every update().
Returns
The system wrapping the function.
Exceptions
std::invalid_argumentIf name is empty or already used, or function is empty.
std::logic_errorIf called while update() runs.

◆ add() [3/3]

ISystem & aegis2d::ecs::Scheduler::add ( std::string  name,
std::unique_ptr< ISystem >  system 
)

Adds a system after the others.

Parameters
nameUnique, non-empty name, used to remove or disable the system.
systemSystem to own.
Returns
The system.
Exceptions
std::invalid_argumentIf name is empty or already used, or system is null.
std::logic_errorIf called while update() runs.

◆ contains()

bool aegis2d::ecs::Scheduler::contains ( std::string_view  name) const
noexcept

Tells whether a system has this name.

Parameters
nameName to look up.
Returns
true if a system was added with it and not removed.

◆ empty()

bool aegis2d::ecs::Scheduler::empty ( ) const
noexcept

Tells whether no system was added.

Returns
true when size() is 0.

◆ enabled()

bool aegis2d::ecs::Scheduler::enabled ( std::string_view  name) const

Tells whether a system runs on update().

Parameters
nameName of the system.
Returns
true unless it was disabled with set_enabled().
Exceptions
std::out_of_rangeIf no system has this name.

◆ names()

std::vector< std::string_view > aegis2d::ecs::Scheduler::names ( ) const

Names of the systems, in update order.

Returns
Views of the names, valid until the system is removed.

◆ operator=() [1/2]

Scheduler & aegis2d::ecs::Scheduler::operator= ( const Scheduler &  )
delete

◆ operator=() [2/2]

Scheduler & aegis2d::ecs::Scheduler::operator= ( Scheduler &&  )
defaultnoexcept

◆ remove()

bool aegis2d::ecs::Scheduler::remove ( std::string_view  name)

Removes a system; the others keep their order.

Parameters
nameName of the system.
Returns
true if it was found; false (nothing changes) otherwise.
Exceptions
std::logic_errorIf called while update() runs.

◆ set_enabled()

void aegis2d::ecs::Scheduler::set_enabled ( std::string_view  name,
bool  enabled 
)

Enables or disables a system; a disabled system keeps its place but is skipped by update().

Can be called by a system during update(): the change applies to the systems that have not run yet in this tick.

Parameters
nameName of the system.
enabledfalse to skip it, true to run it again.
Exceptions
std::out_of_rangeIf no system has this name.

◆ set_profiler()

void aegis2d::ecs::Scheduler::set_profiler ( core::Profiler *  profiler)
noexcept

Times each system with a profiler, in a scope named after it.

Parameters
profilerProfiler of the frame, kept by pointer; nullptr, the default, times nothing.

◆ size()

std::size_t aegis2d::ecs::Scheduler::size ( ) const
noexcept

Number of systems, enabled or not.

Returns
The count.

◆ ticks()

std::uint64_t aegis2d::ecs::Scheduler::ticks ( ) const
noexcept

Number of completed updates.

Returns
The index the next tick will have.

◆ update()

void aegis2d::ecs::Scheduler::update ( Registry &  registry,
core::Duration  step 
)

Runs one tick: every enabled system, in order.

The systems get a Tick with step and the index ticks(). If a system throws, the exception propagates, the next systems do not run and the tick is not counted; the scheduler can be updated again.

Parameters
registryWorld given to the systems.
stepDuration of the tick, usually the fixed step of core::MainLoop.
Exceptions
std::logic_errorIf called by a system, during update().

◆ updating()

bool aegis2d::ecs::Scheduler::updating ( ) const
noexcept

Tells whether update() is running the systems.

Returns
true from the first system of a tick to the end of the last one.

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