Complete beginner at arrays...

Started by De-Communizer, Sun 23/08/2009 20:03:43

Previous topic - Next topic

De-Communizer

Up until now, I've been using the "Global Scripts" menu to make my variables, but now I need to make use of an array or several. Unfortunately, trying to put variables in via the script is confusing me totally, and anything in the Beginner's section seems to have been written for pre-3.0 versions. Hopefully, someone can point me in the right direction!

In the "room_FirstLoad" for room1, I've got the following -

function room_FirstLoad()
{
int type[100];
type[1] = 1;
}

So, from what I can tell so far, I've created an int array called "type", which has 100 sub-sections, and the value of subsection 1 of this array is "1". When the room loads in future, I want a label on a GUI to show the value of this int within the array. To that end, I've got:

function room_Load()
{
Box1.Text = String.Format("%d",type[1]);
}

Of course, I'm certain I'm missing something - I keep reading about "headers" and "defining" variables, but I've no idea where to put it. As a result, I get an error about "undefined symbol 'type'", and no information in the GUI label. What else do I therefore need to do to make an array work?

Many thanks in advance!

Khris

First of all, if the array is only accessed from within one room's functions, it's sufficient to declare it in the room script.
Simply move the declaration line to the top of the room script, outside any function, so every function can "see" it.

Yes, int type[100] creates 100 variables, type[0] to type[99].

Now, if you need the array to be global, e.g. if you want to change its values from the global script as well as room scripts, move its declaration to the top of the global script, then put "export type;" beneath that.
In the global header, add "import int type[100];"

monkey0506

Just to further add to your confusion (:P) if you don't know exactly how many variables you need and it's something that can change you can use a dynamic array. It works exactly the same as a normal array except for how it's declared.

Say for example you wanted to store some additional information about all of the characters in your game. As of AGS 3.0 the AGS_MAX_CHARACTERS constant has been removed, so how can you do that? Do you have to simply know how many characters you have?

No, that's the type of situation where a dynamic array comes in. The reason it's called a dynamic array is because the size of the array does not have to be a constant value. Once you create the array its size will stay the same, but you don't have to explicitly type "100". You can actually specify a variable for the size of the array (which will not work with a normal array).

So, how do you do it then? Like this:

Code: ags
// top of global script
int charHealth[]; // let's add a Health variable for each of our characters
// Note that here you have NOTHING between the brackets

function game_start() { // runs at the very beginning of the game, before any rooms are loaded
  charHealth = new int[Game.CharacterCount];
  // Note that you only type the NAME of the array here, not the brackets after it
  // We then use the NEW keyword to let the engine know that we want to create a NEW dynamic array
  // Then we put the type of array, here we have an INT array
  // Finally, inside of brackets we have the size, here we want one item in the array for each character in the game
  // Now that we have the array created, we should give our characters some health
  int i = 0;
  while (i < Game.CharacterCount) { // loop through the characters
    charHealth[i] = 100; // default everyone to 100 health
    i++;
  }
}

// wherever - character loses health
  charHealth[player.ID] -= 20; // player loses 20 health

// wherever - character gains health
  charHealth[cEnemy1.ID] += 5; // cEnemy1 gains 5 health

SMF spam blocked by CleanTalk