Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. The Burst compiler has its own forum section now.
    Dismiss Notice
  4. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

An Event Listener for Unity JavaScript

Discussion in 'Scripting' started by AlucardJay, Jan 27, 2013.

  1. AlucardJay


    May 28, 2012
    Hello all, I posted this on Unity Answers, but it is probably more a discussion-based question so here it is in the forums =]

    Is it possible to write an event listener in UnityScript,
    with the same functionality as CSharpMessenger Extended as found in the Unity Wiki :

    My previous run-in with anything Delagate says to me "no,
    well not without a major write-around to do the things C# can and uJS cannot".

    The only way that I can currently imagine this is to have a singleton that every script can call upon i.e. broadcast to,
    and in that singleton have a list of every gameObject and string(command) that would normally have AddListener and RemoveListener,
    then when those scripts call to be added or removed, scan the lists and modify them accordingly.
    Then when a broadcast function is called on the singleton, SendMessage to the gameObject with the same index as the string in the list. Very rough and poor idea.

    To explain my idea another way ....

    GUI health bar script would have :

    Code (csharp):
    1.     function OnEnable()
    2.     {
    3.         if ( isPlayerHealthBar )
    4.         {
    5.             MyListener.Instance.StartListening( "PlayerHealth", this.gameObject );
    6.         }
    7.         else
    8.         {
    9.             MyListener.Instance.StartListening( "MobHealth", this.gameObject );
    10.         }
    11.     }
    13.     function OnDisable()
    14.     {
    15.         MyListener.Instance.StopListening( this.gameObject );
    16.     }
    18.     function ListenerUpdate( theValue : int ) // also functions for theValue as float and string
    19.     {
    20.         someVar = theValue;
    21.     }

    PlayerScript would have :

    Code (csharp):
    1.     function Update()
    2.     {
    3.         MyListener.Instance.BroadcastThis( "PlayerHealth", health );
    4.     }

    MobScript would have :

    Code (csharp):
    1.     function Update()
    2.     {
    3.         MyListener.Instance.BroadcastThis( "MobHealth", health );
    4.     }

    MyListener singleton would have :

    Code (csharp):
    1.     list of gameObjects (GO)
    2.     list of strings with same index as to the gameObject in gameObject list
    4.     function StartListening( theFunction : String, theObject : GameObject )
    5.     {
    6.         add theFunction to string list
    7.         add theObject to GO list
    8.     }
    10.     function StopListening( theObject : GameObject )
    11.     {
    12.         search GO list for theObject, get the index
    13.         remove item from both lists at index
    14.     }
    16.     function BroadcastThis( theFunction : string, theValue : int ) // also functions for theValue as float and string
    17.     {
    18.         search string list for string (theFunction)
    19.         if found,
    20.             SendMessage to the gameObject in GO list with same index as string .... GO[index].SendMessage( "ListenerUpdate", theValue );
    21.     }
    My main concern with this idea is the more NPCs, the harder the singleton has to work, for every NPC script in every update is calling MyListener.Instance.BroadcastThis( "SomeString", someVar);

    Then the first real problem I see is that each script is restricted to just one ListenerUpdate function, and then also only one variable unless I use string, int and float making max of 3 variables passable.

    Any thoughts, ideas, suggestions, and/or known uJS resources are most welcome =]

    Link to the UA question :

    Edit : it seems no-one is even remotely interested in reading this, let alone responding .... I can hear the comments now "just use C#" , but where's the challenge in that :p
    Last edited: Jan 28, 2013
  2. souperdavecdn


    Nov 13, 2012
  3. dkozar


    Nov 30, 2009
    You could check out eDriven. It's an open source asynchronous / event-driven framework for Unity. :)

    It implements 2 messaging systems: events (EventDispatcher class here) and signals&slots (link here).

    It's a tested solution, works great with Unity (it doesn't use the standard .NET event system nor yield returns).
  4. AlucardJay


    May 28, 2012
    Hello, and thank you both souperdavecdn and dkozar , I found both your suggestions excellent, and gave me a lot to learn and understand from. Sorry my reply is so late, but I did read your comments, and was very glad for so much excellent information. I still have a lot to learn, but thanks to you both I have a starting point with many excellent useful references to help further my understanding. The replies has been really helpful. You are both awesome, this is a fact.

    All the Best, Jay Kay
    Last edited: Mar 22, 2013