Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Global Events Manager - help with the logic behind it

Discussion in 'Scripting' started by honprovet, May 25, 2015.

  1. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Hello,

    I need help understanding why a Global Events Manager is necessary when there are lots of Events and listeners. How that could help us and how it works.

    I read the wiki event manager code and some others and, to be honest, didnt really get it.

    What i need is:
    - a simple example, no code required, of usage
    - an explanation on how the manager should deal with the Events, the listeners and the event triggers (its pipeline, per say) - again, no code required.

    It should be assumed that I uderstand how events work individually. I just need to learn how to organize them.

    I believe this would help other people a lot as well.

    Thanks!
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    The 'event' keyword in C# allow you to declare that an object can trigger events and other objects can subscribe to these events with delegates that will be executed when the event occurs. That requires your listener to know about the publisher though.

    Code (CSharp):
    1. public class MyListener : MonoBehaviour
    2. {
    3.     public MyPublisher pub;
    4.  
    5.     void Awake(){
    6.         pub.myEvent += myHandler;
    7.     }
    8.  
    9.     void myHandler(){ /* do something*/ }
    10. }
    An Event Dispatcher pattern allows objects in the game to react to events without knowing about the objects that triggered the event. It basically means that you have your publisher call the EventDispatcher and request that it sends the event to all relevant listeners. Listeners can subscribe to the event dispatcher to know about particular events. The main advantage is that listeners don't know about the publishers and publishers don't know about the listeners.

    Here's an example of an event dispatcher in which listeners can subscribe or unsubscribe from events

    https://github.com/DoomSamurai/Kathulhu-Framework/blob/master/Core/Events/EventDispatcher.cs
     
    honprovet likes this.
  3. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Keep in mind that you can make the EventDispatcher a static class. I personnaly make an instance of the EventDispatcher available via a global manager class with a static property.
     
    honprovet likes this.
  4. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Perfect answer! Thanks a lot. I'll study your dispatcher tomorrow!!
     
    DoomSamurai likes this.