create reference to function

I feel I should be able to create references to functions in unity javascript. I’ve done in other languages.

What I’m trying to do is call a particular function based on a certain case. Rather than repeating the call in each case, I’m trying to follow OOP and just set a reference and call my code at the end of my case checking.

Here is the code:

function onGUI()
{
        var windowCreateFunction;       /* reference to desired function */
        switch( activeID )
        {
                case SCR_TITLE:
                        windowCreateFunction = CreateTitleScreen;
                        break;
                case SCR_SCORES:
                        windowCreateFunction = CreateScoresScreen;
                        break;
                case SCR_SELECTRACE:
                        windowCreateFunction = CreateRaceScreen;
                        break;
                case SCR_HOWTO:
                        windowCreateFunction = CreateHowToScreen;
                        break;
                default:
                        windowCreateFunction = CreateTitleScreen;
                        break;
        }


        windowRect = GUI.Window ( activeID, windowRect, windowCreateFunction, "How To Play" );
}

function CreateTitleScreen()
{
    // setup code
}

function CreateHowToScreen()
{
   // some more code stuff
}
etc....

Error occurs at runtime “Cannot cast from source type to destination type.” I’ve tried type setting my var windowCreateFunction and that hasn’t made my script happy either.

thx
-s

I don’t use JS, so I don’t know how far you will get with this, but try casting your functions as

GUI.WindowFunction

That’s the delegate type used for the standard GUI callback, and it has worked for me in C#.

HTH,
-Jeremy

Oh btw, your window functions need to take the window ID int as a parameter.

-Jeremy

thanks jeremy! WindowFunction was the type i needed to set my variable too.

-s