- Home /
Event Listener for uJS
Is it possible to write an event listener in UnityScript, with the same functionality as CSharpMessenger Extended as found in the Unity Wiki : http://wiki.unity3d.com/index.php?title=CSharpMessenger_Extended
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 :
function OnEnable()
{
if ( isPlayerHealthBar )
{
MyListener.Instance.StartListening( "PlayerHealth", this.gameObject );
}
else
{
MyListener.Instance.StartListening( "MobHealth", this.gameObject );
}
}
function OnDisable()
{
MyListener.Instance.StopListening( this.gameObject );
}
function ListenerUpdate( theValue : int ) // also functions for theValue as float and string
{
someVar = theValue;
}
PlayerScript would have :
function Update()
{
MyListener.Instance.BroadcastThis( "PlayerHealth", health );
}
MobScript would have :
function Update()
{
MyListener.Instance.BroadcastThis( "MobHealth", health );
}
MyListener singleton would have :
list of gameObjects (GO)
list of strings with same index as to the gameObject in gameObject list
function StartListening( theFunction : String, theObject : GameObject )
{
add theFunction to string list
add theObject to GO list
}
function StopListening( theObject : GameObject )
{
search GO list for theObject, get the index
remove item from both lists at index
}
function BroadcastThis( theFunction : string, theValue : int ) // also functions for theValue as float and string
{
search string list for string (theFunction)
if found,
SendMessage to the gameObject in GO list with same index as string .... GO[index].SendMessage( "ListenerUpdate", theValue );
}
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);
Any thoughts, ideas, suggestions, and/or known uJS resources are most welcome =]
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 =[
Edit : Thankyou save, you have given me some things to think about.
It's an interesting question! I'm also interested in this type of event listener and I wish I had more info on the subject.
I understand that this isn't what you're looking for but couldn't this be solved with a manager then individual scripts on each GameObject with public variables? Just a quick thought from looking at your example! The manager caches the required components of the GameObjects in lists/built-in arrays. You would then be able to ask the manager for their individual values. Performance-wize this is ridiculous fast and can be done for several objects each frame with almost no impact.
By using a lookup table or other simple id structure (GameObject ID is probably the most optimized or get the script component and ask for an ID previously set by you) you would also be able to randomly pick one object and ask the manager for values. $$anonymous$$akes the object communicate both ways through cached components.
This question is also now on the forums as it is probably more a discussion-based question, and I am getting no traffic here apart from save) : http://forum.unity3d.com/threads/167895-An-Event-Listener-for-Unity-JavaScript
Edit : one more bump, then I'll know I have nobody else interested. I can hear the comments now "just use C#" , but where's the challenge in that :P
Answer by AlucardJay · Mar 22, 2013 at 11:39 AM
To anyone following this, I had some great suggestions on my forum post : http://forum.unity3d.com/threads/167895-An-Event-Listener-for-Unity-JavaScript
Many thanks to souperdavecdn and dkozar for their replies over there. Special mention to save for their interest also, hope the information on my forum post can help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091521im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add EventTriggers through code 0 Answers
Argument 1: cannot convert from 'method group' to 'UnityAction' 1 Answer
How to add a persistent method with a single argument to a UnityEvent? 1 Answer
How do you fill an EventTrigger from a script? 0 Answers
How to add external method from class to Unity3dEvents AddLisitener()? 1 Answer