@latticexyz/recs
#
@latticexyz/recs
#
Table of contents
#
Enumerations
#
Interfaces
#
Type Aliases
AnyComponent AnyComponentValue ArrayType ComponentUpdate ComponentValue Components EntityID EntityIndex EntityQueryFragment EntityType HasQueryFragment HasValueQueryFragment Indexer Layer Layers Metadata NotQueryFragment NotValueQueryFragment NumberType OptionalType OverridableComponent Override ProxyExpandQueryFragment ProxyReadQueryFragment QueryFragment QueryFragments Schema SchemaOf SettingQueryFragment ValueType World
#
Variables
OptionalTypes
#
Functions
Has HasValue Not NotValue ProxyExpand ProxyRead clearLocalCache componentValueEquals createEntity createIndexer createLocalCache createWorld defineComponent defineComponentSystem defineEnterQuery defineEnterSystem defineExitQuery defineExitSystem defineQuery defineRxSystem defineSyncSystem defineSystem defineUpdateQuery defineUpdateSystem getChildEntities getComponentEntities getComponentValue getComponentValueStrict getEntitiesWithValue getEntityComponents hasComponent isArrayType isComponentUpdate isEntityType isFullComponentValue isIndexer isNumberType isOptionalType namespaceWorld overridableComponent removeComponent runQuery setComponent toUpdate toUpdateStream updateComponent withValue
#
Type Aliases
#
AnyComponent
Ƭ AnyComponent: Component
<Schema
#
Defined in
#
AnyComponentValue
Ƭ AnyComponentValue: ComponentValue
Schema
#
Defined in
#
ArrayType
Ƭ ArrayType: NumberArray
| OptionalNumberArray
| BigIntArray
| OptionalBigIntArray
| StringArray
| OptionalStringArray
| EntityArray
| OptionalEntityArray
#
Defined in
#
ComponentUpdate
Ƭ ComponentUpdate<S
, T
>: Object
Type of a component update corresponding to a given
#
Type parameters
#
Type declaration
#
Defined in
#
ComponentValue
Ƭ ComponentValue<S
, T
>: { [key in keyof S]: ValueType<T>[S[key]] }
Used to infer the TypeScript type of a component value corresponding to a given
#
Type parameters
#
Defined in
#
Components
Ƭ Components: Object
#
Index signature
▪ [key: string
]: Component
#
Defined in
#
EntityID
Ƭ EntityID: Opaque
<string
, "EntityID"
>
Used to refer to the string id of an entity (independent from a
#
Defined in
#
EntityIndex
Ƭ EntityIndex: Opaque
<number
, "EntityIndex"
>
Used to refer to the index of an entity in a
#
Defined in
#
EntityQueryFragment
Ƭ EntityQueryFragment<T
>: HasQueryFragment
T
> | HasValueQueryFragment
T
> | NotQueryFragment
T
> | NotValueQueryFragment
T
>
#
Type parameters
#
Defined in
#
EntityType
Ƭ EntityType: Entity
| OptionalEntity
#
Defined in
#
HasQueryFragment
Ƭ HasQueryFragment<T
>: Object
#
Type parameters
#
Type declaration
#
Defined in
#
HasValueQueryFragment
Ƭ HasValueQueryFragment<T
>: Object
#
Type parameters
#
Type declaration
#
Defined in
#
Indexer
Ƭ Indexer<S
, M
, T
>: Component
<S
, M
, T
> & { getEntitiesWithValue
: (value
: ComponentValue
S
, T
>) => Set
<EntityIndex
Type of indexer returned by
#
Type parameters
#
Defined in
#
Layer
Ƭ Layer: Object
#
Type declaration
#
Defined in
#
Layers
Ƭ Layers: Record
<string
, Layer
#
Defined in
#
Metadata
Ƭ Metadata: { [key: string]
: unknown
; } | undefined
Used to add arbitrary metadata to components.
(Eg contractId
for components that have a corresponding solecs component contract.)
#
Defined in
#
NotQueryFragment
Ƭ NotQueryFragment<T
>: Object
#
Type parameters
#
Type declaration
#
Defined in
#
NotValueQueryFragment
Ƭ NotValueQueryFragment<T
>: Object
#
Type parameters
#
Type declaration
#
Defined in
#
NumberType
Ƭ NumberType: Number
| OptionalNumber
#
Defined in
#
OptionalType
Ƭ OptionalType: OptionalNumber
| OptionalBigInt
| OptionalString
| OptionalEntity
| OptionalNumberArray
| OptionalBigIntArray
| OptionalStringArray
| OptionalEntityArray
#
Defined in
#
OverridableComponent
Ƭ OverridableComponent<S
, M
, T
>: Component
<S
, M
, T
> & { addOverride
: (overrideId
: string
, update
: Override
S
, T
>) => void
; removeOverride
: (overrideId
: string
) => void
}
Type of overridable component returned by
#
Type parameters
#
Defined in
#
Override
Ƭ Override<S
, T
>: Object
#
Type parameters
#
Type declaration
#
Defined in
#
ProxyExpandQueryFragment
Ƭ ProxyExpandQueryFragment: Object
#
Type declaration
#
Defined in
#
ProxyReadQueryFragment
Ƭ ProxyReadQueryFragment: Object
#
Type declaration
#
Defined in
#
QueryFragment
Ƭ QueryFragment<T
>: HasQueryFragment
T
> | HasValueQueryFragment
T
> | NotQueryFragment
T
> | NotValueQueryFragment
T
> | ProxyReadQueryFragment
ProxyExpandQueryFragment
#
Type parameters
#
Defined in
#
QueryFragments
Ƭ QueryFragments: QueryFragment
Schema
#
Defined in
#
Schema
Ƭ Schema: Object
Used to define the schema of a Component. Uses Type enum to be able to access the component type in JavaScript as well as have TypeScript type checks.
#
Index signature
▪ [key: string
]: Type
#
Defined in
#
SchemaOf
Ƭ SchemaOf<C
>: C
extends Component
<infer S> ? S
: never
#
Type parameters
#
Defined in
#
SettingQueryFragment
Ƭ SettingQueryFragment: ProxyReadQueryFragment
ProxyExpandQueryFragment
#
Defined in
#
ValueType
Ƭ ValueType<T
>: Object
Mapping between JavaScript Type enum and corresponding TypeScript type.
#
Type parameters
#
Type declaration
#
Defined in
#
World
Ƭ World: Object
Type of World returned by
#
Type declaration
#
Defined in
#
Variables
#
OptionalTypes
• Const
OptionalTypes: Type
[]
Helper constant with all optional Types.
#
Defined in
#
Functions
#
Has
▸ Has<T
>(component
): HasQueryFragment
T
>
Create a
Remarks
The
Example
Query for all entities with a Position
.
runQuery([Has(Position)]);
#
Type parameters
#
Parameters
#
Returns
HasQueryFragment
T
>
query fragment to be used in
#
Defined in
#
HasValue
▸ HasValue<T
>(component
, value
): HasValueQueryFragment
T
>
Create a
Remarks
The
Example
Query for all entities at Position (0,0).
runQuery([HasValue(Position, { x: 0, y: 0 })]);
#
Type parameters
#
Parameters
#
Returns
HasValueQueryFragment
T
>
query fragment to be used in
#
Defined in
#
Not
▸ Not<T
>(component
): NotQueryFragment
T
>
Create a
Remarks
The
Example
Query for all entities with a Position
that are not Movable
.
runQuery([Has(Position), Not(Movable)]);
#
Type parameters
#
Parameters
#
Returns
NotQueryFragment
T
>
query fragment to be used in
#
Defined in
#
NotValue
▸ NotValue<T
>(component
, value
): NotValueQueryFragment
T
>
Create a
Remarks
The
Example
Query for all entities that have a Position
, except for those at Position
(0,0).
runQuery([Has(Position), NotValue(Position, { x: 0, y: 0 })]);
#
Type parameters
#
Parameters
#
Returns
NotValueQueryFragment
T
>
query fragment to be used in
#
Defined in
#
ProxyExpand
▸ ProxyExpand(component
, depth
): ProxyExpandQueryFragment
Create a
Remarks
The depth
on the relationship chain defined by the given component
.
Example
Query for all entities (directly or indirectly) owned by an entity with Name
"Alice".
runQuery([ProxyExpand(OwnedByEntity, Number.MAX_SAFE_INTEGER), HasValue(Name, { name: "Alice" })]);
#
Parameters
#
Returns
ProxyExpandQueryFragment
query fragment to be used in
#
Defined in
#
ProxyRead
▸ ProxyRead(component
, depth
): ProxyReadQueryFragment
Create a
Remarks
The depth
on the relationship chain defined by the given component
.
Example
Query for all entities that have a Position
and are (directly or indirectly) owned by an entity with Name
"Alice".
runQuery([Has(Position), ProxyRead(OwnedByEntity, Number.MAX_SAFE_INTEGER), HasValue(Name, { name: "Alice" })]);
#
Parameters
#
Returns
ProxyReadQueryFragment
query fragment to be used in
#
Defined in
#
clearLocalCache
▸ clearLocalCache(component
, uniqueWorldIdentifier?
): void
#
Parameters
#
Returns
void
#
Defined in
#
componentValueEquals
▸ componentValueEquals<S
, T
>(a?
, b?
): boolean
Compare two a
can be a partial component value, in which case only the keys present in a
are compared to the corresponding keys in b
.
Example
componentValueEquals({ x: 1, y: 2 }, { x: 1, y: 3 }) // returns false because value of y doesn't match
componentValueEquals({ x: 1 }, { x: 1, y: 3 }) // returns true because x is equal and y is not present in a
#
Type parameters
#
Parameters
#
Returns
boolean
True if a
equals b
in the keys present in a or neither a
nor b
are defined, else false.
#
Defined in
#
createEntity
▸ createEntity(world
, components?
, options?
): EntityIndex
Register a new entity in the given
#
Parameters
#
Returns
EntityIndex
index of this entity in the
#
Defined in
#
createIndexer
▸ createIndexer<S
, M
, T
>(component
): Indexer
S
, M
, T
>
Create an indexed component from a given component.
Remarks
An indexed component keeps a "reverse mapping" from
Dev
This could be made more (memory) efficient by using a hash of the component value as key, but would require handling hash collisions.
#
Type parameters
#
Parameters
#
Returns
Indexer
S
, M
, T
>
Indexed version of the component.
#
Defined in
#
createLocalCache
▸ createLocalCache<S
, M
, T
>(component
, uniqueWorldIdentifier?
): Component
<S
, M
, T
>
#
Type parameters
#
Parameters
#
Returns
Component
<S
, M
, T
>
#
Defined in
#
createWorld
▸ createWorld(): Object
Create a new World.
Remarks
A World is the central object of an ECS application, where all
#
Returns
Object
A new World
#
Defined in
#
defineComponent
▸ defineComponent<S
, M
, T
>(world
, schema
, options?
): Component
<S
, M
, T
>
Components contain state indexed by entities and are one of the fundamental building blocks in ECS. Besides containing the state, components expose an rxjs update$ stream, that emits an event any time the value of an entity in this component is updated.
Remarks
Components work with
Example
const Position = defineComponent(world, { x: Type.Number, y: Type.Number }, { id: "Position" });
#
Type parameters
#
Parameters
#
Returns
Component
<S
, M
, T
>
Component object linked to the provided World
#
Defined in
#
defineComponentSystem
▸ defineComponentSystem<S
>(world
, component
, system
, options?
): void
Create a system that is called every time the given component is updated.
#
Type parameters
#
Parameters
#
Returns
void
#
Defined in
#
defineEnterQuery
▸ defineEnterQuery(fragments
, options?
): Observable
<ComponentUpdate
Define a query object that only passes update events of type UpdateType.Enter to the update$
stream.
See
#
Parameters
#
Returns
Observable
<ComponentUpdate
Stream of component updates of entities matching the query for the first time
#
Defined in
#
defineEnterSystem
▸ defineEnterSystem(world
, query
, system
, options?
): void
Create a system that is called on every event of the given
#
Parameters
#
Returns
void
#
Defined in
#
defineExitQuery
▸ defineExitQuery(fragments
, options?
): Observable
<ComponentUpdate
Define a query object that only passes update events of type UpdateType.Exit to the update$
stream.
See
#
Parameters
#
Returns
Observable
<ComponentUpdate
Stream of component updates of entities not matching the query anymore for the first time
#
Defined in
#
defineExitSystem
▸ defineExitSystem(world
, query
, system
, options?
): void
Create a system that is called on every event of the given
#
Parameters
#
Returns
void
#
Defined in
#
defineQuery
▸ defineQuery(fragments
, options?
): Object
Create a query object including an update$ stream and a Set of entities currently matching the query.
Remarks
update$
stream needs to be subscribed to in order for the logic inside the stream to be executed and therefore
in order for the matching
set to be updated.
defineQuery
should be strongly preferred over runQuery
if the query is used for systems or other
use cases that repeatedly require the query result or updates to the query result. defineQuery
does not
reevaluate the entire query if an accessed component changes, but only performs the minimal set of checks
on the updated entity to evaluate wether the entity still matches the query, resulting in significant performance
advantages over runQuery
.
The query fragments are executed from left to right and are concatenated with a logical AND
.
For performance reasons, the most restrictive query fragment should be first in the list of query fragments,
in order to reduce the number of entities the next query fragment needs to be checked for.
If no proxy fragments are used, every entity in the resulting set passes every query fragment.
If setting fragments are used, the order of the query fragments influences the result, since settings only apply to
fragments after the setting fragment.
#
Parameters
#
Returns
Object
Query object: { update$: RxJS stream of updates to the query result. The update contains the component update that caused the query update, as well as the update type. matching: Mobx observable set of entities currently matching the query. }.
#
Defined in
#
defineRxSystem
▸ defineRxSystem<T
>(world
, observable$
, system
): void
Create a system that is called on every update of the given observable.
Remarks
Advantage of using this function over directly subscribing to the RxJS observable is that the system is registered in the world
and
disposed when the world
is disposed (eg. during a hot reload in development).
#
Type parameters
#
Parameters
#
Returns
void
#
Defined in
#
defineSyncSystem
▸ defineSyncSystem<T
>(world
, query
, component
, value
, options?
): void
Create a system to synchronize updates to one component with another component.
#
Type parameters
#
Parameters
#
Returns
void
#
Defined in
#
defineSystem
▸ defineSystem(world
, query
, system
, options?
): void
Create a system that is called on every event of the given
#
Parameters
#
Returns
void
#
Defined in
#
defineUpdateQuery
▸ defineUpdateQuery(fragments
, options?
): Observable
<ComponentUpdate
type
: UpdateType
}>
Define a query object that only passes update events of type UpdateType.Update to the update$
stream.
See
#
Parameters
#
Returns
Observable
<ComponentUpdate
type
: UpdateType
}>
Stream of component updates of entities that had already matched the query
#
Defined in
#
defineUpdateSystem
▸ defineUpdateSystem(world
, query
, system
, options?
): void
Create a system that is called on every event of the given
#
Parameters
#
Returns
void
#
Defined in
#
getChildEntities
▸ getChildEntities(entity
, component
, depth
): Set
<EntityIndex
Recursively compute all direct and indirect child entities up to the specified depth down the relationship chain defined by the given component.
#
Parameters
#
Returns
Set
<EntityIndex
Set of entities that are child entities of the given entity via the given component.
#
Defined in
#
getComponentEntities
▸ getComponentEntities<S
, T
>(component
): IterableIterator
<EntityIndex
Get a set of all entities of the given component.
#
Type parameters
#
Parameters
#
Returns
IterableIterator
<EntityIndex
Set of all entities in the given component.
#
Defined in
#
getComponentValue
▸ getComponentValue<S
, T
>(component
, entity
): ComponentValue
S
, T
> | undefined
Get the value of a given entity in the given component. Returns undefined if no value or only a partial value is found.
#
Type parameters
#
Parameters
#
Returns
ComponentValue
S
, T
> | undefined
Value of the given entity in the given component or undefined if no value exists.
#
Defined in
#
getComponentValueStrict
▸ getComponentValueStrict<S
, T
>(component
, entity
): ComponentValue
S
, T
>
Get the value of a given entity in the given component. Throws an error if no value exists for the given entity in the given component.
Remarks
Throws an error if no value exists in the component for the given entity.
#
Type parameters
#
Parameters
#
Returns
ComponentValue
S
, T
>
Value of the given entity in the given component.
#
Defined in
#
getEntitiesWithValue
▸ getEntitiesWithValue<S
>(component
, value
): Set
<EntityIndex
Get a set of entities that have the given component value in the given component.
#
Type parameters
#
Parameters
#
Returns
Set
<EntityIndex
Set with
#
Defined in
#
getEntityComponents
▸ getEntityComponents(world
, entity
): Component
[]
Get all components that have a value for the given entity.
Dev
Design decision: don't store a list of components for each entity but compute it dynamically when needed because there are less components than entities and maintaining a list of components per entity is a large overhead.
#
Parameters
#
Returns
Array of components that have a value for the given entity.
#
Defined in
#
hasComponent
▸ hasComponent<S
, T
>(component
, entity
): boolean
Check whether a component contains a value for a given entity.
#
Type parameters
#
Parameters
#
Returns
boolean
true if the component contains a value for the given entity, else false.
#
Defined in
#
isArrayType
▸ isArrayType(t
): t is ArrayType
#
Parameters
#
Returns
t is ArrayType
#
Defined in
#
isComponentUpdate
▸ isComponentUpdate<S
>(update
, component
): update is ComponentUpdate<S, undefined>
Type guard to infer the TypeScript type of a given component update
#
Type parameters
#
Parameters
#
Returns
update is ComponentUpdate<S, undefined>
True (+ infered type for update
) if update
belongs to component
. Else false.
#
Defined in
#
isEntityType
▸ isEntityType(t
): t is EntityType
#
Parameters
#
Returns
t is EntityType
#
Defined in
#
isFullComponentValue
▸ isFullComponentValue<S
>(component
, value
): value is ComponentValue<S, undefined>
Helper function to check whether a given component value is partial or full.
#
Type parameters
#
Parameters
#
Returns
value is ComponentValue<S, undefined>
#
Defined in
#
isIndexer
▸ isIndexer<S
>(c
): c is Indexer<S, Metadata, undefined>
Helper function to check whether a given component is indexed.
#
Type parameters
#
Parameters
#
Returns
c is Indexer<S, Metadata, undefined>
#
Defined in
#
isNumberType
▸ isNumberType(t
): t is NumberType
#
Parameters
#
Returns
t is NumberType
#
Defined in
#
isOptionalType
▸ isOptionalType(t
): t is OptionalType
#
Parameters
#
Returns
t is OptionalType
#
Defined in
#
namespaceWorld
▸ namespaceWorld(world
, namespace
): Object
Create a new namespace from an existing World.
The dispose
method of a namespaced World only calls disposers registered on this namespace.
#
Parameters
#
Returns
Object
World with a new namespace.
#
Defined in
#
overridableComponent
▸ overridableComponent<S
, M
, T
>(component
): OverridableComponent
S
, M
, T
>
An overridable component is a mirror of the source component, with functions to lazily override specific entity values. Lazily override means the values are not actually set to the source component, but the override is only returned if the value is read.
- When an override for an entity is added to the component, the override is propagated via the component's
update$
stream. - While an override is set for a specific entity, no updates to the source component for this entity will be propagated to the
update$
stream. - When an override is removed for a specific entity and there are more overrides targeting this entity,
the override with the highest nonce will be propagated to the
update$
stream. - When an override is removed for a specific entity and there are no more overrides targeting this entity,
the non-overridden underlying component value of this entity will be propagated to the
update$
stream.
#
Type parameters
#
Parameters
#
Returns
OverridableComponent
S
, M
, T
>
overridable component
#
Defined in
#
removeComponent
▸ removeComponent<S
, M
, T
>(component
, entity
): void
Remove a given entity from a given component.
#
Type parameters
#
Parameters
#
Returns
void
#
Defined in
#
runQuery
▸ runQuery(fragments
, initialSet?
): Set
<EntityIndex
Execute a list of query fragments to receive a Set of matching entities.
Remarks
The query fragments are executed from left to right and are concatenated with a logical AND
.
For performance reasons, the most restrictive query fragment should be first in the list of query fragments,
in order to reduce the number of entities the next query fragment needs to be checked for.
If no proxy fragments are used, every entity in the resulting set passes every query fragment.
If setting fragments are used, the order of the query fragments influences the result, since settings only apply to
fragments after the setting fragment.
#
Parameters
#
Returns
Set
<EntityIndex
Set of entities matching the query fragments.
#
Defined in
#
setComponent
▸ setComponent<S
, T
>(component
, entity
, value
): void
Set the value for a given entity in a given component.
Example
setComponent(Position, entity, { x: 1, y: 2 });
#
Type parameters
#
Parameters
#
Returns
void
#
Defined in
#
toUpdate
▸ toUpdate<S
>(entity
, component
): ComponentUpdate
S
, undefined
> & { type
: UpdateType
}
Helper function to create a component update for the current component value of a given entity.
#
Type parameters
#
Parameters
#
Returns
ComponentUpdate
S
, undefined
> & { type
: UpdateType
}
Component update corresponding to the given entity, the given component and the entity's current component value.
#
Defined in
#
toUpdateStream
▸ toUpdateStream<S
>(component
): UnaryFunction
<Observable
<EntityIndex
Observable
<ComponentUpdate
S
, undefined
> & { type
: UpdateType
}>>
Helper function to turn a stream of
#
Type parameters
#
Parameters
#
Returns
UnaryFunction
<Observable
<EntityIndex
Observable
<ComponentUpdate
S
, undefined
> & { type
: UpdateType
}>>
Unary function to be used with RxJS that turns stream of
#
Defined in
#
updateComponent
▸ updateComponent<S
, T
>(component
, entity
, value
, initialValue?
): void
Update the value for a given entity in a given component while keeping the old value of keys not included in the update.
Remarks
This function fails silently during runtime if a partial value is set for an entity that does not have a component value yet, since then a partial value will be set in the component for this entity.
Example
updateComponent(Position, entity, { x: 1 });
#
Type parameters
#
Parameters
#
Returns
void
#
Defined in
#
withValue
▸ withValue<S
, T
>(component
, value
): [Component
<S
, Metadata
T
>, ComponentValue
S
, T
>]
Util to create a tuple of a component and value with matching schema. (Used to enforce Typescript type safety.)
#
Type parameters
#
Parameters
#
Returns
[Component
<S
, Metadata
T
>, ComponentValue
S
, T
>]
Tuple [component, value]