Systems and Queries
#
SystemsEntities are just collections of Facets which only contain data.
Systems provide all the behavior of your ECS.
#
Defining SystemsUse the useSystem hook to define a new system. It takes a callback that is called every time ECS.update is called.
useSystem returns null
so you can return it, to keep your system components tidy.
#
System PrioritiesBy default, all systems have a priority of 0
and are called in the order of your useSystem calls.
Pass a priority as the second parameter (lower is first) to override this behavior:
#
Using SystemsTo use a system just include it anywhere within the context of an ECS.Provider:
Systems don't need to appear as direct children of ECS.Provider. Organize things however you'd like.
#
Processing EntitiesThe useEngine hook can be used to access all Entities with your ECS:
Like with most things, The useEngine hook must be used within the context of an ECS.Provider.
#
QueriesQueries keep track of which Entities which have a specific set of Facets.
#
Creating QueriesUse the useQuery hook:
It takes a predicate function that should return true
if the passed Entity has the desired Facets.
Anytime an Entity is added or removed from the query, it will re-render the component.
#
Query EntitiesEvery Query has a .entities property that contains all the entities matched by the query.
However, there is also the Query.loop method:
Query.loop takes two arguments:
- a tuple of Facets types you're interested in.
- a function called for every entity matched by the query.
The callback receives two arguments:
- an entity
- the facet instances of that entity, that you requested with the tuple of types
In the example above, the query matches all entities with the IsEnemy
, Health
and PoisonStatus
. But the loop call only requests the Health
and PoisonStatus
effects, which it receives as health
and poison
.
The facet instances passed to your callbacks are type-safe and appear in the order in which you requested them:
A tuple of [Health, PoisonStatus]
results in a tuple [health, poison]
where health
is statically-typed as Health
and poison
is statically-typed as PoisonStatus
. (Pretty cool right?)
The tuple of types you request doesn't have to be the same facets used in the query's predicate. However, it is a good idea to keep it to a subset of the facets used in the query's predicate, to ensure the entities you loop over actually have the facets you request.
#
Query EventsThe useQuery hook has a second argument where you can supply event callbacks: