Skip to content

Class WorldListenerExtensions ​

Namespace: KenseiECS

Extension methods for listener management on World. Wraps Listeners<T> component — user doesn't interact with it directly.

Usage:

csharp
world.Subscribe<IDamageListener>(entity, view);
world.Unsubscribe<IDamageListener>(entity, view);
world.UnsubscribeLater<IDamageListener>(entity, this);   // from inside a callback
csharp
public static class WorldListenerExtensions

Inheritance ​

object ← WorldListenerExtensions

Inherited Members ​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Methods ​

CreateWithListener<T>(World, T) ​

Create a new entity with a Listeners<T> component and an initial listener. Shortcut for CreateEntity + Subscribe.

csharp
public static Entity CreateWithListener<T>(this World world, T listener) where T : class

Parameters ​

world World

listener T

Returns ​

Entity

Type Parameters ​

T

HasListeners<T>(World, Entity) ​

Check if entity has at least one listener of type T.

csharp
public static bool HasListeners<T>(this World world, Entity entity) where T : class

Parameters ​

world World

entity Entity

Returns ​

bool

Type Parameters ​

T

Subscribe<T>(World, Entity, T) ​

Subscribe a listener to an entity. Creates Listeners<T> component automatically if not present.

csharp
public static void Subscribe<T>(this World world, Entity entity, T listener) where T : class

Parameters ​

world World

entity Entity

listener T

Type Parameters ​

T

Unsubscribe<T>(World, Entity, T) ​

Unsubscribe a listener from an entity. The Listeners<T> component stays on the entity even when empty, so unsubscribing the last listener never auto-destroys the entity. Remove<Listeners<T>> explicitly to drop the component. Not from inside a callback of this list: the system walking it would skip or repeat a listener. Use UnsubscribeLater<T>() there.

csharp
public static void Unsubscribe<T>(this World world, Entity entity, T listener) where T : class

Parameters ​

world World

entity Entity

listener T

Type Parameters ​

T

UnsubscribeLater<T>(World, Entity, T) ​

Unsubscribe a listener at the end of the tick: the removal is queued and applied when the outermost Run() of this world returns, after its systems and OneFrame cleanup (also when a system threw). This is how a listener leaves from inside a callback, while a system iterates the list it is in, which must not change during that iteration. The listener stays in the list, and is called by the dispatches that run, until then.

csharp
public static void UnsubscribeLater<T>(this World world, Entity entity, T listener) where T : class

Parameters ​

world World

entity Entity

listener T

Type Parameters ​

T

Remarks ​

Removals are applied in the order they were queued, each as Unsubscribe<T>() would, to the list as it is then: a listener subscribed again in the same tick is removed too. One whose entity was destroyed by then (even if a new entity took the slot), or whose entity has no Listeners<T> any more, is skipped. Called while no runner of this world runs, the removal is applied at the next NextTick(), which the root runner's Run() calls before its systems (a world driven without a runner calls it itself). Clear() drops the queued removals, and on a destroyed world the call does nothing. A listener may call it while its entity's own DestroyEntity runs. The queue is kept per listener type and reused, so it allocates nothing once it reached its peak.

Pass the listener type explicitly (UnsubscribeLater<IDamageListener>(entity, this)): with this C# would infer the view's own class, and the removal would look for a Listeners of that class.