This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Events
Christian Sagel edited this page Aug 12, 2018
·
23 revisions
Events are messages that inform one to many subscribers that an "event" has taken place. Some examples of such events would be:
- A player reaching an area for the first time.
- An agent has decided to engage a target.
- A given amount of time has passed.
- The player has chosen to go back to the main menu.
All events in the framework are objects, and are defined by deriving from the Stratus.Event class. Since an event is an object it can contain as many fields/properties as you need in order to describe it.
An event is defined by deriving from the Stratus.Event class:
public class SampleEvent : Stratus.Event { public int number; public Vector2 vector2; public bool boolean; }
Users can take two different actions with events:
- Dispatch: An event is dispatched onto a target GameObject, or to the Scene.
- The event must first be constructed (or retrieved from a cache):
SampleEvent eventObj = new SampleEvent();
- We then set its values:
eventObj.number = 5;
- Then finally we can dispatch it:
this.gameObject.Dispatch<SampleEvent>(eventObj);
Scene.Dispatch<SampleEvent>(eventObj);
- Connect: A Monobehaviour can connect to an event in order to listen to it. It must provide a callback function which will be invoked when the event is received by the GameObject.
this.gameObject.Connect<SampleEvent>(OnSampleEvent);