Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Knox on Mon 24/05/2010 00:58:27

Title: How to add your global variables in a function's parameters?
Post by: Knox on Mon 24/05/2010 00:58:27
Ive recently learnt how to make my first function with parameters and it looks like this:

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;


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';


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
Title: Re: How to add your global variables in a function's parameters?
Post by: Khris on Mon 24/05/2010 10:42:59
You can't use the contents of a string as variable name.

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

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:

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();
}
Title: Re: How to add your global variables in a function's parameters?
Post by: Knox on Tue 25/05/2010 18:19:31
Hi Khris,

Ok Im going to see what I can do with this for now. Thanks!