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.
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!
Thank you, that is what I needed to know. :)
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;
;D I was sleeping, I guess...
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.
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)
Ok! Oh, I think I had tried some like this time ago, but i think I forgotten the " " . Thanks! You were very fast!!!
http://bfaq.xylot.com/#coding01