Labels
Labels are strings that can be attached to entities and then used during querying.
Attaching a label to an entity
After spawning an entity, call setEntityLabel(entityId, label)
:
ts
const id = engine.spawn(...)
engine.setEntityLabel(id, "My Important Label")
Getting a label from an entity
To check which label is attached to an entity, call getEntityLabel(entityId)
:
ts
const label = engine.getEntityLabel(id)
If no label was attached to the entity, it will return undefined
.
Querying result by Label
When querying entities with Query<T>
, the results will have a method called getByLabel(label)
that can be called to get the first entity in the results that has the label specified attached.
ts
function system(query: Query<[Car]>) {
const result = query.getByLabel("Toyota")
if (result) {
console.log(result[0]) // car component
}
}