Skip to content

World Events ​

IWorldEventListener receives every entity and component change in a world.

csharp
class MyListener : IWorldEventListener {
    public void OnEntityCreated(int entityIndex) { }
    public void OnEntityDestroyed(int entityIndex) { }
    public void OnComponentAdded(int entityIndex, int typeIndex) { }
    public void OnComponentRemoved(int entityIndex, int typeIndex) { }
}
csharp
var listener = new MyListener();
world.AddEventListener(listener);
world.RemoveEventListener(listener);

Order and guarantees ​

  • OnEntityCreated fires after the first component was added (CreateEntity) or after all components were copied (CopyEntity); OnComponentAdded for that first component fires before it. When a listener of one of those components destroys the entity, OnEntityCreated does not fire for it (up to 2.2.1 it did, for a dead entity or for the one a listener created in its slot), and CopyEntity copies nothing more. Since 2.5.0 such an entity gets no OnEntityDestroyed either.
  • OnComponentRemoved fires while the entity is still alive. If it was the last component, auto-destroy (OnEntityDestroyed) follows.
  • OnEntityDestroyed fires before components are removed; the entity is already dead (IsAlive false) but its components are still readable. They are removed one by one, and a listener of each removal still sees the components not removed yet, with Has as with Get (up to 2.2.1 Has said false for them while they were still readable). A listener that calls World.Clear ends the destroy: Clear has removed everything.
  • A listener may undo the change it is told about: remove the component again in OnComponentAdded (or destroy the entity), add it back in OnComponentRemoved, destroy the entity in OnEntityCreated. Every listener hears the changes of an entity in pairs and in order (created and destroyed, a component added and removed), and the last call it gets matches the world. A change nested in the notification of another change of the same entity, made directly or from a callback further in (a filter listener, a listener of another entity's change), goes only to the listeners that heard of that change, and the notification ends once its change is undone: the listeners after the one that undid it hear of neither.
  • An Add, a Remove, CreateEntity and CopyEntity count from the start, before the filters are updated. A filter listener that removes the component again while it is being added (or destroys the entity), or adds it back while it is being removed, tells world listeners nothing: they hear of neither the change nor the undoing. An entity that a listener of one of its components destroys while CreateEntity or CopyEntity builds it gets no OnEntityDestroyed, as it had no OnEntityCreated; neither does a world listener subscribed while the entity was being made. This holds for every entity still being created, the outer ones of nested creations included (a listener of a component of an entity being made creates another, whose listener destroys the first). A NextTick or a SystemsRunner.Run that a listener calls while entities are being created does not end their creation: they stay unannounced until their OnEntityCreated. An entity whose CreateEntity or CopyEntity throws before it is announced gets neither OnEntityCreated nor OnEntityDestroyed (until the next NextTick outside a runner, or the end of the outermost SystemsRunner.Run), with or without world listeners when the creation began. A snapshot loaded before then (a rollback: Clear, then WorldSerializer.Load), also by a listener in the middle of nested creations, drops what the world remembers of such entities: an entity to which the restored generations give the same handle again is announced, and gets its OnEntityDestroyed, like any other. A Clear or a Load that a listener calls ends what is still running about the entities it removes, since a snapshot may give their handles to restored entities, which it brings without events: a CreateEntity or CopyEntity in progress announces nothing and copies nothing more (also when an AutoCopy made the rollback: the value it returns is stored nowhere; and a copy that then fails destroys nothing), and the listeners not told yet of a change in progress are not told of it. To a world listener subscribed while an entity is being created, the entity's lifecycle is absent until its OnEntityCreated, while its components follow the general rule: it hears the component changes that happen after it subscribed.
  • Listeners never change which slot or handle an entity gets. A listener may destroy the entity whose component's removal is being told and create another that takes its slot (destroy and spawn a replacement): the listeners not told of that removal yet hear it before the new entity's events, which go to every listener. Events carry only the slot index, so this order is what tells the old entity from the new one.
  • Listeners may add or remove listeners during dispatch. A listener hears the changes that happen after it subscribed: one subscribed during a notification (also a world listener that a filter listener subscribes while the filters are updated) is not told of that change, but of the changes after it, nested ones included. OnEntityCreated goes to the listeners subscribed when it is sent, also to one that a listener of the new entity's components subscribed while they were added. A dispatch goes on over the listeners it chose when it began: one that an earlier listener unsubscribes is still called for that change, but hears no change nested in it. Listeners may modify the world, including the entity being destroyed.
  • Warmup and Clear fire no events. Neither does WorldSerializer.Load. The entities a snapshot restores and the ones a listener creates during Warmup are there without an OnEntityCreated, like entities that existed before a listener subscribed: their destroy is told. An entity a listener destroys during Warmup gets no OnEntityDestroyed (events are suppressed for the whole call, see Warmup).

Changed in 2.5.0

This changes what 2.2.1 and 2.2.2 documented. Up to 2.4.0 a filter listener that removed the component again while it was being added told world listeners of the removal alone (no OnComponentAdded), one that removed it and added it back at once told them of the removal and then of two additions, and one that added the component back while it was being removed told them of the addition alone. Among world listeners, the ones after a listener that undid the change heard the two events in reverse order, and an entity destroyed while it was being created got an OnEntityDestroyed without an OnEntityCreated. A world listener that a filter listener subscribed during an Add or a Remove got that change's event, and could hear it twice. A listener that destroyed an entity while its component's removal was being told and created a replacement in its slot cut that notification short: the listeners after it never heard the removal, and then heard the new entity's component come.

Fixed in 2.5.1

In 2.5.0 a NextTick outside a runner or a SystemsRunner.Run called by a listener in the middle of a creation made the world forget the entities still being created: a world listener subscribed since was told the destroy of such an entity, and after a rollback an entity that got the handle of one of them was taken for it (its OnEntityDestroyed was not sent). A creation that threw while world listeners were subscribed left its entity unrecorded, and its destroy was told. A creation, a copy or a notification still running when a listener rolled the world back went on with the restored entity that got the same handle: it sent that entity a second OnEntityCreated or told the listeners left a change it no longer had, copied components into it or destroyed it. An AutoCopy that destroyed the copy, cleared the world or rolled it back still had its value stored, on the freed slot or on the restored entity.

Known limitation

Listeners are told apart by reference. An object that is unsubscribed and subscribed again during one notification counts as the same subscription: it is treated as if it had stayed subscribed, not as a listener subscribed after the change.

Exceptions

If a listener throws, the exception propagates after the operation is brought to a consistent state. In the middle of an Add or Remove, every filter is brought to the entity's current components (without notifying filter listeners), and an entity that lost its last component is destroyed, which reports OnEntityDestroyed; the listeners not called yet are not called for that change. Up to 2.4.0 the filters not updated yet kept their old membership and such an entity stayed alive without components. The first exception is the one that leaves: a world listener that throws again in the OnEntityDestroyed of that destroy does not replace it, and the entity is destroyed all the same.

A listener that throws in OnEntityCreated is told about an entity that is complete: the exception leaves CreateEntity or CopyEntity, the listeners after it are not called, and the entity stays alive with all its components, announced (its OnEntityDestroyed goes to every listener) (the copy too: only a failure while the components are copied destroys it, see Entities). The caller gets no handle, so such an entity is reached through filters.

During DestroyEntity, the entity slot is released. Components whose removal had not run yet stay orphaned in their pools; the released slot leaves every filter and owning group and is not reused until Clear, so a new entity never meets those components. Treat the exception as fatal for that world or Clear() it: WorldSerializer.Load refuses a world with such components until it is cleared. Up to 2.4.0 the slot stayed in the filters of those components, and an owning group kept counting it as a member.

Type indices ​

typeIndex resolves via ComponentType.TypeOf(typeIndex) / ComponentType.NameOf(typeIndex):

csharp
class ComponentLog : IWorldEventListener {
    public void OnEntityCreated(int entityIndex) { }
    public void OnEntityDestroyed(int entityIndex) { }

    public void OnComponentAdded(int entityIndex, int typeIndex) {
        Console.WriteLine($"+{ComponentType.NameOf(typeIndex)} on {entityIndex}");
    }

    public void OnComponentRemoved(int entityIndex, int typeIndex) {
        Type type = ComponentType.TypeOf(typeIndex);
        Console.WriteLine($"-{type.Name} on {entityIndex}");
    }
}

For hooks on a single component type, component listeners avoid the world-wide dispatch.