Skip to content

OneFrame Components ​

A one-frame component is an event: it is added during the frame and removed automatically at the end of the runner's Run.

csharp
struct DamageEvent : IComponent { public float Value; }

Register the type on the runner:

csharp
systems.OneFrame<DamageEvent>();
csharp
// In a system — create event
world.Add(entity, new DamageEvent { Value = 10 });
// All systems later in the pipeline see it this frame
// Removed automatically at the end of Run()

Ordering ​

OneFrame<T> removes at the end of Run, so a producer must run before its consumers; an event created after the consumer ran is removed unseen. The cleanup runs even when a system throws.

Use DelHere<T>() to put the cleanup at a specific point of the pipeline. Systems registered before it see the components; systems after it do not.

csharp
var systems = new SystemsRunner(world)
    .Add(new DamageSystem())          // sees DamageEvent
    .DelHere<DamageEvent>()           // removes every DamageEvent right here
    .Add(new SpawnSystem());          // does not see it

Nested runners

Each runner cleans its own OneFrame components at the end of its own Run. See nested runners. By default a paused runner — a group whose slot is disabled, or a disabled phase — skips that cleanup, so its one-frame components outlive the frame while it is paused. Register the type with OneFrame<T>(cleanWhenDisabled: true) to remove it in the paused frames too: at the skipped group's slot in the parent's pipeline, or in Run() of the disabled phase. See disabled runners.

Event entities ​

An entity whose only component is a one-frame component is auto-destroyed by the cleanup, which makes "event entities" free.

csharp
// An entity whose only component is the event:
// the cleanup removes the component, the empty entity is destroyed with it.
world.CreateEntity(new DamageEvent { Value = 5 });

Several events per entity per frame ​

A plain component holds one value per entity, and Add throws on the second one. EventBuffer<T> collects any number:

csharp
world.AddEvent(target, new Hit { Amount = 10 });
world.AddEvent(target, new Hit { Amount = 5 });

AddEvent appends to the entity's EventBuffer<T>, adding the component on the first call. T is any struct; it does not have to implement IComponent.

csharp
class HitSystem : IInitSystem, IRunSystem {
    Filter _hitTargets;
    ComponentPool<EventBuffer<Hit>> _hits;
    ComponentPool<Health> _health;

    public void Init(World world, SharedData shared) {
        _hitTargets = world.Filter().Inc<EventBuffer<Hit>>().Inc<Health>().End();
        _hits = world.Pool<EventBuffer<Hit>>();
        _health = world.Pool<Health>();
    }

    public void Run(World world) {
        foreach (int e in _hitTargets) {
            var hits = _hits.Get(e).Values;
            for (int i = 0; i < hits.Count; i++) {
                _health.Get(e).Value -= hits[i].Amount;
            }
        }
    }
}
csharp
// Required: nothing else removes the buffers. Their lists are pooled and reused.
systems.OneFrame<EventBuffer<Hit>>();

Register the buffer type

AddEvent adds the EventBuffer<T> component, and nothing removes it unless the type is registered with OneFrame<EventBuffer<T>>() (or DelHere). Without the registration the events of earlier frames stay in Values: every consumer reads them again each frame (a hit of 10 added every frame takes 10, then 20, then 30) and the list keeps growing. Nothing reports it, in release or under KENSEI_DEBUG. A plain one-frame component fails loudly in the same situation: its Add in the next frame throws because the component is still there.

The Values lists are pooled: removing the component returns its list to the pool, so a OneFrame<EventBuffer<T>> registration stops allocating once the pool has reached its peak — as many lists as the most entities that held a buffer of that type at once, each grown to the most events one entity received.

Copies of a buffer share its list

EventBuffer<T> is a struct whose Values field is the pooled list, so a copy (var b = pool.Get(e)) is a second handle to the entity's list: b.Add(x) changes the entity's buffer at once. Once the component is removed — OneFrame cleanup (also a nested runner's, before the parent's CommandBuffer.Playback), DelHere, Remove, DestroyEntity, Clear — the list is back in the pool and the copy is stale: its events are gone, and Add on it writes into a list the pool may already have handed to another entity's buffer. 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 the list, and a stale copy, or a copy of another entity's buffer, is stored with an empty list of its own instead (Count is 0, Values is not null) — or, when it overwrites the entity's buffer, the buffer is left as it is. world.AddEvent records the entity as the holder of every list it rents, also for a buffer the entity has without a list (added as new EventBuffer<T>(), or set to default; Values is null in such a buffer until its first event). A new buffer filled before it is stored (var b = new EventBuffer<T>(); b.Add(x); cb.Add(e, b);) keeps its list until it is: every rent of a pooled list is numbered, and the buffer that rented it records the number, so a stale copy stored meanwhile that holds the same list (back from the pool and rented again) is told apart from it and gets the empty list (in 2.2.1 both entities ended up with the list). Writing a copy straight into the pool's storage (Get(e) = copy, RawData) bypasses that check, and so does pool.Get(e).Add(x) on a buffer without a list: the list it rents has no recorded holder, and a copy of that buffer can give it to a second entity. Don't do either. Add events with world.AddEvent, and read Values while the component is there.

Prewarming the list pool ​

World.Warmup does not fill the pool: it adds a default EventBuffer<T>, whose Values is null, so it rents no list. Without prewarming, a frame with more buffered entities than ever before allocates new lists, and a list that receives more events than its capacity grows. EventBuffer<T>.Prewarm(lists, capacity) fills the pool up front:

csharp
// At load: room for every enemy to receive up to 8 hits in one frame.
// The pool is static per event type, so this covers every world.
EventBuffer<Hit>.Prewarm(maxAliveEnemies, 8);

Afterwards the next lists lists the pool hands out have Capacity of at least capacity: free lists already that large are used first, then the largest of the remaining free lists are grown, and missing ones are created. At most lists lists are grown, so a pool that once peaked at many more lists is not inflated as a whole. A repeated call with the same arguments does nothing. Choosing the lists takes up to O(lists × free lists) steps — each prepared slot may scan the free lists below it — which is nothing at load time but adds up for thousands of free lists, so do not call it per frame. Lists rented by live buffers are not counted, so call it at load, before the first frame. With the pool prewarmed for the peak, a OneFrame<EventBuffer<T>> frame allocates nothing as long as no more than lists entities hold a buffer and none receives more than capacity events; past that the pool grows on demand as before.

The pool is static and shared

The pool is one per event type T, static, and shared by every world in the process: one Prewarm covers all worlds, and they compete for the same lists. It is not thread-safe. Lists come back in last-in, first-out order, so with lists of mixed capacity a small one can still be handed to an entity that receives many events and grow again. Prewarm puts the lists it prepared on top, so they are handed out first after the call.

Replacing a warmup that created entities

Prewarm fills only the list pool. A warmup that spawned a crowd of entities with buffers also grew the world's entity storage and the pools and filters along the way; to keep that, add world.ReserveEntities(entities) and Pool<T>().Reserve(count) for those types next to the Prewarm call.