EntityCommands

View source

Builds entity changes for deferred application to a world.

Start an operation with EntityCommands.entity() to select an existing entity or with EntityCommands.spawn() to allocate one. Add components with EntityCommands.insert() or EntityCommands.insertPrefab(), then call EntityCommands.build() to add the buffered insertion to the world’s deferred queue. The world applies the queued change later. spawn() returns a handle immediately, before its components are inserted.

Calling entity() or spawn() starts a new buffered operation and replaces any unfinished operation. clone(), remove(), despawn(), and reparent() add their changes to the deferred queue immediately.

Examples

import { EntityCommands } from '@wimaengine/commands'
import { Position2D } from '@wimaengine/transform'

class Selectable {}

// `world` must already support deferred entity changes.
const commands = new EntityCommands(world)
const entity = commands
  .spawn()
  .insert(new Position2D())
  .build()

commands.entity(entity).insert(new Selectable()).build()
// The world applies the queued changes later.

Constructor


Properties


Methods


entity(entity: EntityHandle): EntityCommands

View source

Selects an existing entity for a buffered component insertion.

This stores the handle without changing the entity and replaces any unfinished buffered operation. Call insert() or insertPrefab(), then build() to queue the insertion.

Parameters

NameDescriptionDefault
entity

Existing entity to target.

—

Returns

This builder.

spawn(): EntityCommands

View source

Allocates an empty entity immediately and starts buffering components for it.

Components are not inserted until build() queues the operation and the world applies it. Calling this again replaces an unfinished buffered operation; a handle allocated by the previous call remains allocated but is no longer buffered.

Returns

This builder.

spawnBatch<T extends unknown[]>(batch: [...T[]][]): EntityHandle[]

View source

Spawns one entity for each component tuple in batch.

For each tuple, this calls spawn(), appends the tuple with insertPrefab(), and calls build(). Handles are allocated immediately; their component insertions are queued in batch order and applied later. An empty batch returns an empty array and leaves the current buffered operation unchanged.

Type Parameters

T extends unknown[]

Parameters

NameDescriptionDefault
batch

Component tuples for the new entities.

—

Returns

The newly allocated handles in batch order.

build(): EntityHandle

View source

Queues the current buffered entity insertion.

The insertion is added to the world’s deferred queue, then the buffered operation is cleared and its entity handle is returned. Calling this method without first calling entity() or spawn() throws an assertion error.

Returns

The allocated or selected entity handle.

insert<T>(component: T): EntityCommands

View source

Adds one component to the current buffered entity insertion.

This does not queue or apply a change by itself. It must follow entity() or spawn() and be followed by build(). Without a buffered operation, the method throws an assertion error.

Type Parameters

T

Parameters

NameDescriptionDefault
component

Component instance to add.

—

Returns

This builder.

set(componentType: ConstructorFn, value: unknown, path?: (string | number)[]): EntityCommands

View source

Sets a component or one value inside an existing component.

An empty path replaces the component. Non-empty paths address nested live values, for example ['x'] or ['transform', 'position', 'x'].

Parameters

NameDescriptionDefault
componentType——
value——
path?—[]

insertPrefab<T extends unknown[]>(components: [...T[]]): EntityCommands

View source

Adds all components in a tuple to the current buffered entity insertion.

This is equivalent to calling insert() for each value, preserving tuple order. It does not queue or apply the change until build() is called.

Type Parameters

T extends unknown[]

Parameters

NameDescriptionDefault
components

Component instances to add in order.

—

Returns

This builder.

clone(entity: EntityHandle): EntityHandle

View source

Queues a clone of an entity and its descendants.

Clone handles are allocated immediately, while component insertion is deferred. Components with registered clone behavior are copied, and hierarchy references are remapped for the clone. The source entity must exist.

Parameters

NameDescriptionDefault
entity

Root entity to clone.

—

Returns

The cloned root entity handle.

remove<T extends unknown[]>(entity: EntityHandle, components: TupleConstructor<T>): void

View source

Queues removal of component types from an entity.

The component constructors are used when the deferred change is applied. A stale entity handle has no effect.

Type Parameters

T extends unknown[]

Parameters

NameDescriptionDefault
entity

Entity whose components will be removed.

—
components

Component constructors to remove.

—

despawn(entity: EntityHandle): void

View source

Queues an entity and its components for removal.

The entity is removed when the deferred change is applied. A stale entity handle has no effect.

Parameters

NameDescriptionDefault
entity

Entity to remove.

—

reparent(entity: EntityHandle, parent?: EntityHandle): void

View source

Queues a reparent operation for an entity.

The change is applied later. When complete 2D or 3D transform component sets are present, the entity keeps its world-space transform under the new parent. Passing undefined detaches the entity. A parent that is the entity itself or one of its descendants fails when the change is applied.

Parameters

NameDescriptionDefault
entity

Entity to reparent.

—
parent?

New parent, or undefined to detach the entity.

undefined