Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace Query

A module used to locate entities based on their component composition.

Index

Type aliases

Functions

Type aliases

RecordData

RecordData<$Type>: { [ K in keyof $Type]: $Type[K] extends Schema.Id ? Archetype.Column<$Type[K]>["data"] : never }

Component data sorted to match a query selector layout.

example

Scalar binary query data

const data: Query.RecordData<[Health]> = [
Float64Array,
]
example

Complex binary query data (struct-of-array)

const data: Query.RecordData<[Position, Health]> = [
[{ x: Float64Array, y: Float64Array }],
Float64Array,
]
example

Complex native query data (array-of-struct)

const data: Query.RecordData<[Position, Health]> = [
[{ x: 0, y: 0 }],
[0],
]

Type parameters

Record

Record<$Type>: [entities: ReadonlyArray<Entity.Id>, data: RecordData<$Type>]

A single query result.

example

Iterating a query record (array-of-struct)

const [e, [p, v]] = record
for (let i = 0; i < e.length; i++) {
const entity = e[i]
const position = p[i]
const velocity = v[i]
// ...
}

Type parameters

Struct

Struct<$Type>: Record<$Type>[]

An iterable list of query records, where each result corresponds to an archetype (or archetype) of entities that match the query's selector.

example

Iterate a query

const query = Query.make(world, [Position, Velocity])
for (let i = 0; i < query.length; i++) {
const [entities, [p, v]] = record
// ...
}

Type parameters

Filter

Filter: (type: Type.Struct, archetype: Archetype.Struct) => boolean

Type declaration

    • (type: Type.Struct, archetype: Archetype.Struct): boolean
    • A function that is executed when an entity is considered by a query. Returning false will exclude the entity from the query results.

      Parameters

      Returns boolean

Functions

make

  • Create an auto-updating list of entities and matching components that match both a set of schema ids and provided query filters, if any. The query will attempt to include newly-incorporated archetypes as the ECS grows in complexity.

    example

    A simple motion system (struct-of-array)

    const Kinetic = [Position, Velocity] as const
    const kinetics = Query.make(world, Kinetic)
    for (let i = 0; i < kinetics.length; i++) {
    const [e, [p, v]] = kinetics
    for (let j = 0; j < e.length; j++) {
    // apply motion
    p.x[j] += v.x[j]
    p.y[j] += v.y[j]
    }
    }

    Type parameters

    • $Type: readonly Schema.Id<AnySchema>[]

    Parameters

    Returns Query.Struct<$Type>

makeStatic

  • Create an auto-updating list of results that match both a set of schema ids and provided query filters, if any. The query will not attempt to include newly-incorporated archetypes.

    example

    Ignoring new archetypes

    Entity.make(world, [Point])
    const points = Query.make(world, [Point])
    points.reduce((a, [e]) => a + e.length, 0) // 1
    Entity.make(world, [Point, Velocity])
    points.reduce((a, [e]) => a + e.length, 0) // 1 (did not detect (P, V) archetype)

    Type parameters

    • $Type: readonly Schema.Id<AnySchema>[]

    Parameters

    Returns Query.Struct<$Type>

not

Generated using TypeDoc