How to add your global variables in a function's parameters?

Started by Knox, Mon 24/05/2010 00:58:27

Previous topic - Next topic

Knox

Ive recently learnt how to make my first function with parameters and it looks like this:
Code: ags

function off_SoTurnOn(this Button*)
{
  if (this.NormalGraphic == 1175)
  {
    this.NormalGraphic = 1176;
    this.PushedGraphic = 1175; 
    this.MouseOverGraphic = 1184;
  } 
}


I was wondering how can I add a global variable as  the parameter...something like this;

Code: ags

function myFunction(this GlobalVariable*)
{
   //do something
}


Basically I just want to be able then to type in the name of my global variable in another function, (myVariable.myFunction) and it will execute more script.

Since I couldnt figure it out, I tried to do this instead as a work-around but I get an error I cant convert 'Button*' to 'const string';

Code: ags

function panelSettingsRestore(this Button*)
{ 
//the name of my variable is the same as the button name, plus "Restore" (btnMyVariableRestore)

 String globalVarName = (this + "Restore");  
 if (globalVarName == true)
  {
    this.off_SoTurnOn();
  }
  else
  {
    this.on_SoTurnOff();
  }

}

So how do I "get" the String "contents" from a Button* name and be able to add other pieces of "string" to make a new string? ...and even better, how do I just add global variables I have declared at the top of my global script into a parameter for a function?

Hope this makes sense :P
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

You can't use the contents of a string as variable name.

To add parameters to custom functions, declare them inside the parentheses.
Example:

Code: ags
function DisplaySum(int a, int b) {
  Display("%d + %d = %d", a, b, a+b);
}


What you want is a variable for every button. Every GUI object has an ID, starting at 0, so you could use an array for that:

Code: ags
bool panelButtonRestore[20];   // creates 20 variables, panelButtonRestore[0] through panelButtonRestore[19]

function panelSettingsRestore(this Button*) { 
  if (panelButtonRestore[this.ID]) this.off_SoTurnOn();
  else this.on_SoTurnOff();
}

Knox

Hi Khris,

Ok Im going to see what I can do with this for now. Thanks!
--All that is necessary for evil to triumph is for good men to do nothing.

SMF spam blocked by CleanTalk