Class EventChannel<TConfig, TEvent, TContext>
- Namespace
- ElectricDrill.AstraRpgFramework.Events
- Assembly
- com.electricdrill.astra-rpg-framework.Runtime.dll
Generic base class for event channels that dispatch to a global event (retrieved from a configuration object) and to per-entity extra events. Subclasses bind the config type and the config-provider delegate for their specific system.
The _extras list is serializable: when a consumer field is marked
[SerializeField], Unity persists the extra-event references so they can be
assigned statically in the Inspector. The System.Func<T, TResult> delegates are
not serializable and must be re-initialised after deserialisation via
Initialize(Func<TConfig, TEvent>, Func<TConfig>).
[Serializable]
public class EventChannel<TConfig, TEvent, TContext> where TEvent : GameEventGeneric1<TContext>
Type Parameters
TConfigThe configuration interface that exposes the global event.
TEventThe concrete GameEventGeneric1<T> subclass.
TContextThe context payload type.
- Inheritance
-
objectEventChannel<TConfig, TEvent, TContext>
Constructors
EventChannel()
Parameterless constructor required by Unity serialisation.
public EventChannel()
EventChannel(Func<TConfig, TEvent>, Func<TConfig>)
public EventChannel(Func<TConfig, TEvent> globalGetter, Func<TConfig> configProvider)
Parameters
globalGetterFunc<TConfig, TEvent>configProviderFunc<TConfig>
Methods
Add(TEvent)
Registers an additional event that will be raised alongside the global event.
public void Add(TEvent evt)
Parameters
evtTEvent
Initialize(Func<TConfig, TEvent>, Func<TConfig>)
Re-initialises the non-serialisable delegates after Unity deserialisation.
Must be called from the consumer's Awake() or equivalent init method.
public void Initialize(Func<TConfig, TEvent> globalGetter, Func<TConfig> configProvider)
Parameters
globalGetterFunc<TConfig, TEvent>configProviderFunc<TConfig>
Raise(TContext)
Dispatches the context to the global event (if a config is available) and to all registered extra events. Tolerates a missing config (no global raise). Uses a reverse index loop so that listeners may safely remove extras during dispatch.
public virtual void Raise(TContext ctx)
Parameters
ctxTContext
RaiseExtras(TContext)
Dispatches ctx to all registered extra events.
Uses a reverse index loop so that listeners may safely remove extras during dispatch.
Exposed to subclasses so they can reuse the extras loop while overriding global dispatch.
protected void RaiseExtras(TContext ctx)
Parameters
ctxTContext
Remove(TEvent)
Unregisters a previously added extra event.
public void Remove(TEvent evt)
Parameters
evtTEvent