Bump. Anyone know what type this would be declared as? I want to create an array of function pointers/delegates.
you normally declare arrays as : Array not [] style on JS so it shouldn't be a problem. but if thats not fine for you do a bit of experimentation with #pragma strict enabled, I'm sure it will throw you an error directly if the type is not fine
I believe internally mono uses a class of type Object for this. Sort of a high level container class similar to how Unity uses the gameObject class. You could always set myVar = myFunc then see what GetType makes of it to be sure.
Normally mono use delegates for function pointers but I'm not sure if the type delegate is required for JS
From what I know, Unity's JS does not support declaring delegate types, like we can in C#, or at least not yet... One thing you can do, though, is declare function types, as was alluded here in this discussion: Code (csharp): #pragma strict class Foo extends MonoBehaviour { private var functionPointer : function(SomeType) : SomeOtherType; function Start() { if (someCondition) { functionPointer = FirstMethod; } else { functionPointer = SecondMethod; } } function Update() { functionPointer(); } function FirstMethod(aParam : SomeType) : SomeOtherType { // do whatever, return a SomeOtherType variable appropriately. } function SecondMethod(aParam : SomeType) : SomeOtherType { // do whatever, return a SomeOtherType variable appropriately. } } Here the functionPointer variable is declared to be of type "function taking a parameter of type SomeType and returning a value of type SomeOtherType", which as far as I can see pretty much works just like declaring a delegate type in C# since in both approaches you have to define a method signature and stick to it (just as you would declare a function pointer with a defined function prototype/signature in good 'ol C and only assign functions that stick to it). Both First Second methods adhere to the specified signature and therefore can be freely assigned to the functionPointer variable, which gets called as a method itself in Update() every frame. This example is of course rather simplistic, you could just as easily place the if (someCondition) block in Update and call the methods directly, but if performing that test only once is possible then why not leverage that performance enhancement? I'm sure more elaborate examples and uses will make function types shine just as much as full-blown C# delegates. HTH! Regards, - jmpp
Two things I forgot to say are that: 1) The function type variables are a Unity 3.x thing, but being already at 3.3 and with 3.4 close at bay I'm sure the majority of us are already in the clear. 2) When any of the components of your function type are void you have to be a bit careful, i.e.: Code (csharp): var functionPointer : function(); If you don't want your function prototype to take any arguments then you don't get to declare it as function(void), as that will produce a syntax error (don't ask me why! ). And if you don't want to return any values then I think you can omit the colon and void return entirely (i.e. use plain function(), not function : void), but I'm sure a little experimentation with the syntax will clear things up. The documentation I got this from is the 3.x upgrade details page (JavaScript improvements section): http://unity3d.com/support/documentation/Manual/MonoUpgradeDetails.html Regards, -jmpp
Hi, Thanks for illuminating this lesser-known functionality of UnityScript. I'm attempting to make a UnityScript - based messaging system with function pointers but I can't seem to figure out the syntax for passing parameters to function pointers stored in a dictionary...here's my code: This is the basic code to store an event function pointer with no parameters - it works great Code (csharp): otherScript.js // add event listener EventController.AddEvent("testing", Test); // send event EventController.SendEvent("testing"); // test function function Test(){ print("test success"); } //-------------------------------- EventController.js code import System.Collections.Generic; static var eventDictionary : Dictionary.<String, ArrayList> = new Dictionary.<String, ArrayList>(); // add event function pointer with no parameters static function AddEvent(eventName : String, func : Function){ // add function pointer to appropriate ArrayList if event already exists if (eventDictionary.ContainsKey(eventName)){ if (!eventDictionary[eventName].Contains(func)){ eventDictionary[eventName].Add(func); } } // otherwise create new dictionary entry else{ var newArray : ArrayList = new ArrayList(); newArray.Add(func); eventDictionary.Add(eventName, newArray); } } static function SendEvent(eventName : String){ for (f in eventDictionary[eventName]){ (f as Function)(); } } As I said, this works great. However, I'd like to be able to send along some info about an event when it happens - so this is how I tried sending along a parameter based on what I've read on the few posts I've seen about this: Code (csharp): // OtherScript.js EventController.AddEvent("testing", ParameterTest); // send event EventController.SendEvent(ParameterTest, "arg"); function ParameterTest(ob : Object){ print(ob.ToString()); } //------------------------------------ //EventController.js // experimental event with parameter static function AddEvent(eventName : String, func : function(Object)){ if (eventDictionary.ContainsKey(eventName)){ if (!eventDictionary[eventName].Contains(func)){ eventDictionary[eventName].Add(func); } } else{ var newArray : ArrayList = new ArrayList(); newArray.Add(func); eventDictionary.Add(eventName, newArray); } } // and to send the event static function SendEvent(func : function(Object), arg : Object){ func(arg); } Unfortunatey this doesn't work so well It gives me this error: IndexOutOfRangeException: Array index is out of range. CompilerGenerated.__EventController_AddEvent$callable0$27_53__.Call (System.Object[] args) (at Assets/_Scripts/EventController.js:27) - line 27 refers to the declaration of the AddEvent function. I know it has to be something with the syntax, but its completely foreign to me and I haven't been able to figure it out - any help would be much appreciated! Thanks!