Skip to content

Struct EventBuffer<T> ​

Namespace: KenseiECS

Component holding several events of type T for one entity within a frame, for cases where a plain OneFrame component (one per entity) is not enough. Lists are pooled: AutoReset returns the list on remove (OneFrame cleanup, DestroyEntity, World.Clear), so once the pool holds as many lists as are alive at the peak, a OneFrame<EventBuffer<T>> registration allocates nothing. World.Warmup does not fill this pool: it adds buffers without lists; call Prewarm() for that. The pool is static, one per event type, and shared by all worlds in the process. CopyEntity gives the copy its own list (IAutoCopy).

A copy of the struct (var b = pool.Get(e)) shares the entity's list: Add() on it changes the entity's buffer at once, and once the component is removed (OneFrame cleanup, DelHere, Remove, DestroyEntity, Clear) the copy is stale: its list is back in the pool and may already belong to another entity's buffer, so do not use it any more. A pooled list is held by one entity at most: storing a value through the pool (Add, CommandBuffer.Add/Set/CreateEntity, a snapshot load) checks it, and a value whose list went back to the pool or belongs to another entity is stored with an empty list of its own instead (an empty buffer, Values not null), or, when it replaces the entity's buffer, leaves that buffer as it is. world.AddEvent records the entity as the holder of every list it rents, also for a buffer the entity already has without a list. Every rent of a pooled list is numbered and the renting buffer keeps the number, so a new buffer filled before it is stored (new EventBuffer<T>(), then Add(), for a CommandBuffer.Add) keeps its list when a stale copy holding the same list is stored first. Writing a copy directly into the pool's storage (through a ref or RawData) bypasses the check, and so does pool.Get(e).Add(v) on a buffer without a list (Values null): the list it rents has no recorded holder, and a copy of that buffer can give it to a second entity. Use world.AddEvent to add events.

Usage:

csharp
world.AddEvent(entity, new DamageEvent { Value = 10 });
world.AddEvent(entity, new DamageEvent { Value = 5 });

foreach (int e in _damaged) {
    var events = _buffers.Get(e).Values;
    for (int i = 0; i < events.Count; i++) { ... }
}

systems.OneFrame<EventBuffer<DamageEvent>>();
csharp
public struct EventBuffer<T> : IComponent, IAutoReset<EventBuffer<T>>, IAutoCopy<EventBuffer<T>> where T : struct

Type Parameters ​

T

Implements ​

IComponent, IAutoReset<EventBuffer<T>>, IAutoCopy<EventBuffer<T>>

Inherited Members ​

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

Fields ​

Values ​

Recorded events, in insertion order. Null until the first Add and after AutoReset, so also in a buffer added without events (new EventBuffer<T>(), CommandBuffer.Set(e, default)); Count is 0 then.

csharp
public List<T> Values

Field Value ​

List<T>

Properties ​

Count ​

Number of recorded events; 0 when no list is rented.

csharp
public int Count { get; }

Property Value ​

int

Methods ​

Add(T) ​

Append an event. Rents a pooled list on first use.

csharp
public void Add(T value)

Parameters ​

value T

AutoCopy(ref EventBuffer<T>) ​

Give the copy its own pooled list with the same events.

csharp
public void AutoCopy(ref EventBuffer<T> c)

Parameters ​

c EventBuffer<T>

AutoReset(ref EventBuffer<T>) ​

Clear the list and return it to the pool, leaving Values null.

csharp
public void AutoReset(ref EventBuffer<T> c)

Parameters ​

c EventBuffer<T>

Prewarm(int, int) ​

Fill the list pool for event type T ahead of gameplay, so that up to lists entities can hold a buffer of up to capacity events each without allocating. Afterwards the next lists lists the pool hands out have Capacity of at least capacity: free lists that are already large enough are used first, then the largest remaining free lists are grown, and new lists are created only when the pool has fewer than lists free lists. At most lists lists are grown, so a pool that once peaked at many more free lists is not inflated. Lists currently rented by a buffer are not counted or touched, so call it while no buffers of this type exist (at load, before the first frame).

csharp
public static void Prewarm(int lists, int capacity)

Parameters ​

lists int

Minimum number of free lists in the pool.

capacity int

Minimum Capacity of each of those lists.

Remarks ​

The pool is static: one per event type, shared by every world in the process, so one call covers all worlds, and lists prewarmed for one world can be taken by another. Like the rest of the pool it is not thread-safe. Warmup() does not fill it. The call allocates the missing lists and their storage; repeating it with the same arguments allocates nothing. Choosing the lists takes O(lists × free lists) time in the worst case (each prepared slot may search the free lists below it), which is fine at load time but not for a pool of many thousands of free lists every frame.

Exceptions ​

ArgumentOutOfRangeException

When an argument is negative.