Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

[Released] Events Pro

Discussion in 'Assets and Asset Store' started by stanislav-osipov, Jul 9, 2013.

  1. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Events Pro, this is fast and flexible event system, builded on C# delegates. With built-in editor extension. Visual part will help you to see structure of your running game in real time, and will help with profiling, bug fixing and optimizing.

    Avaliable on Asset Store
    Full Documentation

    Event System main features:
    • addEventListener
    • removeEventListener
    • dispatch / dispatchEvent
    • stopPropagation / stopImmediatePropagation
    • Events can be dispatched by string or int Ids
    • Two types of listener functions avaliavle
    Full Event System Documentation with use examples


    Event Graph features:
    • Searching
    • Positions remembering
    • dispatch / dispatchEvent
    • 3 Collapsing modes
    • Branches
    • Overloaded listener detection
    • Forgotten listener detection
    • Listeners leaking detection

    $zzz.jpg



    Now I whant to tell more about main idea of visual event grpah and how can it help you to make your code better. The only thing you do - is building your game as you want using this events system, and as bonus you get visualization of how your game works. And also it can work like debugging tool, with should show you where you have overloaded or forgotten listeners, or memory leaking. Here is how event graph looks for a simple example scene.

    $s1.png



    Main Features

    Events Dispatch detecting.
    This is the main feature of graph. When event is dispatched the conection between dispatcher class and listner becomes green for few seconds. And of course count of dispatched and resived events is chnging. So you can see in runtime how mutch events was dispathed by specific class, and how muth of them was resived by specific listner.

    Searching.
    There is search field in the top left corner of Graph window. So that how our graph will look if we will search “Controller”
    Direct Picture URL

    Positioning:
    During graph construction, the elements are placed to avoid overlapping. Sometimes this is not the way you want to see your grap. That’s why it support position recording, if you move element via mouse, it position will be remembered. and in the next time it will be placed where you want it to be.

    Collapsin:
    If you have a lot of small listeners or dispatchers in you event graph, collapsin can make it more readable. Collapsin has three modes:
    1) Nothing - collapsing not working.
    2) Listeners - equal(same class, same listen functions) listeners will collapse in one.
    3) All Matches - equal listeners and dispatchers will be collapse in one.
    Here is how our example graph will look like, if we will choose All Matches collapse mode.

    $s3.png

    Branches:
    In the example scene you can see three separate event branches. But in real project usually much more event brunches. That’s why you can view them separately from each other.
    Use Branch list to switch view to any branch or switch back to view all of them. Or you can just use “Next Brunch” button.

    Element Context menu:
    Right click on graph element to open context menu
    $es.png


    Profiling and bug fixing
    This extension was designed not only for visualizing your game events, but to help you profile your events.
    You can find few examples of how to use it below.


    Overloaded listener.
    In result of some game action’s we have the same listener added twice. That’s very easy to find this kind of issue with Events Pro. If Event listened more that one time for the same listener function of the same class. Event link will become red.

    Besides you can compare count of event being dispatched, and listener got called. See the picture below.
    $e2.png


    Forgotten Listener.
    The gameobject with listener class was deleted, but listener, wasn’t. In that case you will get very dangerous situation, because listener class that was attached to gameobject still alive and can get events.
    Picture below will illustrate grap reaction, if this kind of situation happens.

    There is limit of image attached, so here is Direct Picture URL


    Memory Leaking.
    This two easy ways how you detect memory leaking.
    1) If after you finish “Playing” your game, you still see some active elements in the graph. This mean that probably your forgot to remove listener on game object destroy.

    Direct Picture URL

    2) If while your game running you can see that number of listeners is increasing or simply if there is more listeners than should be, you can be sure that you have a listener's leak. Usually listeners leaking happens on scene changing.

    Unnecessary listener.
    For example you have monsters who listening mouse events. But you do need them to do this when you in pause mode. In Event Graph you see every single event that is dispatches, so you can analyze your game by looking on event graph. As result you will cut some listeners according to game current game state, etc, and save some performance!


    If you whant to lear more here is Full Documentation

    Inside the package you will find use exmples, and scene where you can profile and emulate errors to understand how it works.

    Thanks for you time. Any suggestions or features request you can post here or send me on lacost.st@gmail.com
     
    Last edited: Jul 9, 2013
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    From the screenshots, it seems like you're going against convention. Where you use "On", I'd expect to see "Handle". "On" is what I'd expect to see prefixing a protected method that raises an event. I can understand trying to mimic what Unity does; but their "On"s are not methods you can subscribe to events. What would you do for the protected methods I mentioned?
     
  3. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Hi, Jessy .
    First of all this event system not connected with unity messages. And it do not force to use any naming conventions. The naming convetions is up to you.

    Let me exmplaing how it works a little bit more wider.
    For example I whant to create My own GUI button class.
    Button.cs will look like this

    Code (csharp):
    1.  
    2. public class Button : EventDispatcher {
    3.  
    4.     private float w = 150;
    5.     private float h = 50;
    6.    
    7.     private Rect buttonRect;
    8.    
    9.     void Awake() {
    10.         buttonRect = new Rect((Screen.width - w) / 2, (Screen.height - h) / 2, w, h);
    11.     }
    12.    
    13.     void OnGUI() {
    14.         if(GUI.Button(buttonRect, "click me")) {
    15.             dispatch(BaseEvent.CLICK);
    16.         }
    17.     }
    18.    
    19. }
    20.  
    21.  
    And here is example how I can create and listen for my button CLICK event in my ButtonListner Class

    Code (csharp):
    1.  
    2. void Start () {
    3.         //creating our button
    4.         Button btn = gameObject.AddComponent<Button>();
    5.         //listening for the event
    6.         btn.addEventListener(BaseEvent.CLICK, onButtonClick);
    7.     }
    8.  
    9.  
    And in this case two nodes will apper in Graph
    Button node with CLCIK event bounded to onButtonClick function of ButtonListner node.

    or if you will change ButtonListner Code like this

    Code (csharp):
    1.  
    2. void Start () {
    3.         //creating our button
    4.         Button btn = gameObject.AddComponent<Button>();
    5.         //listening for the event
    6.         btn.addEventListener(BaseEvent.CLICK, HandleButtonClick);
    7.     }
    8.  
    9.  
    Then in events grap you will see
    Button node with CLCIK event bounded to HandleButtonClick function of ButtonListner node.

    And of course HandleButtonClick can be protected or public this is all up to you.
     
    Last edited: Jul 9, 2013
  4. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    What is the overhead for this? Is it just in editor so it is stripped out on the builds?
     
  5. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Hello, imtrobin.

    It almoust not eating editor resourses on runing game.
    It can only take some time on start up when you creating a lot of listners. For example for my project it has to create over 100 graph nodes, so it takes about 0.1sec on start up.

    Yes, that's right. It will not affect your game perfomance at all.
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    hi Stanislav, i can't resist to post my feedback ;).

    documentation:
    Branches: “Next Brunch”, brunch is a meal.

    api:
    Code (csharp):
    1.  
    2. public  void  addEventListener(string eventName, Func<Void> handler)
    there is a C# convention that method names use Pascal naming scheme ie begin capitalized: AddEventListener. i suggest to change your api early to prevent many people using your package need changing their code.
    Code (csharp):
    1.  
    2. public class Button : EventDispatcher {
    3.     void Awake()
    does that mean EventDispatcher derives from monobehavior and i can only invoke events from monobehavior classes?
     
  7. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Hey, exiguous
    Thanks for pointing this, I'll fix this typo in upcoming version.

    The source code is open, so it can be changed as you with. But I'm taking your advice and I'm going to chnage it, thanks.

    Yes that's write, EventDispatcher derives from MonoBehavior .
    Point is that if you whant your MonoBehavior component Invoke the events you should derive it from.EventDispatcher
    Code (csharp):
    1. public class EventDispatcher : MonoBehaviour, IDispatcher, IEventGraphNode
    But if you whant your not MonoBehavior component Invoke the events you should derive it from EventDispatcherBase
    Code (csharp):
    1. public class EventDispatcherBase : IDispatcher, IEventGraphNode
    There is only two diffrense between EventDispatcher and EventDispatcherBase


    1) EventDispatcher is MonoBehavior . EventDispatcherBase is base C# Object, as you can see it in declaration above

    2) EventDispatcher has additional feature.
    Because it MonoBehavior component, it can detect when you destroyng it, and if you forgot to remove listners it's not a problem beacuse EventDispatcher will clean tham for you on destroy.

    Code (csharp):
    1.  
    2. protected virtual void OnDestroy() {
    3.        #if UNITY_EDITOR
    4.             EventGraphManager.removeAllNodeListners(this);
    5.             EventGraphManager.removeNode(this);
    6.        
    7.         #endif
    8.         clearEvents();
    9.     }
    10.  
    I hope you enjoy using it!
     
    Last edited: Jul 12, 2013
  8. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    I think you should prefix or use namepsace to prevent conflicts. Eventdispatch is a very common class name
     
  9. MaDDoX

    MaDDoX

    Joined:
    Nov 10, 2009
    Posts:
    764
    Looks really good Lacost, might ease down the visualization overload of large projects event relationships. Congratulations, and I look forward to future developments! :)
     
  10. Play_Edu

    Play_Edu

    Joined:
    Jun 10, 2012
    Posts:
    722
    Hi,

    What different between play-maker, an treas universe or this tool.
     
  11. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Thx for pointing this imtrobin , I'll think it over.

    Thank for kind worlds MaDDoX, appreciate this. New release is coming soon. Just waiting Unity team until they fix one small bug, but very importent for this tool. I sure it will be fixed on 4.3 update.

    The Playmaker is visual scripting tool. This tool is visualize what you already did :) And helps you to debug and see how your game events are working in runtime.
     
  12. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    This thread is pretty old but I'm wondering: I have been using this lib in a game of mine but one of the things I don't like is that I can't pass send event objects with my dispatch. I know there is CEvent.data but you have to cast it and I have to keep track which events are sending load data of specific types and which are not.

    Also the graph does get sluggish when there are many lines and nodes.

    Any chance of an update?