Skip to content

Class CommandBuffer ​

Namespace: KenseiECS

Records structural changes and applies them later with Playback(world), in order. Use it to defer changes from inside a filter iteration or a nested loop, where destroying or modifying a not-yet-visited entity is unsafe.

Commands targeting an Entity that is dead at Playback time are skipped. Payloads are stored per component type without boxing; after the first frame the buffer allocates nothing.

Usage:

csharp
foreach (int e in filter) {
    if (hp.Get(e).Value <= 0) {
        buffer.DestroyEntity(world.GetEntity(e));
    }
}
buffer.Playback(world);
csharp
public sealed class CommandBuffer

Inheritance ​

object ← CommandBuffer

Inherited Members ​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Properties ​

Count ​

Number of recorded commands.

csharp
public int Count { get; }

Property Value ​

int

Methods ​

Add<T>(Entity, T) ​

Record Add. At Playback throws if the entity already has the component, like World.Add.

csharp
public void Add<T>(Entity entity, T component) where T : struct, IComponent

Parameters ​

entity Entity

component T

Type Parameters ​

T

Add<T>(PendingEntity, T) ​

Record Add on an entity created earlier in this buffer. At Playback throws if it already has the component.

csharp
public void Add<T>(PendingEntity entity, T component) where T : struct, IComponent

Parameters ​

entity PendingEntity

component T

Type Parameters ​

T

Exceptions ​

InvalidOperationException

Under KENSEI_DEBUG, when entity was returned by another buffer, is from before this buffer's last Playback or Clear, or is a default PendingEntity.

Clear() ​

Discard all recorded commands. The PendingEntity handles returned so far are no longer valid.

csharp
public void Clear()

CreateEntity<T>(T) ​

Record CreateEntity with one initial component. Returns a handle for further commands.

csharp
public PendingEntity CreateEntity<T>(T component) where T : struct, IComponent

Parameters ​

component T

Returns ​

PendingEntity

Type Parameters ​

T

DestroyEntity(Entity) ​

Record DestroyEntity. No-op at Playback if the entity is already dead.

csharp
public void DestroyEntity(Entity entity)

Parameters ​

entity Entity

DestroyEntity(PendingEntity) ​

Record DestroyEntity on an entity created earlier in this buffer. No-op at Playback if it is already dead.

csharp
public void DestroyEntity(PendingEntity entity)

Parameters ​

entity PendingEntity

Exceptions ​

InvalidOperationException

Under KENSEI_DEBUG, when entity was returned by another buffer, is from before this buffer's last Playback or Clear, or is a default PendingEntity.

Playback(World) ​

Apply all recorded commands to the world in order, then clear the buffer. If a command throws, the remaining commands are discarded. Commands that a listener records into this buffer while it is played back are applied by the same Playback, after the ones recorded before.

csharp
public void Playback(World world)

Parameters ​

world World

Remove<T>(Entity) ​

Record Remove. No-op at Playback if the component is absent.

csharp
public void Remove<T>(Entity entity) where T : struct, IComponent

Parameters ​

entity Entity

Type Parameters ​

T

Remove<T>(PendingEntity) ​

Record Remove on an entity created earlier in this buffer. No-op at Playback if the component is absent.

csharp
public void Remove<T>(PendingEntity entity) where T : struct, IComponent

Parameters ​

entity PendingEntity

Type Parameters ​

T

Exceptions ​

InvalidOperationException

Under KENSEI_DEBUG, when entity was returned by another buffer, is from before this buffer's last Playback or Clear, or is a default PendingEntity.

Set<T>(Entity, T) ​

Record Add-or-overwrite. An overwrite is a plain write that change tracking sees as a modification; AutoReset is not run on the old value, so a modified copy of the component that shares its references (a list) can be written back. Typed listeners see an Add only when the component was missing.

csharp
public void Set<T>(Entity entity, T component) where T : struct, IComponent

Parameters ​

entity Entity

component T

Type Parameters ​

T

Remarks ​

Do not use it to add events to an EventBuffer<T>: a copy taken with Get shares the entity's list, so Add on it changes the buffer at once, and if the buffer is removed before Playback (OneFrame cleanup, possibly of a nested runner, DelHere, Remove, DestroyEntity) the copy is stale and its events are gone. Set then stores an empty buffer, or keeps the entity's current one, instead of a list that went back to the pool. Call world.AddEvent directly, or record the event values and add them after Playback.

Set<T>(PendingEntity, T) ​

Record Add-or-overwrite on an entity created earlier in this buffer. Overwrites behave as in Set<T>().

csharp
public void Set<T>(PendingEntity entity, T component) where T : struct, IComponent

Parameters ​

entity PendingEntity

component T

Type Parameters ​

T

Exceptions ​

InvalidOperationException

Under KENSEI_DEBUG, when entity was returned by another buffer, is from before this buffer's last Playback or Clear, or is a default PendingEntity.