Querying Components
Learn about how to obtain lists of entities that have components in common, to make checking or updating them easier.
Last updated
// Define a System
function PhysicsSystem(dt: number) {
// query for entities that include both a Transform and a Physics component
for (const [entity] of engine.getEntitiesWith(Transform, Physics)) {
const transform = Transform.getMutable(entity)
const vel = Physics.get(entity).velocity
transform.position.x += vel.x
transform.position.y += vel.y
transform.position.z += vel.z
}
}
// Add the system to the engine
engine.addSystem(PhysicsSystem)for (const [entity] of engine.getEntitiesWith(myComponent, myOtherComponent)) {
//...
}for (const [entity] of engine.getEntitiesWith(Transform)) {
//get read-only version
const transformReadOnly = Transform.get(entity)
// get mutable version
const transformMutable = Transform.getMutable(entity)
}// returns references to the entity and the first listed component
for (const [entity, component1] of engine.getEntitiesWith(
MyCustomComponent1,
MyCustomComponent2
)) {
// iterate over list of entities
}
// returns references to the entity and the first two listed components
for (const [entity, component1, component2] of engine.getEntitiesWith(
MyCustomComponent1,
MyCustomComponent2
)) {
// iterate over list of entities
}for (const [entity, transformReadOnly] of engine.getEntitiesWith(Transform)) {
console.log('entity id: ', entity)
console.log('has position : ', transformReadOnly.position)
}