Class
EntityCommands
View sourceBuilds 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
View source
Methods
entity(entity: EntityHandle): EntityCommands
View sourceSelects 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.
spawn(): EntityCommands
View sourceAllocates 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.
spawnBatch<T extends unknown[]>(batch: [...T[]][]): EntityHandle[]
View sourceSpawns 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.
build(): EntityHandle
View sourceQueues 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.
insert<T>(component: T): EntityCommands
View sourceAdds 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.
set(componentType: ConstructorFn, value: unknown, path?: (string | number)[]): EntityCommands
View sourceSets 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'].
insertPrefab<T extends unknown[]>(components: [...T[]]): EntityCommands
View sourceAdds 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.
clone(entity: EntityHandle): EntityHandle
View sourceQueues 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.
remove<T extends unknown[]>(entity: EntityHandle, components: TupleConstructor<T>): void
View sourceQueues 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.
despawn(entity: EntityHandle): void
View sourceQueues an entity and its components for removal.
The entity is removed when the deferred change is applied. A stale entity handle has no effect.
reparent(entity: EntityHandle, parent?: EntityHandle): void
View sourceQueues 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.
