Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: tassieboy on Sat 21/06/2008 08:59:00

Title: Declaring Global Variables
Post by: tassieboy on Sat 21/06/2008 08:59:00
Hello. I'm working on my first AGS game.

I am wondering if it is possible to declare global variables that will retain their values between rooms. I have searched the manual and BFAQ and have only found reference to pre-defined globals.

I want to use a boolean flag, which is set in one room, and checked in another.
I have tried declaring my boolean in the globalscript.acs file, and even tried preceeding it with "global",  but it appears to be out of scope when I try to access it from a room.

Is there something simple I am missing? Any assistance appreciated.
Title: Re: Declaring Global Variables
Post by: Maverick on Sat 21/06/2008 09:42:39
Hi,

Look up import and export in the manual.

After you define the global variable in global script you must export it as per the example below. Take note that when you export the "type" of variable is left out!!

Global script file

bool Phone_Ringing_Cassey;
bool Phone_Ringing_Police;
bool Phone_Ringing_Taxi;
export Phone_Ringing_Cassey,Phone_Ringing_Police,Phone_Ringing_Taxi;


To be able to access these variables from any other room in the game you can then import them into the script header. Variable type is specified on import!!!

Script header

import bool Phone_Ringing_Cassey,Phone_Ringing_Police,Phone_Ringing_Taxi;


I hope this helps
Title: Re: Declaring Global Variables
Post by: tassieboy on Sat 21/06/2008 10:25:11
Cool. Thankyou for your prompt help. This was exactly what I needed.
Title: Re: Declaring Global Variables
Post by: Dualnames on Sun 22/06/2008 10:35:45
Or you can go at the script header (global) and do this:
bool Phone_Ringing_Cassey;
bool Phone_Ringing_Police;
bool Phone_Ringing_Taxi;


instead of importing exporting.
Title: Re: Declaring Global Variables
Post by: Lt. Smash on Sun 22/06/2008 10:42:17
OR you can use the new Global Variables pane from the 3.0.2 editor.
Title: Re: Declaring Global Variables
Post by: Khris on Sun 22/06/2008 10:47:04
Quote from: Dualnames on Sun 22/06/2008 10:35:45
Or you can go at the script header (global) and do this:
bool Phone_Ringing_Cassey;
bool Phone_Ringing_Police;
bool Phone_Ringing_Taxi;


instead of importing exporting.

No.
This will create separate, independent variables for each room.
Why the hell would everybody go the import/export route if it wasn't necessary...!? ::)

EDIT: You gave the same, wrong advice in RetroJay's thread...
Title: Re: Declaring Global Variables
Post by: Dualnames on Sun 22/06/2008 10:58:08
You're right Khris. I'm misinforming people. Well, didn't manage to see what you said happening due to I usually script games a certain same way. So, ok, people nevermind my replies about the global header.

And still, sorry again. Trying to be helpful.
Title: Re: Declaring Global Variables
Post by: Makeout Patrol on Sun 22/06/2008 22:12:29
There's also the new "global variables" pane in AGS 3.0.2, so that you don't have to bother with all the importing business.
Title: Re: Declaring Global Variables
Post by: Khris on Sun 22/06/2008 22:59:52
Quote from: Lt. Smash on Sun 22/06/2008 10:42:17
OR you can use the new Global Variables pane from the 3.0.2 editor.
Title: Re: Declaring Global Variables
Post by: Makeout Patrol on Mon 23/06/2008 07:54:22
Quote from: KhrisMUC on Sun 22/06/2008 22:59:52
Quote from: Lt. Smash on Sun 22/06/2008 10:42:17
OR you can use the new Global Variables pane from the 3.0.2 editor.

Whoops, looks like I'm blind
Title: Re: Declaring Global Variables
Post by: TibbaGames on Sun 29/03/2020 23:36:24
Quote from: Khris on Sun 22/06/2008 22:59:52
Quote from: Lt. Smash on Sun 22/06/2008 10:42:17
OR you can use the new Global Variables pane from the 3.0.2 editor.

Hello, I'm having an issue with this, if I define a String as Global Variable in the new Global variables Pane (for example some text that the player will repeat as an unhandled action) I can't find the content in the translation file. Any help will be appreciated
Thanks
Title: Re: Declaring Global Variables
Post by: Khris on Mon 30/03/2020 10:22:25
I guess the reason is that variables usually don't need to be translated, only constants and string literals do.

Unhandled event texts shouldn't occur more than once in your code, so there's no need to use variables for them in the first place.
Unless of course you want them to change during the game, that's a different story.

Anyway, if you declare a global variable using import/export, the string you assign to it does end up in the translation file:

Code (ags) Select
// header

import String LookUnhandled, TakeUnhandled;

// main script

String LookUnhandled, TakeUnhandled;
export LookUnhandled, TakeUnhandled;

void game_start() {
  LookUnhandled = "You don't see anything special.";
  TakeUnhandled = "You can't take that.";
}