Optional

View source

Represents a filter that makes a component descriptor optional in a Query while retaining its position in the result tuple.

Use optional when constructing this filter through the factory API.

Examples

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

class Position {}
class Velocity {}

const world = new World()
world.spawn([new Position()])
const query = new Query(world, [Position, Velocity], [new Optional(Velocity)])
query.single() // [Position instance, undefined]

Constructor


constructor<T>(component: Constructor<T>)

View source

Creates an optional-component filter.

The component constructor is converted to its TypeId with typeid. Construction does not add the component to a query; pass the resulting filter alongside a query whose component descriptor list already contains that constructor, or use optional.

Type Parameters

T

Parameters

NameDescriptionDefault
component

Component constructor whose descriptor may be absent.

—

Properties


readonlytypeid: TypeId

View source

Type ID of the component descriptor made optional by this filter.

Optional#isRequired compares each query descriptor with this value. The ID is derived from the constructor supplied to Optional#constructor and is not a component value or an archetype ID.

Methods


isRequired(type: TypeId): boolean

View source

Makes only this filter’s component descriptor non-required.

Query calls this method for every descriptor while building its required-descriptor list. It returns false when type equals Optional#typeid, and true for every other descriptor. The targeted descriptor stays in the query tuple and is read as undefined when the matching archetype has no such component; this method does not inspect an archetype or alter other filters.

Parameters

NameDescriptionDefault
type

Query descriptor TypeId being tested.

—

Returns

false for this filter’s TypeId; otherwise true.

archetype(_types: readonly TypeId[]): boolean

View source

Returns whether an archetype’s component layout matches this filter.

Query#update passes the complete, read-only TypeId list from the Archetype. The method does not receive an entity, row, or component value. The default accepts every archetype; a query includes an archetype only when this method returns true for every configured filter.

Parameters

NameDescriptionDefault
_types

Complete component TypeIds in the archetype.

—

Returns

Whether the archetype is accepted by this filter.