Passing Function AS parameter [SOLVED]

Started by johto760, Sat 15/09/2012 12:23:27

Previous topic - Next topic

johto760

Hello everyone,

Like what the title says,


I'm trying to make a dynamic text box, where I can control hopefully "dynamically" what is written in the label, or what the confirm and cancel button does.
In order for me to do this, I think I should pass a String to some function and use that as the label, and pass a function and put it in a global variable where my btnConfirmTextBox_Click() and btnCancelTextBox_Click() functions can access them.

I have something like:

Code: AGS

function createTextBox(function confirmCallback, function cancelCallback)
{
    // When I do something like this:
    confirmCallback();
    // I get an error: GlobalScript.asc(561): Error (line 561): PE04: parse error at 'confirmCallback'
    // When I do it like this:
    confirmCallback;
    // I still get the error: GlobalScript.asc(561): Error (line 561): PE04: parse error at 'confirmCallback'
}


How does passing a function as parameter work anyways? does it even work?

I put the code above in my GlobalScript.asc, then I call it from one of the rooms after it fades in,
I create a function in some room like this:

Code: AGS

// This should be the function that will be called in the GlobalScript.asc
function generic()
{
    Display("Yeah");
}

/* I just realized while writing this post that GlobalScripts.asc could not call scripts from local scripts hehe, does this fact affect my question? I hope not ^_^,*/

/*If GlobalScripts cannot access/call functions from local scripts, then passing a function from a local script to the globalscript is impossible? does that make sense at all?"*/


function room_AfterFadeIn()
{
    // I tried doing something like this:
    createTextBox(generic(), generic());


    // and something like this
    createTextBox(generic, generic);
 
}



ADDITIONAL INFO:

I'm using (Build 3.2.1.111) by the way, I think it's the latest version

I just saw that I cannot use a function as a global variable... *sigh*.... how would I go about this?


The goal is still the same:

create a textbox where I can change "on the fly" what my btnConfirmTextBox_Click() and btnCancelTextBox_Click() does.



I think I've solved it... *SIGH*


Since the value of a textbox would always be a String, Instead of finding a way to store a function in a global variable, I'm just gonna have to store whatever value the text of the textbox is on a global String variable..

something like:
Code: AGS


function btnConfirmTxtBox_OnClick(GUIControl *control, MouseButton button)
{
  valueOfContent = txtContent.Text;

    // Where String valueOfContent as Global Variable
}



Sorry for the trouble guys...

Snarky

You can't pass functions as parameters, I'm afraid. In this case you should just pass the String; otherwise you may just have to hardcode the function to be called (or switch between a handful of possible functions depending on an enum parameter, for example).

In some cases you could also break up the function into two separate ones around the function call that you want to pass, requiring the caller to call them both in sequence with the right function call in between:

Code: AGS

{
    // ...
    // This is how you might call the functions
    temp = createTextBox1(x,y);
    s = selectString(temp);
    createTextBox2(x,y,temp,s);
    // ...
}


But generally, if you find yourself trying to pass functions as parameters, you're not working the way the language was meant to be used. There should usually be a simpler and more appropriate way to do it.

SMF spam blocked by CleanTalk