Skip to content

Struct Listeners<T> ​

Namespace: KenseiECS

Component that holds a list of listeners implementing interface T: the bridge from ECS to the views (MonoBehaviours) that want to hear about an entity. It is data only; a system calls the listeners itself:

csharp
// In system:
foreach (int e in _damageFilter) {
    var listeners = _listenersPool.Get(e).Values;
    for (int i = 0; listeners != null && i < listeners.Count; i++) {
        listeners[i].OnDamage(damage);
    }
}

A callback must not change the list it is called from (subscribe or unsubscribe on that entity, remove the component or destroy the entity): the loop would skip or repeat a listener. A listener that leaves from inside its own callback uses UnsubscribeLater<T>(), applied at the end of the tick. Subscribe with Subscribe<T>(). (foreach over the List turns a broken rule into an InvalidOperationException, but under IL2CPP it costs 1.5 to 3 times as much as the for loop.)

Removing the component (or destroying the entity) clears the list and drops it. CopyEntity gives the copy its own list.

csharp
public struct Listeners<T> : IComponent, IAutoReset<Listeners<T>>, IAutoCopy<Listeners<T>> where T : class

Type Parameters ​

T

Implements ​

IComponent, IAutoReset<Listeners<T>>, IAutoCopy<Listeners<T>>

Inherited Members ​

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

Fields ​

Values ​

Registered listeners, in subscription order. Null until the first Subscribe (and on a component added by hand without a list).

csharp
public List<T> Values

Field Value ​

List<T>

Properties ​

Count ​

Number of listeners.

csharp
public int Count { get; }

Property Value ​

int

Methods ​

Add(T) ​

Add a listener. Creates list if needed.

csharp
[Obsolete("Use World.Subscribe. Removed in 3.0.")]
public void Add(T listener)

Parameters ​

listener T

AutoCopy(ref Listeners<T>) ​

Give the copy its own list with the same listeners.

csharp
public void AutoCopy(ref Listeners<T> c)

Parameters ​

c Listeners<T>

AutoReset(ref Listeners<T>) ​

Clear the list and drop it, leaving Values null.

csharp
public void AutoReset(ref Listeners<T> c)

Parameters ​

c Listeners<T>

Invoke(Action<T>, Action<T, Exception>) ​

Call call for every listener, in registration order (first subscribed, first called). The listeners registered when Invoke starts are taken as a snapshot; each snapshot entry is called at most once, and only if it is still in the list when its turn comes: a listener removed from the list during the dispatch (by itself or another callback) before its turn is skipped. A listener subscribed during the dispatch is not called by it. Removing the component (or destroying the entity) during the dispatch clears the list, so the remaining listeners are skipped. Nothing when Values is null.

"Still in the list" is tested with the same comparison as [`Remove()`](KenseiECS.Listeners-1.md#KenseiECS_Listeners_1_Remove__0_) (`Default`), and the instance found in the list is called: a boxed struct listener removed and added again counts as still subscribed. A listener added several times is matched by value, not per copy: while at least one copy is in the list, every copy in the snapshot is called (removing one of two copies does not skip either).

Unity: Object.Destroy runs OnDestroy at the end of the frame, so a view a callback destroys with it is still subscribed (and still alive) for the rest of this dispatch and is called. It is skipped only if it leaves the list before its turn: an explicit [`Remove()`](KenseiECS.Listeners-1.md#KenseiECS_Listeners_1_Remove__0_) in the callback, or DestroyImmediate with the unsubscribe in OnDestroy.

csharp
[Obsolete("Iterate Values in the system: var values = listeners.Values; for (int i = 0; values != null && i < values.Count; i++) values[i].OnX(...). No delegate, nothing allocated. A callback must not change that list; it leaves with World.UnsubscribeLater. Removed in 3.0.")]
public void Invoke(Action<T> call, Action<T, Exception> onError = null)

Parameters ​

call Action<T>

Called once per listener.

onError Action<T, Exception>

Null: an exception from call propagates at once and the remaining listeners are not called. Otherwise it receives the listener and the exception, and the dispatch continues. If onError itself throws, that exception propagates and the dispatch stops.

Remarks ​

The snapshot is rented from Shared, cleared and returned afterwards (also when a callback or onError throws), so a dispatch allocates nothing once the shared pool has an array of that size; nested Invoke calls rent their own snapshot. While the list is unchanged the "still subscribed" test is one reference comparison per listener. After a change the lookup widens from where the previous listener was found, in both directions: a listener that unsubscribed itself, or any removal or insertion before the current position, costs about two steps per changed entry, and the dispatch stays linear. Only a listener removed by another callback is searched for in the whole list, which costs about as much as the Remove() that removed it. A capturing lambda allocates a closure per call: pass state through Invoke<TArg>() and a static lambda.

Obsolete since 2.3.0, removed in 3.0: a system iterates [`Values`](KenseiECS.Listeners-1.md#KenseiECS_Listeners_1_Values) and calls the listeners directly, which is faster and needs no delegate; a listener leaves from inside a callback with World.UnsubscribeLater.

Invoke<TArg>(TArg, Action<T, TArg>, Action<T, TArg, Exception>) ​

Call call for every listener with arg, with the same order, snapshot semantics (unsubscribed listeners are skipped, new ones are not called) and error handling as Invoke(). With a static lambda (Invoke(damage, static (l, d) => l.OnDamage(d))) the call allocates nothing: the state travels in arg instead of a closure. onError gets arg too, so a static error handler can log it (static (l, offer, ex) => Log(offer.Serial, ex)) without capturing it. Obsolete since 2.3.0, removed in 3.0: see Invoke().

csharp
[Obsolete("Iterate Values in the system: var values = listeners.Values; for (int i = 0; values != null && i < values.Count; i++) values[i].OnX(arg). No delegate, nothing allocated. A callback must not change that list; it leaves with World.UnsubscribeLater. Removed in 3.0.")]
public void Invoke<TArg>(TArg arg, Action<T, TArg> call, Action<T, TArg, Exception> onError = null)

Parameters ​

arg TArg

Passed to every call and to onError.

call Action<T, TArg>

Called once per listener with arg.

onError Action<T, TArg, Exception>

Null: an exception from call propagates at once and the remaining listeners are not called. Otherwise it receives the listener, arg and the exception, and the dispatch continues. If onError itself throws, that exception propagates and the dispatch stops.

Type Parameters ​

TArg

Remove(T) ​

Remove a listener.

csharp
[Obsolete("Use World.Unsubscribe, or World.UnsubscribeLater from inside a callback of this list. Removed in 3.0.")]
public void Remove(T listener)

Parameters ​

listener T