Interface: EcsSystem<TQuery, TBeforeQueryResult, TAfterRunInput>
Defined in: ecs/ecs-system.ts:18
A unit of per-tick logic that operates on every entity matching its
query (and tags, if given). Register an instance with
EcsWorld.addSystem to have run invoked for each matching entity every
EcsWorld.update call.
Type Parameters
TQuery
TQuery extends readonly unknown[] = readonly unknown[]
The tuple of component data types the system reads,
in query order. Matches the array of ComponentKeys in query.
TBeforeQueryResult
TBeforeQueryResult = null
The type returned by beforeQuery and
passed to every run call this tick. Defaults to null when
beforeQuery is not implemented.
TAfterRunInput
TAfterRunInput = void
The type returned by run and collected into
the array passed to afterRun. Defaults to void when run returns
nothing.
Properties
query
query:
KeysFromComponents<TQuery>
Defined in: ecs/ecs-system.ts:27
The component keys an entity must have for this system to process it.
Order determines the order of queryResult.components passed to run.
tags?
optionaltags?:TagKey[]
Defined in: ecs/ecs-system.ts:33
Additional tag keys an entity must have for this system to process it.
Unlike query, tags don't contribute a value to
queryResult.components.
Methods
afterRun()?
optionalafterRun(inputs):void
Defined in: ecs/ecs-system.ts:64
Invoked once per tick, after run has been called for every matched
entity, with an array of every one of those calls' return values (in
query order; empty if nothing matched). Use it for work that needs to
see the whole tick's results together rather than one entity at a time:
for example the render system collects one RenderPassResult per
camera in run, then afterRun draws every camera's batch once all of
them are known.
Parameters
inputs
TAfterRunInput[]
Returns
void
beforeQuery()?
optionalbeforeQuery(world):TBeforeQueryResult
Defined in: ecs/ecs-system.ts:74
Invoked once per tick, before entity iteration begins, to compute a
value shared across every run call this tick. Use it for expensive,
shared pre-computation (for example a spatial index) that would be
wasteful to redo for each matched entity.
Parameters
world
The EcsWorld running this system.
Returns
TBeforeQueryResult
The value to pass as beforeQueryResult to every run call
this tick.
cleanupEntities()?
optionalcleanupEntities(queryResult,world):void
Defined in: ecs/ecs-system.ts:83
Invoked for every entity still matching query (and tags) when the
owning EcsWorld is stopped via EcsWorld.stop. Use it to release
resources the system acquired for that entity.
Parameters
queryResult
QueryResult<TQuery>
The matched entity's id and its queried component data, in query order.
world
The EcsWorld that is stopping.
Returns
void
cleanupSystem()?
optionalcleanupSystem(world):void
Defined in: ecs/ecs-system.ts:89
Invoked once when the system is removed from an EcsWorld. Use it to
release resources the system acquired for its lifetime.
Parameters
world
The EcsWorld the system was removed from.
Returns
void
onRegister()?
optionalonRegister(world):void
Defined in: ecs/ecs-system.ts:39
Invoked once when the system is registered with an EcsWorld. Use it to
acquire resources the system will need for its lifetime.
Parameters
world
The EcsWorld the system was registered with.
Returns
void
run()
run(
queryResult,world,beforeQueryResult):TAfterRunInput
Defined in: ecs/ecs-system.ts:50
Invoked once per tick for every entity matching query (and tags).
Parameters
queryResult
QueryResult<TQuery>
The matched entity's id and its queried component data, in query order.
world
The EcsWorld running this system.
beforeQueryResult
TBeforeQueryResult
The value returned by beforeQuery for this
tick, or null if beforeQuery is not implemented.
Returns
TAfterRunInput
A value to be collected, along with every other matched
entity's return value this tick, into the array passed to afterRun.