Array values not being saved between rooms?

Started by Firgof, Wed 15/04/2015 03:34:52

Previous topic - Next topic

Firgof

I have a script (available here: http://pastebin.com/BhK8w64A) which saves data to an array which I access in a later room.

However, despite that array being initialized in GlobalScript.ash (and thus, I imagine, universally global) - I seem to be losing the data when I switch to another room.

Can anyone assist me in debugging this?  Time is a factor - I have to have this ready to go by tomorrow or I'll just have to scrap it and take it out of my game (I'm participating in a gamejam and have a time limit for how long I can spend working on it).

Snarky

#1
I can't see from a quick look at the scripts which array you're talking about, and you don't seem to have included the global script, so there'd be no way to tell from just the files included what was going on, anyway.

However, most likely, the problem is "that array being initialized in GlobalScript.ash". Not sure exactly what you mean by this, since you say you're saving the values in the room file, but you should practically never define or initialize values in the header. The right way to do it would be to initialize the array in GlobalScript.asc, and just put the import statement in the .ash file. For example:

Code: ags
// GlobalScript.asc
// Array definition
int myArray[200];
export myArray;

function game_start()
{
  // Initialize array
  int i=0;
  while(i<200)
  {
    myArray[i] = Random(10);
    i++;
  }
}


Code: AGS
// GlobalScript.ash
import int myArray[200];    // Array declaration

Crimson Wizard

#2
One misconception about AGS script is that if you put normal variable declaration in script header, it will be one variable for all the scripts.
The thing is, however, that the contents of the headers are plain copied to every script.
This means, that if you write something like:
Code: ags

int myArray[200];

in the GlobalScript.ash (header), this will create individual array "myArray" in every script. These arrays will not be related and viewed as different entities.

So, if then you write
Code: ags

myArray[0] = 10;

in room 1, and then
Code: ags

Display("value = %d", myArray[0]);

in room 2, it will still display 0, not 10. That is because room 2 will have its own "version" of "myArray".

What you should do, is follow Snarky's example, and declare array as "import myArray[200]" in the header, and put actual variable in script's body (like "int myArray[200]"). This way there will be only one version of "myArray", but it will be accessible in all the room scripts too.


PS. Of course, it is not necessarily to do array value initialization in global script too; you may do that in one of the rooms, since the array is shared now.

SMF spam blocked by CleanTalk