Author Topic: changing global variables in the room script.  (Read 163 times)

changing global variables in the room script.
« on: 28 Feb 2018, 00:59 »
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: Adventure Game Studio
  1. int iCharAct[23];         // header declaration
  2.  

room script
Code: Adventure Game Studio
  1. iCharAct[17] = 1; // changing value in room script
  2.  

Global Script
Code: Adventure Game Studio
  1. if (iCharAct[17] == 1)
  2.   {
  3.  player.Say("1");            // should say this if iCharAct[17] is 1, but he doesn't
  4.   }
  5.  

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

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: changing global variables in the room script.
« Reply #1 on: 28 Feb 2018, 01:24 »
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: Adventure Game Studio
  1. import int iCharAct[23];         // header declaration
  2.  
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: Adventure Game Studio
  1. int iCharAct[23];
  2. export iCharAct;
  3.  
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
« Last Edit: 28 Feb 2018, 01:44 by Crimson Wizard »

Re: changing global variables in the room script.
« Reply #2 on: 28 Feb 2018, 01:43 »
Seems awfully long-winded but it worked, thanks!