Systems¶
Purpose¶
A system is a function executed by the ECS to update simulation state (usually by iterating queries and mutating component values). Systems are registered on the World, and executed during world.update(dt).
System function type¶
SystemFn¶
A system is a function with the signature:
(world: WorldApi, dt: number) => void
In practice, examples call
query()andcmd()inside systems, which are available throughWorldApi.
Registering systems¶
world.addSystem(fn): this¶
Adds a system to the world.
- Systems run in the order they were added (as described by “runs systems in order”).
Example:
1 2 3 4 5 6 7 8 |
Running systems (frame execution)¶
world.update(dt): void¶
Runs one ECS frame:
- Runs all registered systems (in order)
- Flushes queued commands at the end
The reference summary explicitly lists:
addSystem(fn): thisupdate(dt): void(runs systems in order, then flushes)
Structural changes inside systems¶
While systems are running (and while iterating queries), doing structural changes directly can throw. The recommended pattern is:
- enqueue structural changes with
world.cmd() - apply them with
world.flush()(or letupdate()do it at the end)
Systems in phases (Schedule)¶
If you need explicit ordering across groups of systems, use Schedule:
sched.add(phase, systemFn)sched.run(world, dt, phases)runs phases in order and callsworld.flush()after each phase
This provides deterministic “phase barriers” where deferred commands are applied.