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);OnAddedruns after the component is stored and filters are updated. Therefpoints at the stored component, so the listener can adjust it.OnRemovedruns beforeAutoReset, so the component data is still intact.- A listener may call its own pool again. From
OnRemovedit 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 oneOnRemovedper 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
OnAddedthat removes the component again (or destroys the entity) ends theAddfor the listeners after it: they get theOnRemovedand noOnAdded. 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
Removeleaves it alone, and an outerAddgives its remaining listeners noOnAddedfor it, since the nestedAddalready 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
Addsubscribes during thatAddgets noOnAddedfor it (the component was stored before the listener subscribed) and starts with the nextAdd, 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 theOnAdded, 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 heldEventBuffer<T>). A typed listener that a filter or world listener unsubscribes during theAddgets noOnAdded. Once theOnAddeddispatch 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
DestroyEntitythe components go one by one, andOnRemovedof each still sees the ones not removed yet:world.HasandGetagree, as during a plainRemove(up to 2.2.1Hassaidfalsefor them, soif (!world.Has<B>(e)) world.Add(e, …)threw "already has component"). A listener that callsWorld.Clearends 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). OnAddedmay destroy the entity, also one thatCreateEntity,CopyEntityorEcsEntityView.Spawnis still building: the rest of its components are not added (up to 2.2.1CopyEntityandSpawnput them into the freed slot, where the next entity inherited them), and the returned handle is dead.Warmupis not hidden from these listeners: they see its temporary entity, withdefault(T)data, added and removed. A listener may destroy it (a death rule firing on the defaultHealth):Warmupthen 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.
