Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: CocodaMonkey on Fri 25/03/2005 08:16:54

Title: Make a global variable?
Post by: CocodaMonkey on Fri 25/03/2005 08:16:54
Is it at all possible to make a global variable in AGS?

Right now I've got something like


Ã,  function dialog_request (int xvalue) {
Ã,  Ã,  if (xvalue == 1){
Ã,  Ã,  Ã,  gamevar++;
Ã,  Ã,  }
Ã,  }

Obviously that isn't the whole piece of code but it's all that is relavent to my question.

Of course this won't work because gamevar isn't a variable in there. I also can't define it inside of the function because I need it's old value to be retained whenever this function is called, and if I define it in there it will reset it each time the function is called.

I tried adding the variable to the game_start() function. That seems to work, but it doesn't make it a global variable so it doesn't help me any.
Title: Re: Make a global variable?
Post by: DoorKnobHandle on Fri 25/03/2005 09:08:54
There are two ways to work with variables in AGS:

1. GlobalVars

You don't need to declare those, simple use:


function dialog_request (int xvalue) {
   if ( xvalue == 1 )
   {
      SetGlobalInt ( 1 ) = GetGlobalInt ( 1 ) + 1;
   }
}


You can access those pre-defined GlobalVar with the functions GetGlobalInt or GetGlobalString and assign values to them by using SetGlobalInt or SetGlobalString.

The parameter is the "id" number of the variable. In that example we set the global variable 1 to whatever it used to be plus 1 ( which equals the game_var++ command by the way, but can't use that ++ on GlobalVars since you access those using functions )...

2. "Real" Variables

Now to the real stuff: You can also define a variable in your global script header ( press CTRL+H ) and then define it like this:


int game_var;


"int" defines it as integer, the following characters represent the name... ( you can use string or char as well to define a string! )

Now close and save the header file and switch to your script file ( press CTRL+G ) and you can use the game_var just like you described... It will be accessable in every room, character, object or hotspot script...

Use it like this:


function dialog_request (int xvalue) {
   if ( xvalue == 1 )
   {
       game_var++;
   }
}


If you have further questions please ask!
Title: Re: Make a global variable?
Post by: CocodaMonkey on Fri 25/03/2005 09:33:13
Thank you, that is what I needed to know. :)
Title: Re: Make a global variable?
Post by: on Fri 25/03/2005 14:49:51
1. It's

  SetGlobalInt(1, GetGlobalInt(1) + 1);

2. If you define a variable in the main script header, SEPERATE variables will be created for the global script and each room script.
To make a single global variable available from all scripts, define it in the global script, export it and import in the main script header:


// global script

int game_var;
export game_var;



// main script header

import int game_var;
Title: Re: Make a global variable?
Post by: DoorKnobHandle on Fri 25/03/2005 14:51:40
;D I was sleeping, I guess...
Title: Re: Make a global variable?
Post by: Reminder on Wed 06/04/2005 12:15:25
Hi!
This global scripts are the same that those we can configure in the Interaction Editor?

I use in my game variables defined with the Interaction Editor and I modify this variables whit "Set variable value..." and the game runs perfectly. But when I want to modify them with the script the variables are not inicializated.

How I can change the variable's value defined by menus in the script?
Thanks.
Title: Re: Make a global variable?
Post by: Ashen on Wed 06/04/2005 12:21:34
No, global variables aren't the same as those you set in the interaction editor. To use those in scripting, you need to use GetGraphicalVariable (variable) and SetGraphiclaVariable(Variable, amount)
E.g.:

if (GetGraphicalVariable ("my variable") == 1) { // Don't forget the " " around the name
  SetGraphicalVariable ("my other variable", 3);
}

(Look them up in the manual for more details)
Title: Re: Make a global variable?
Post by: Reminder on Wed 06/04/2005 17:27:59
Ok! Oh, I think I had tried some like this time ago, but i think I forgotten the " " . Thanks! You were very fast!!!
Title: Re: Make a global variable?
Post by: TerranRich on Fri 08/04/2005 14:32:30
http://bfaq.xylot.com/#coding01