- Home /
JS Delegates
Hey guys, Can someone please point me in the right direction to get delegates and events to work in JavaScript/UnityScript? I usually program in C#/C++ but for this project we're required to work in JS. I've followed Prime31's youtube video but still cannot get it to work.
Major issues I'm having is issues with unity not recognising the "delegate" and "event" tokens. Even trying to simplify it down to just having a function, and a static variable of that function as a type Unity still gives an unexpected token error message.
Thanks.
Answer by Bunny83 · Nov 21, 2011 at 12:00 AM
As far as i know you have to use the "usual" Javascript syntax (the type "Function" with a capital F). As you might know even UnityScript is compiled to a .NET / Mono dll. UnityScript is not a standardized language and i only use C# for Unity so i can't tell the exact syntax but i think i can remember a question about that issue and it wasn't that difficult.
I would try something like:
var myDelegate : Function(parameter : float);
but haven't tested it yet ;)
Thanks i'll have to try that out. I do remember reading some pseudo code using Function with a capital F, but i didn't realise that this is the "type" function. C# Syntax would be something like:
public delegate void functionHandler( string data );
public static event functionHandler newCallback1;
public static event functionHandler newCallback2;
I'll try something similar with your suggestion above =)
Sure, but i don't think UnityScript have something like the "event" modifier. Even in C# it's "just" a way to restrict the invocation to the containing class and enables you to declare it in an interface declaration. Since interfaces is one of the things that can't be defined (but used) in UnityScript the event keyword becomes almost useless.
See this article about delegates and events
Unfortunately this suggestion does not work entirely. I got everything working up to this point: var $$anonymous$$yDelegate : function( String );
static var firstCallBack : Function;
firstCallBack = $$anonymous$$yDelegate;
static var secondCallBack : Function;
secondCallBack = $$anonymous$$yDelegate;
But when i try to add the callbacks to my listener through the normal syntax of firstCallBack += newFunction; It spits the dummy.
Unfortunately you can't create a delegate-type in UnityScript (AFAI$$anonymous$$). In Javascript there are only something like function pointers. I've found the "old" question on this issue ;)
I'm not sure if UnityScript supports multicast delegates. Personally i don't really care about that since it's not my language, sorry ;)
$$anonymous$$aybe one of the pro UnityScript users can tell you more about that, but it seems that's one of those features which are not completely implemented in US.
An interesting feature I found. JS can't create events, but it can add its function type objects to events created by C# scripts. Also, js supports creation of anonymous functions and you can assign them to any compatible function pointer.
//C#
public class Blah {
public event eventHandle trigger;
}
------
//js
var instance : Blah;
var myFunc : Function = function() { Debug.Log("Anonymous $$anonymous$$ethod"); };
instance.trigger += myFunc;
Your answer
Follow this Question
Related Questions
How to do c# Action in UnityScript 0 Answers
c# delegates/events using from js? 1 Answer
UnityScript, Delegates and SmoothMoves 1 Answer
Difference between Function and function() Types 1 Answer
Setting Scroll View Width GUILayout 1 Answer