optional

View sourceoptional<T>(component: Constructor<T>): Optional<T>

Creates an Optional filter for a component constructor.

When passed to a Query, the filter makes each matching component descriptor whose TypeId matches component optional. The component remains in its original position in each returned tuple and is undefined when the entity’s archetype does not contain it. Other filters continue to constrain the query. The component constructor must already be in the query’s component list; this factory does not add a descriptor or tuple entry.

Examples

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

class Position {}
class Velocity {}

const world = new World()
world.spawn([new Position()])

const query = new Query(world, [Position, Velocity], [optional(Velocity)])
query.each(([position, velocity]) => {
  // `velocity` is undefined when the entity has no Velocity component.
})

Type Parameters

T

Parameters

NameDescriptionDefault
component

Component constructor whose descriptor should be optional.

—

Returns

An Optional filter that allows the component to be absent from matching archetypes.