changing global variables in the room script.

Started by MrAbu, Wed 28/02/2018 00:59:49

Previous topic - Next topic

MrAbu

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

int iCharAct[23];         // header declaration


room script
Code: ags

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


Global Script
Code: ags

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.

Crimson Wizard

#1
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

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

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

MrAbu

Seems awfully long-winded but it worked, thanks!

SMF spam blocked by CleanTalk