Systems to handle disjoint sets of components, or optional components #77
-
Is it correct to say that "For an entity to be handled by a system, it must have all the components specified in the query"? If so, how would you formulate a query for a system where entities with one set of components interacted with entities with a different set of components? For example, one set of components has a 'Sensor' system, but no 'Body'. Another set has a 'Body' system, but no 'Sensors'. A related question: Can a system have "optional" components in the query? For example during a damage calculation, only some entities might have a 'Shield' component? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Not quite. A query can have operators, which are documented in the Readme. For example
If you want to query two different sets, make two different queries. You can have as many queries as you want per System. Then all you have to do is match the results in your System.
const CountSystem = createSystem({
sensorQuery: queryComponents({
sensor: Read(Sensor),
}),
bodyQuery: queryComponents({
body: Read(Body),
}),
})
.withRunFunction(({sensorQuery, bodyQuery}) =>
sensorQuery.execute(({sensor}) =>
bodyQuery.execute(({body}) => {
if (sensor.isInRange(body) {
/* do something */
}
}))
)
.build(); Please note that query results are cached, so that going over the components in a loop like above is fast (using an array of object refs internally) and won't trigger querying again.
Yes, please take a look at the documentation. |
Beta Was this translation helpful? Give feedback.
Not quite. A query can have operators, which are documented in the Readme. For example
WithOut(Counter)
would return all entities without a Counter.If you want to query two different sets, make two different queries. You can have as many queries as you want per System. Then all you have to do is match the results in your System.