Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: MrAbu on Wed 28/02/2018 00:59:49

Title: changing global variables in the room script.
Post by: MrAbu on Wed 28/02/2018 00:59:49
As the title says, is it possible to take a global variable, change the value in the room script, and then use the changed variable back in the global script?

header
Code (ags) Select

int iCharAct[23];         // header declaration


room script
Code (ags) Select

iCharAct[17] = 1; // changing value in room script


Global Script
Code (ags) Select

if (iCharAct[17] == 1)
  {
player.Say("1");            // should say this if iCharAct[17] is 1, but he doesn't
  }


The player should say "1" if iCharAct[17] was changed properly, but somewhere it was dropped. Any way to fix this or am I not allowed to change variables in the room script?

Thanks.
Title: Re: changing global variables in the room script.
Post by: Crimson Wizard on Wed 28/02/2018 01:24:27
You are making a classic mistake (people get into this problem every now and then).
AGS Script works like this: everything found in the script headers is being copied to each of the script modules (including room ones).
So when you declare a variable in the header like this, you actually create multiple non-connected variables, one per each module and per each room.


To make it work properly, in AGS script's header you need to declare an "import" of a variable:
Code (ags) Select

import int iCharAct[23];         // header declaration

This declaration means that there is ONE instance of variable somewhere.

Then in the Global script itself you need to actually create and export this variable:
Code (ags) Select

int iCharAct[23];
export iCharAct;

This means that you are making this one variable available for use in other modules under such name.


More information about these keywords may be found in the manual: http://www.adventuregamestudio.co.uk/manual/ags46.htm#importkeyword
Title: Re: changing global variables in the room script.
Post by: MrAbu on Wed 28/02/2018 01:43:26
Seems awfully long-winded but it worked, thanks!