CSharpEventManager

Author: Billy "brian" Fletcher

Description
This is an iPhone friendly implementation of a non-delegate driven event system for C#, similar to the javascript messaging systems in that you can subscribe to an event purely by a string identifier, regardless of whether it exists or not at that time. It comes in two versions, one iPhone friendly version using ArrayLists and one optimised version by AngryAnt using C# generics. There is an option to limit the amount of time spent processing events per frame and priority designation may be added in future although the processing spent sorting can easily outweigh the gain of having some events processed faster.

Usage
The event manager uses a Singleton design pattern that ensures it is self instantiating and globally accessible through the static EventManager.instance reference.

The system consists of two base interfaces for interacting and creating events, the IEvent interface that declares the base functionality of all events and the IEventListener interface that allows any class to receive events.

To create an event, simply create a new CS file and inherit from the IEvent interface and implement the two functions declared within it: GetName and GetData. GetData returns a C# object which can then be casted to any data type such as ArrayLists or predefined structures.

Here is an example event, note that the GetName code can be reused for nearly every event.

To create an event listener, follow a similar process and simply inherit from the IEventListener interface and implement the HandleEvent function. Note that an object can inherit from any number of interfaces, but only one defined class, this allows any MonoBehaviour or other object to receive events very easily.

To subscribe to events, simple call the EventManager.instance.AddListener function and provide a reference to the event listener (typically using the 'this' keyword) and the name of the event it wishes to subscribe to. Note that if you use the above GetName code it is simply the class name of the event, although the event does not have to exist in order to subscribe to it. The main benefit of this is that when working in multiple person teams, provided the events are designed beforehand, components can be developed completely independently.

The HandleEvent function takes in any event the object is subscribed to and a boolean value must be returned. This value indicates whether or not the event has been consumed by the object and therefore should not be passed to other listeners of the same event. A listener can subscribe to multiple events and differentiate them by using the GetName method.

Here is an example of an event listener that reacts to the TestEvent above:

To queue or trigger an event, merely call the EventManager.instance.QueueEvent function with a new instance of the event you wish to pass, if the event must be sent immediately call the TriggerEvent function. Note that the queue event function is typically fast enough for the vast vast majority of uses.