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 : 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);
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 : Event Listener for uJS - Questions & Answers - Unity Discussions
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 ![]()