Skip to main content
File: Packages/com.molca.core/Runtime/Events/EventDispatcher.cs
Subsystem: registered with RuntimeManager like other services

When to use

Use EventDispatcher when you need decoupled, cross-component communication without direct references:
  • Cross-system notifications (e.g. language changed, app initialized, session events) where publisher and subscriber live in different assemblies or scenes.
  • String-keyed events with optional typed payloads — avoids coupling to concrete event classes.
  • Prefer UnityEvent for Inspector-wired, single-object callbacks. Prefer C# event for tightly-coupled same-class patterns. Use EventDispatcher for everything else.

TypedEvents

File: Events/TypedEvents.cs — wraps common event names. Prefer these over magic strings where entries exist (scene load, modals, etc.).

Using from code

Use a normal MonoBehaviour with an injected EventDispatcher, subscribe in OnEnable, and always unsubscribe in OnDisable.

API Reference

Troubleshooting

  • Event not received: confirm RegisterEvent runs before the first DispatchEvent. If subscription is in OnEnable but dispatch fires during RuntimeManager init, the handler may not be registered yet — move subscription to Start with await RuntimeManager.WaitForInitialization().
  • Duplicate callbacks firing: calling RegisterEvent twice with the same callback adds it twice via Delegate.Combine. Always pair with UnregisterEvent in OnDisable to prevent stacking across enable/disable cycles.
  • Missing unsubscribe causes errors after scene unload: destroyed objects’ callbacks remain registered and throw MissingReferenceException. Always call UnregisterEvent in OnDisable or OnDestroy.
  • Wrong type parameter on DispatchEvent<T>: the type key must match exactly between publisher and subscriber. DispatchEvent<int> will not reach RegisterEvent<float> handlers even with the same event name.