World Events
IWorldEventListener receives every entity and component change in a world.
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) { }
}var listener = new MyListener();
world.AddEventListener(listener);
world.RemoveEventListener(listener);Order and guarantees
OnEntityCreatedfires after the first component was added (CreateEntity) or after all components were copied (CopyEntity);OnComponentAddedfor that first component fires before it. When a listener of one of those components destroys the entity,OnEntityCreateddoes 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), andCopyEntitycopies nothing more. Since 2.5.0 such an entity gets noOnEntityDestroyedeither.OnComponentRemovedfires while the entity is still alive. If it was the last component, auto-destroy (OnEntityDestroyed) follows.OnEntityDestroyedfires before components are removed; the entity is already dead (IsAlivefalse) 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, withHasas withGet(up to 2.2.1Hassaidfalsefor them while they were still readable). A listener that callsWorld.Clearends the destroy:Clearhas 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 inOnComponentRemoved, destroy the entity inOnEntityCreated. 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, aRemove,CreateEntityandCopyEntitycount 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 whileCreateEntityorCopyEntitybuilds it gets noOnEntityDestroyed, as it had noOnEntityCreated; 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). ANextTickor aSystemsRunner.Runthat a listener calls while entities are being created does not end their creation: they stay unannounced until theirOnEntityCreated. An entity whoseCreateEntityorCopyEntitythrows before it is announced gets neitherOnEntityCreatednorOnEntityDestroyed(until the nextNextTickoutside a runner, or the end of the outermostSystemsRunner.Run), with or without world listeners when the creation began. A snapshot loaded before then (a rollback:Clear, thenWorldSerializer.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 itsOnEntityDestroyed, like any other. AClearor aLoadthat 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: aCreateEntityorCopyEntityin progress announces nothing and copies nothing more (also when anAutoCopymade 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 itsOnEntityCreated, 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.
OnEntityCreatedgoes 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. WarmupandClearfire no events. Neither doesWorldSerializer.Load. The entities a snapshot restores and the ones a listener creates duringWarmupare there without anOnEntityCreated, like entities that existed before a listener subscribed: their destroy is told. An entity a listener destroys duringWarmupgets noOnEntityDestroyed(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):
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.
