Declaring Global Variables

Started by tassieboy, Sat 21/06/2008 08:59:00

Previous topic - Next topic

tassieboy

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.

Maverick

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
Code: ags

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
Code: ags

import bool Phone_Ringing_Cassey,Phone_Ringing_Police,Phone_Ringing_Taxi;


I hope this helps

tassieboy

Cool. Thankyou for your prompt help. This was exactly what I needed.

Dualnames

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.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Lt. Smash

OR you can use the new Global Variables pane from the 3.0.2 editor.

Khris

#5
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...

Dualnames

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.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Makeout Patrol

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.

Khris

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.

Makeout Patrol

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

TibbaGames

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

Khris

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
// 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.";
}

SMF spam blocked by CleanTalk