TIGERFORGE
Easy Event Manager
2.3
[[[Introduction]]] Easy Event Manager is a lightweight Event Manager system very easy to use. Source code is included and commented so to make clear what the manager does and how it works (and in case modify it on your needs). {{{HOW IT WORKS}}} An Event can be seen as a message that is sent globally in the Scene. This message can be sent by anyone who is in the Scene and can be listened to by anyone who is in the Scene. This means that the Event Manager is able to emit an Event (to send a message) and to listen to an Event (to listen to a message). Moreover, the Event Manager can store any kind of data when an Event is emitted and can release this data to who is listening to. {{{HOW TO START}}} In all your C# Script, where you intend to use this Event Manager, start importing the TigerForge namespace (using TigerForge;). Now, you have access to the EventManager class, which contains all the methods of this system. {{{WHAT'S NEW IN 2.3}}} Removed saveCallBack option from StartListening
This optional parameter of the StartListening() method has been removed because it didn't work as expected and it didn't offer a a particularly useful solution. Added callBackID option in StartListening.
The previous saveCallBack optional parameter of the StartListening() method has been replace by callBackID string parameter. When defined, the callback function is associated to this unique ID. Successively, in the StopListening() method you can use this ID instead of the callback function name. Dispose() / DisposeAll() methods
The ClearData() method is became deprecated and replaced by Dispose() and DisposeAll() methods. Those methods clear the memory occupied by the events, without stopping the Event. Basically, it removes data associated with the events. Bug Fix
Various minor bugs have benn fixed. [[[]]] [[[Emitting events]]] Emmitting an event means to send a message globally, to everyone who is in the Scene. Only who is listening to events will be able to receive events.| Events can be emitted from any point of a C# Script. Every time an Event is emitted, a listener detects that Event and execute the defined callback function. {{{.EMITEVENT}}} To emit an event you have to use the EmitEvent() method. This method has different options and features, so it comes with some overloads. |-||[1]EventManager.EmitEvent(eventName)||| |-||[2]EventManager.EmitEvent(eventName, sender)||| |-||[3]EventManager.EmitEvent(eventName, delay)||| |-||[4]EventManager.EmitEvent(eventName, delay, sender)||| |-||[5]EventManager.EmitEvent(eventName, filter, delay = 0)||| {{Option{}Type{}Description}} {eventName!string!The name of the Event to listen to.} {sender!object!The object that is emitting this Event.} {delay!float!The number of seconds to wait before emitting this Event.} {filter!string!The filter for selecting which listeners have to receive this Event (see Filter paragraph for details).} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --// Basic event. --EventManager.EmitEvent("HELLO_TO_ALL"); --// Basic dalayed event (after 2 seconds). --EventManager.EmitEvent("HELLO_TO_ALL", 2); --// Basic event with a simple filter. --EventManager.EmitEvent("HELLO_TO_ALL", "tag:Player"); -} } [[[]]] [[[Listenting to events]]] To make your GameObjects able to listen to an event, you have to use StartListening() method.|It's important to keep in mind that this method must be executed only once by a GameObject. For this reason, you must write this method inside a Start() or Enable() or Awake() Unity function (a function you are sure it's executed only once as the game start). {{{.STARTLISTENING}}} StartListening() is the method you must call to give a GameObject the ability to listen to an event. Basic usage always requires the name of the event (the name that was specified in the EmitEvent() method) and the name of the function (callBack function) to be executed when the event is listened to. |-||[1]EventManager.StartListening(eventName, callBack)||| |-||[2]EventManager.StartListening(eventName, target, callBack, callBackID = "")||| {{Option{}Type{}Description}} {eventName!string!The name of the Event to listen to.} {target!GameObject!The Unity GameObject that is listening to this Event. It must be defined for filtering feature.} {callBack!function!The name of the function to call every time this Event is detected.} {callBackID!string (optional)!When specified, the callBack function gets identified by this unique ID. You can use this ID string in the StopListening() method instead of using the callBack function name.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --// Basic listening. --EventManager.startlistening("HELLO_TO_ALL", MyCallBackFunction); --// Advanced listening. --EventManager.startlistening("HELLO_TO_ALL", gameObject, MyCallBackFunction); -} -void MyCallBackFunction() -{ --// Code to be execute... -} } {{{.STOPLISTENING}}} StopListening() method stops the GameObject from listening for the given event name. Keep in mind that this method just remove the GameObject callBack function from the events managament and it doesn't stop the whole event. |-||[1]EventManager.StopListening(eventName, callBack)||| |-||[2]EventManager.StopListening(eventName, callBackID)||| {{Option{}Type{}Description}} {eventName!string!The name of the event you no longer want to listen to.} {callBack!function!The name of the callBack function that was called.} {callBackID!string!The unique ID that was associated to the callBack function.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --// Version 1: Basic listening. --EventManager.startlistening("HELLO_TO_ALL", MyCallBackFunction); --// Version 2: Basic listening with callBackID. --EventManager.startlistening("HELLO_TO_ALL", MyCallBackFunction, "MY_CALLBACK"); -} -void MyCallBackFunction() -{ --// Remember that StopListening() has effect on this GameObject only. --// The "HELLO_TO_ALL" event continues to work and it's listened to by all the Objects on the Scene. --// Version 1: Using callBack function name. --EventManager.stoplistening("HELLO_TO_ALL", MyCallBackFunction); --// Version 2: Using callback ID. --EventManager.stoplistening("HELLO_TO_ALL", "MY_CALLBACK"); -} } [[[]]] [[[FILTER]]] During the emission of an Event, it's possible to use a rule to specify which listeners can receive an Event. At the moment, you can filter by name, tag and layer number. Moreover, using the wildcards, you can filter names and tags starting, ending or containing a specific string. {{{HOW TO USE THIS FEATURE}}} In order to use filtering feature, you have to use the StartListening() and the EmitEvent() methods in a proper way. {.{{1.}}} Firstly, use the StartListening method defining the target parameter, in all the GameObjects you want to involve in this feature. The Event Manager system will register the target information needed for the filtering: // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --EventManager.startlistening("HELLO_TO_ALL", gameObject, MyCallBackFunction); -} -void MyCallBackFunction() -{ -} } {.{{2.}}} Emit the Event using the filter parameter. This parameter has to be a string in this form: name: gameobject_name;tag: gameobject_tag;layer: layer_number {-{{Notes}}} - name, tag, layer commands has to be separated by ;
- you can use just one of them, two or all commands

In the following example, the Event is emitted to all the listeners that are listening to an Event with the name "I_AM_PLAYER", but only the GameObjects with "Player" tag will detect this Event. // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --// Only the GameObject with the 'Player' tag will listen to this event. --EventManager.EmitEvent("HELLO_TO_ALL", "tag:Player"); -} } {{{USE OF WILDCARDS}}} You can also use the * symbol so as to filter names and tags (not layers) starting, ending or containing a string: String* : filter names and tags starting with 'String';
*String : filter names and tags ending with 'String';
*String* : filter names and tags containing 'String'; In the following example, the Event is received only by GameObjects with name starting with 'Play', tag containing 'super', in the Default layer. // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --// Only the GameObject with the name starting with 'Play', with the tag containing 'super', in the layer 0, will listen to this event. --EventManager.EmitEvent("HELLO_TO_ALL", "name:Play*;tag:*super*;layer:0"); -} } [[[]]] [[[Listenting to a group of events]]] This alternative approach offers a more practical way to define, start and stop listeners. The idea is to collect all the listeners in a single group and then start / stop that group. {{{HOW TO USE THIS FEATURE}}} Firstly, declare a global instance of the class EventsGroup. Then, where you usually declare all the listeners, add Event names and callback functions to that instance and just call StartListening() method from it. In the same way, when you need to stop listening, just use StopListening() method on that instance. // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -// EventsGroup declaration. -EventsGroup myGroup = new EventsGroup(); -void Start() -{ --// Add some listeners. --myGroup.add("LISTENER_1", MyCallBack_1); --myGroup.add("LISTENER_2", MyCallBack_2); --myGroup.add("LISTENER_3", MyCallBack_3); --// Start the group. --myGroup.startlistening(); -} -void OnFinish() -{ --// Stop the group. --myGroup.stoplistening(); -} } [[[]]] [[[SENDING AND RECEIVEING SIMPLE DATA]]] The basic usage of StartListening() and EmitEvent() methods is just for sending and receiving an Event with a specific name. However, often is useful to emit an event with some data, that listeners can receive and read. The Event Manager has some built-in methods to send and receive any kind of data. {{{GENERIC DATA}}}
{-{{EMITTING}}} To send data with the emitted Event you have to use the SetData method. This event accepts a generic object data; this means that you can send any kind of data that can be recognized as object data type. |||EventManager.SetData(eventName, dataToEmit)||| {{Option{}Type{}Description}} {eventName!string!The name of the Event to emit.} {dataToEmit!object!The data associated with this event.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --EventManager.SetData("TO_ALL_PLAYERS", 100); --EventManager.EmitEvent("TO_ALL_PLAYERS"); -} } {-{{READING}}} To read data emitted with an Event, you have to use the GetData method or one of the Get* methods that come with the Event Manager system. If you use GetData, you will get an object; this means that you have to cast this object in the proper data type. The other Get* methods have been implemented to get a value directly converted in a specific data type. {{Method{}Returned value{}Description}} {GetData(eventName)!object!The object value. It should be casted in the proper data type.} {GetGameObject(eventName)!GameObject!The GameObject element.} {GetInt(eventName)!int!The integer value.} {GetBool(eventName)!bool!The boolean value.} {GetFloat(eventName)!float!The float number value.} {GetString(eventName)!string!The string value.} {GetSender(eventName)!object!The sender object. See "Sender Parameter" paragraph for details.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --EventManager.startlistening("TO_ALL_PLAYERS", MyCallBackFunction); -} -void MyCallBackFunction() -{ --var value = EventManager.GetInt("TO_ALL_PLAYER"); -} } {{{SENDER PARAMETER}}}
{-{{EMITTING}}} The EmitEvent method comes with an optional sender object parameter. When you use this method overload, the Event Manager saves the sender with the Event name. // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --EventManager.EmitEvent("TO_ALL_PLAYERS", gameObject); -} } {-{{READING}}} Once the Event is detected from your listener, you can read the sender GameObject simply using the GetSender() method. // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --EventManager.startlistening("TO_ALL_PLAYERS", MyCallBackFunction); -} -void MyCallBackFunction() -{ --var sender = EventManager.GetSender("TO_ALL_PLAYERS"); --if (sender != null) GameObject go = (GameObject)sender; -} } [[[]]] [[[SENDING AND RECEIVEING COMPLEX DATA]]] SetData() method is a practical and easy way to send data. However, it accepts one value only and it's not very suitable in case you need to emit a large amount of data. For this reason, Event Manager comes with two approaches to manage a lot of data. {{{SET/GET DATA GROUP}}} This method consists of collecting data into an array and realising this data in a collection with built-in conversion methods. {-{{EMITTING}}} SetDataGroup() method allows to send an unlimited list of values. |||EventManager.SetDataGroup(eventName, parameters...);||| {{Option{}Type{}Description}} {eventName!string!The name of the Event to emit.} {parameters!array!An unlimited list of data.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --// This method accepts a list of any kind of data. --EventManager.SetDataGroup("TO_ALL_PLAYERS", 24, true, 20.5f, gameObject, "Hello", 267, false); --EventManager.EmitEvent("TO_ALL_PLAYERS"); -} } {-{{READING}}} GetDataGroup() method allows to read the emitted data collection. The data becomes available as an array. However, Easy Event Manager uses the GetDataGroup method to release this array with a built-in structure with specific methods to convert the values in the proper way. |||EventManager.GetDataGroup(eventName);||| {{Option{}Type{}Description}} {eventName!string!The name of the Event to emit.} {{Returned Value{}Type{}Description}} {Built-in array of values!array!An array with built-in conversion methods.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --EventManager.startlistening("TO_ALL_PLAYERS", MyCallBackFunction); -} -void MyCallBackFunction() -{ --// Accessing to the data array. --var eventData = EventManager.GetDataGroup("TO_ALL_PLAYERS"); --// Array reading with proper values convertion. --int age = eventData[0].ToInt(); --bool canAttack = eventData[1].ToBool(); --float power = eventData[2].ToFloat(); --GameObject me = eventData[3].ToGameObject(); --string say = eventData[4].ToString(); --... -} } {{{INDEXED DATA GROUP}}} Being an array, the DataGroup approach requires you use the 0 to X indices to locate the values, in the same order they were declared in the SendDataGroup() method. The IndexedDataGroup approach offers a more flexible way allowing to define a unique ID for each value, exactly as happens in the C# dictionary data-type. {-{{EMITTING}}} SetIndexedDataGroup() method requires the DataGroup type in order to declare couples of ids and values. |||EventManager.SetIndexedDataGroup(eventName, dataGroupParameters...)||| {{Option{}Type{}Description}} {eventName!string!The name of the Event to emit.} {dataGroupParameters!parameters!An unlimited list of DataGroup parameters.} |||EventManager.DataGroup(id, value)||| {{Option{}Type{}Description}} {id!string!An unique ID to indentify this value.} {value!object!A value.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --// This method accepts a list of any kind of data. --EventManager.SetIndexedDataGroup( ----"TO_ALL_PLAYERS", ----new EventManager.DataGroup { id = "strength", data = 100 }, ----new EventManager.DataGroup { id = "coins", data = 250 }, ----new EventManager.DataGroup { id = "bonus", data = "Iron Shield" } --); --EventManager.EmitEvent("TO_ALL_PLAYERS"); -} } {-{{READING}}} GetIndexedDataGroup() method releases the emitted data as a C# dictionary-style structure. The value reading require the use of the unique ID. |||EventManager.GetIndexedDataGroup(eventName);||| {{Option{}Type{}Description}} {eventName!string!The name of the Event to emit.} {{Returned Value{}Type{}Description}} {Built-in collection of values!collection!A data collection with built-in conversion methods.} // Import TigerForge namespace for accessing to EventManager class. using TigerForge; public class Demo : MonoBehaviour { -void Start() -{ --EventManager.startlistening("TO_ALL_PLAYERS", MyCallBackFunction); -} -void MyCallBackFunction() -{ --// Accessing to the data collection. --var eventData = EventManager.GetIndexedDataGroup("TO_ALL_PLAYERS"); --// Colection reading with proper values convertion. --int strength = eventData.ToInt("strength"); --int coins = eventData.ToInt("coins"); --int bonuses = eventData.ToString("bonus"); -} } [[[]]] [[[MANAGEMENT METHODS]]] Easy Event Manager comes with a collection of methods that can be use to get informations or do specific operations. {{{EVENT NAME EXISTS}}} Return true if an event with the given name exists. |||EventManager.EventExists(eventName)||| {{{STOP ALL THE EVENTS}}} Shut down the listening system. |||EventManager.StopAll()||| {{{LISTENING CHECK}}} Return true if there is at least one listener. |||EventManager.IsListening()||| {{{PAUSE / RESTART}}} Pause and restart the listening of an Event (if specified) or of all the Events (if nothing is specified). |||EventManager.PauseListening(eventName)||| |||EventManager.RestartListening(eventName)||| {{{PAUSE CHECK}}} Return true if the specified Event has been paused. |||EventManager.isPaused(eventName)||| {{{DISPOSING DATA MEMORY}}} Clear the memory occupied by single event or by all the Event Manager system. This method clear data only, whereas the listeners continue to work. |||EventManager.Dispose(eventName)||| |||EventManager.DisposeAll()||| [[[]]]