Delegates in Javascript - adding a function

I’m in the process of cleaning up my in game GUI, if for no other reason than my onGUI function looks like a God awful mess. I’m interested in using a delegate approach but I wanted to ask if its at all possible to add functions together. In C# if I understand correctly you can do the following.

currentGUIMethod something = SomeMethod;
something += AnotherMethod;

i.e. our GUI would now be showing both methods with their respective buttons right? so maybe

this.currentGUIMethod = MainMenu;
MainMenu += AnotherMethod;

does this work somehow (the above doesn’t work obivously)? can I do this with a delegate system built on Function in Javascript or is it specific to C# and I’m asking a retarded question. My reasoning is that I’d like to get away from the stupid number of booleans I have controlling the visibly of the various menus in onGUI. Equally though I don’t want my main HUD to disappear when I decide to open a character menu for example.

Cheers in advance.

http://answers.unity3d.com/questions/55155/how-to-assign-multiple-editorapplicationupdate-del.html
http://answers.unity3d.com/questions/155738/how-to-typecast-a-method-holder-in-javascript-like.html

The question is not retarded. The use of “JavaScript”, well… :roll_eyes:

Just responding in case anyone out there wonders how I got on with this…

Well roughly a week on and I am beginning to see the light on using C# from the beginning. I wanted to develop a delegate GUI system using Javascript but I find that there is no work around for the abscence of adding (+=) and removing methods (-=) to the delegate this.currentGUIMethod. The best thing I was hoping for was being able to write a small script in C# (held in the Plugins folder) that would allow me to use this. However, while I have no doubt that its possible the lack of two way referencing (because the C# script must be compiled first) and my poor scripting skills have made me abandon such an idea.

I even tried to develop a callback method that might allow me to do the same thing purely in JS but this defeated me as well. Oh well at least I learnt something.

You can use delegates in UnityScript too from, but you have to use Delegate.Combine/Remove methods, instead of += and -= which are just overloads that use the below methods for easier usage.

Fairly sure this isn’t supported but could I be a pain and press you for an crude example. My implimentation brings

The name ‘Delegate’ does not denote a valid type (‘not found’). Did you mean ‘System.Delegate’?

There is a nice delegate example but the JS version does not make use of the type delegate

http://www.unifycommunity.com/wiki/index.php?title=MainMenu

I am attempting to do

this.currentGUIMethod = MainMenu;
this.currentGUIMethod = Delegate.Combine(DropMenu);

which knowing my poor skills would be wrong even if they were supported.

Well, you need to add the

import System; for UnityScript or using System; for C# at the top of your script. Or use System.Delegate, both get the same effect.

My friend you are a genius, a giant amongst men. You know I’ve wasted so much time trying to figure that one out. There are loads of redundant posts lying around that led me to believe it wasn’t possible. I’m not a programmer by trade (bet you’d guessed that lol) took some Java/SQL/Python at Uni but that was back in 2004 (Bioinformatics MSc). I work in labs mostly, think I need to learn some fundamentals.

One more question about delegates…

Is there a way to setup a delegate variable from inspector ?

I mean choose wich method to call for one delegate ? I tried some things but it appears that

public delegate void method();
public method the_choosen_method;

don’t show in inspector. Neither the possibility to assign anything.

This would be damn usefull ! but am not sure this can be achieved.
If some Unity GodCoder comes around, maybe he can light my candle ?

Thanks a lot.

No, delegates are not serializable. You would have to write your own EditorScript.

You could of course use a string variable to save the method name and use reflections, but that’s pretty messy and not worth the effort. And it’s better this way, because in most situations you must subscribe AND unsubscribe the delegate, in order to prevent memory leaks.

i.e. if you have Object A which subscribes to Object B and you destroy Object A, it’s memory won’t be collected by the garbage collection if you have forgotten too unsubscribe. Only when Object B gets destroyed to, the Garbage collection will be able to release memory from Object A.

If it’s Object B is a static object for example, it will never be released and you can have a serious memory leak which leads to crashes or bad performance.