Query

View source

Selects entities in a World by component type and exposes their component values as ordered tuples.

Use a query for repeated component reads or iteration. Pass QueryFilter instances to refine which entities participate. The query’s matching component combinations are established when it is constructed and refreshed only by Query#update; entities using a new component combination are not included until update() is called.

Examples

import { Query, World, optional } from '@wimaengine/ecs'

class Position {
  constructor(x = 0) {
    this.x = x
  }
}
class Velocity {
  constructor(x = 0) {
    this.x = x
  }
}

const world = new World()
const entity = world.spawn([new Position()])
const query = new Query(world, [Position, Velocity], [optional(Velocity)])

world.spawn([new Position(10), new Velocity(1)])
query.update()

query.each(([position, velocity]) => {
  position.x += velocity?.x ?? 0
})
const first = query.single()
const entityComponents = query.get(entity)

Constructor


constructor<T extends unknown[], U extends QueryFilter<FilterTransform>[] = []>(world: World, componentTypes: [...TupleConstructor<T>[]], filters?: [...U[]])

View source

Creates a query for a world and an ordered component signature.

Component constructors are converted to type IDs in the order supplied, and that order is retained in every returned tuple. A component is required when every filter reports it as required through QueryFilter#isRequired. The built-in Optional, Has, and Without filters refine matching entities without adding entries to the result tuple; all configured filters must accept an entity’s component combination for it to match.

The query’s matching selection is established immediately. Construction does not add entities or components to the world, and the component types do not need to have appeared in the world beforehand.

Type Parameters

T extends unknown[]

U extends QueryFilter<FilterTransform>[] = []

Parameters

NameDescriptionDefault
world

World whose entities the query reads.

—
componentTypes

Component constructors in the order returned by query operations.

—
filters?

Filters that refine which component combinations match.

...

Properties


readonlydescriptors: TypeId[]

View source

Component type IDs in the order supplied to the constructor.

Query results use this order, so each tuple entry corresponds to the component constructor at the same position.

Default: []

readonlyrequiredDescriptors: TypeId[]

View source

Component type IDs that an entity must have to match by default.

A component type ID remains in this list only when every configured filter’s QueryFilter#isRequired result is true. The built-in Optional filter makes its targeted component optional; filters such as Has and Without still constrain which entities match without changing the returned tuple.

Default: []

readonlyfilters: U

View source

Filters applied when the query’s matching entities are refreshed.

Every configured filter must accept an entity’s component combination for it to match. The supplied filters remain the query’s configuration; call Query#update after changing the world in a way that creates a new component combination.

Methods


update(): void

View source

Refreshes the query’s matching component combinations from the current world.

Matching component combinations are considered in their creation order. A combination is selected only when it contains every required component and every configured filter accepts it. The previous selection is replaced, not appended to. The constructor calls this method once; call it after a world mutation creates a new component combination that should be considered. Entities added to a combination already selected are visible without another update.

This method changes only what the query selects. It does not change entities, components, or the world.

get(entity: EntityHandle): ApplyFilters<T, U>

View source

Gets the components of an entity when its current component combination matches the query.

The returned tuple follows the component order supplied to the constructor and is a new array for each call. Values are read from the entity’s current component combination; an optional component is undefined when that component is absent. Returns null for a stale or unknown EntityHandle, a non-matching entity, an unavailable matching combination, or an invalid entity location. This method does not refresh the query’s selection.

Parameters

NameDescriptionDefault
entity

Entity handle to check against the query.

—

Returns

The matching components, or null.

each(callback: EachFunc<ApplyFilters<T, U>>): void

View source

Calls a callback once for every entity in the query’s current matching selection.

Matching component combinations are visited in selection order, with entities in increasing order within each combination. The callback receives one component tuple in constructor order; the entity handle is not passed separately, so include EntityHandle in componentTypes when it is needed. The same tuple array is reused and overwritten for the next callback, so do not retain that array; the component objects in it are the live values owned by the world. This method does not refresh the query’s selection.

Parameters

NameDescriptionDefault
callback

Function called for each matching entity.

—

eachCombination(callback: EachCombinationFunc<ApplyFilters<T, U>>): void

View source

Calls a callback for every unique unordered pair of matching entities.

Each callback receives two component tuples in constructor order. The nested traversal visits later matching component combinations and, within one combination, only entities after the first entity, so pairs are not repeated with their order reversed and an entity is never paired with itself. The two tuple arrays are reused and overwritten for the next callback; do not retain them. This method does not refresh the query’s selection.

Parameters

NameDescriptionDefault
callback

Function called for each distinct pair.

—

single(): ApplyFilters<T, U>

View source

Returns the components of the first matching entity.

The first entity is the first available entity in query traversal order, so this method does not require the query to contain exactly one entity. It returns a new tuple array, does not refresh the selection, and returns null when no matching entity is available. Use Query#count when the number of matches matters.

Returns

The first matching components, or null when there are no matches.

count(): number

View source

Counts entities in the query’s current matching selection.

It reflects current entity membership in matching component combinations, but a new combination created after the last Query#update is not included. Missing matching combinations contribute zero, and this method does not refresh or re-filter the selection.

Returns

The number of entities in the current matching selection.