Skip to content

Interface IWorldEventListener ​

Namespace: KenseiECS

Listener for World lifecycle events. Register via world.AddEventListener().

Usage:

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) { }
}
world.AddEventListener(new MyListener());
csharp
public interface IWorldEventListener

Remarks ​

Every listener hears the changes of an entity alternately (created and destroyed, a component added and removed), in the order they happen, and the last call it got matches the world. A listener may undo the change it is told about: remove the component again in OnComponentAdded, add it back in OnComponentRemoved, destroy the entity in OnEntityCreated. The nested change (also one made further in, by a filter listener or from a callback of another entity's change) is told only to the listeners that heard of the change it undoes, and the listeners after the one that undid it hear of neither. An Add, a Remove, CreateEntity and CopyEntity count as heard of by no world listener until the filters are updated (until the components are in place): a filter listener, or a listener of the new entity's component, that undoes them tells the world listeners nothing, neither the change nor the undoing (no OnEntityDestroyed for an entity whose OnEntityCreated never came). A listener hears the changes that happen after it subscribed: one subscribed during a notification (also by a filter listener while the filters are updated) is not told of that change, but of the changes after it, nested ones included. OnEntityCreated is the exception: it goes to the listeners subscribed when it is sent; a listener that would get it but does not, because a listener destroyed the entity while it was made, gets no OnEntityDestroyed for it either. Listeners never change which slot or handle an entity gets: when a listener destroys an entity whose component's removal is still being told and creates another that takes its slot, the listeners not told yet hear that removal before the new entity's events.

Methods ​

OnComponentAdded(int, int) ​

Called after a component is added and filters are updated. typeIndex is the component type index. A listener that removes the component again (or destroys the entity) ends the Add for the listeners after it: they hear of neither the Add nor the removal.

csharp
void OnComponentAdded(int entityIndex, int typeIndex)

Parameters ​

entityIndex int

typeIndex int

OnComponentRemoved(int, int) ​

Called after a component is removed and filters are updated. If it was the last component of a live entity, the entity is destroyed after the dispatch, also when a listener throws (an exception thrown from the OnEntityDestroyed of that destroy is then dropped: the first one is reported). A listener that adds the component back ends the removal for the listeners after it: they hear of neither the removal nor the new component.

csharp
void OnComponentRemoved(int entityIndex, int typeIndex)

Parameters ​

entityIndex int

typeIndex int

OnEntityCreated(int) ​

Called after an entity is created and its initial components are added. A listener that destroys the entity ends the dispatch: the listeners after it hear of neither the creation nor the destroy. A listener that throws ends it too, and the entity stays alive and complete, for CreateEntity and for CopyEntity alike.

csharp
void OnEntityCreated(int entityIndex)

Parameters ​

entityIndex int

OnEntityDestroyed(int) ​

Called when an entity is destroyed: it is already marked dead, but its components are not yet removed.

csharp
void OnEntityDestroyed(int entityIndex)

Parameters ​

entityIndex int