Graph Events
There are 3 types of events, each one is used for different situations. They are:
- EventMediator
- MonoEventDispatcher
- StringEventDispatcher
There are several Tasks that already implement use cases for the three types of events, and this should cover most project needs. You can create custom implementations in your project, but it is recommended to follow the same subscription and unsubscription pattern used in existing Tasks.
MonoEventDispatcher
MonoEventDispatcher is a utility component and it will be added when needed, in other words, when you call a task that depends on listening to events from a specific GameObject, the logic will try to get or add the component.
Here is the list of events it implements:
// Trigger 2D
public event Action<Collider2D> OnTriggerEnter2DEvent;
public event Action<Collider2D> OnTriggerStay2DEvent;
public event Action<Collider2D> OnTriggerExit2DEvent;
// Collision 2D
public event Action<Collision2D> OnCollisionEnter2DEvent;
public event Action<Collision2D> OnCollisionStay2DEvent;
public event Action<Collision2D> OnCollisionExit2DEvent;
// Trigger
public event Action<Collider> OnTriggerEnterEvent;
public event Action<Collider> OnTriggerStayEvent;
public event Action<Collider> OnTriggerExitEvent;
// Collision
public event Action<Collision> OnCollisionEnterEvent;
public event Action<Collision> OnCollisionStayEvent;
public event Action<Collision> OnCollisionExitEvent;
// Particle
public event Action<GameObject> OnParticleCollisionEvent;
public event Action OnParticleTriggerEvent;
// Visibility
public event Action OnBecameInvisibleEvent;
public event Action OnBecameVisibleEvent;
// Activation
public event Action OnEnableEvent;
public event Action OnDisableEvent;
public event Action OnDestroyEvent;
StringEventDispatcher
This component operates similarly to MonoEventDispatcher
, but instead of sending MonoBehaviour events like OnTrigger
, it sends string messages.
It is ideal for fast, simple and efficient communication of sending a signal to some GameObject that has GraphRunners.
Example:
Let's say you want to implement a lever that opens doors, and both doors have a GraphRunner.
When a door initializes, it subscribes to a string event, such as "Open", associated with its own GameObject. This also adds the StringEventDispatcher
component to the door’s GameObject.
When interacting with the lever, it invokes the string event "Open" on the Door GameObject you want to open. This triggers the graph for the corresponding door, finalizing the 'Open' task and progressing to the task, that plays an animation to open the door.
EventMediator
Differing from MonoEventDispatcher
and StringEventDispatcher
which are associated to a GameObject, EventMediator is associated to a ScriptableObject. It's ideal for global events since all active graphs referencing the same ScriptableObject instance will receive the events.
Example:
Let's say you have an arena game where all enemies die when the time runs out.
At the start, all enemies subscribe to the same EventMediator. When the game timer ends, the GameController invokes the EventMediator forcing all remaining enemies to die.