Skip to content

Component Listeners ​

Typed hooks on one pool, without world-wide dispatch:

csharp
class HealthHooks : IComponentListener<Health> {
    public void OnAdded(int entityIndex, ref Health c) => c.Value = Math.Min(c.Value, 100);
    public void OnRemoved(int entityIndex, ref Health c) { /* data still intact, AutoReset runs after */ }
}
csharp
var hooks = new HealthHooks();
world.Pool<Health>().AddListener(hooks);

// later
world.Pool<Health>().RemoveListener(hooks);
  • OnAdded runs after the component is stored and filters are updated. The ref points at the stored component, so the listener can adjust it.
  • OnRemoved runs before AutoReset, so the component data is still intact.
  • A listener may call its own pool again. From OnRemoved it may remove components of the same type, the one being removed included: by destroying its entity, or through a linked entity whose listener removes this one back. Every listener still gets one OnRemoved per component, and a cycle of entities that remove each other ends (up to 2.2.1 such a cycle recursed until the stack overflowed).
  • An OnAdded that removes the component again (or destroys the entity) ends the Add for the listeners after it: they get the OnRemoved and no OnAdded. Since 2.5.0 filter listeners and world listeners stop as well, and go further: the ones after a listener that undid the change hear of neither the change nor the undoing, so a later listener never sees a stale event.
  • A component that a listener removes and adds again, to the same entity or to a new entity that takes the freed slot, is a new component: the outer Remove leaves it alone, and an outer Add gives its remaining listeners no OnAdded for it, since the nested Add already notified them.
  • Listeners may subscribe and unsubscribe listeners of the pool. A typed listener hears the changes that happen after it subscribed, like filter listeners and world listeners: one that a filter or world listener of an Add subscribes during that Add gets no OnAdded for it (the component was stored before the listener subscribed) and starts with the next Add, whether or not the pool already had typed listeners. Up to 2.4.0 that depended on the pool: with a typed listener already subscribed the new one got the OnAdded, without one it did not (up to 2.2.1 it also got it when the pool tracked changes, belonged to an owning group or held EventBuffer<T>). A typed listener that a filter or world listener unsubscribes during the Add gets no OnAdded. Once the OnAdded dispatch has started, it goes over the listeners it started with: one unsubscribed by an earlier typed listener is still called, one subscribed is not. A removal notifies the listeners it started with.
  • During DestroyEntity the components go one by one, and OnRemoved of each still sees the ones not removed yet: world.Has and Get agree, as during a plain Remove (up to 2.2.1 Has said false for them, so if (!world.Has<B>(e)) world.Add(e, …) threw "already has component"). A listener that calls World.Clear ends the destroy (up to 2.2.1 the destroy went on and left the world with a negative entity count and an entity that was not alive).
  • OnAdded may destroy the entity, also one that CreateEntity, CopyEntity or EcsEntityView.Spawn is still building: the rest of its components are not added (up to 2.2.1 CopyEntity and Spawn put them into the freed slot, where the next entity inherited them), and the returned handle is dead.
  • Warmup is not hidden from these listeners: they see its temporary entity, with default(T) data, added and removed. A listener may destroy it (a death rule firing on the default Health): Warmup then goes on with a new temporary entity for the remaining types. See Warmup.

For notifications about every entity and component type, use world events. For the Unity-side pattern of notifying views from systems, see Listener Bridge.