Array

From Game Editor

Revision as of 21:56, 27 June 2009 by Skydereign (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

An array is used to categorize similar variables together, by using the same name. When declaring a variable, you would attach [x] after the variable name, x being the number of variables you want the array to hold. What this does is create x number of that variable, allowing for a single variable name. This prevents the need for variables like this, enemyHP_1, enemyHP_2, enemyHP_3, and so on. An important thing to remember is that the first variable is [0].

Declaring an Array

There are two ways of declaring variables, using C, or gameEditor's built in variables, Creating variables. Variables must be declared early on, so if you are creating an event local array, you would declare it at the top. You can either put the declaration in global code, so all actors can see it, or in a script of an actual event. In the latter case, the variable array is only accessible by that script.

Global Code

int enemyHP[10];

This would create 10 variables, called enemyHP[0], enemyHP[1], enemyHP[2], through enemyHP[9]. Since it is in global code, all actors can see it. Using this, you can not only quickly reference these variables by index, you can link them to an actor, the number within the [] would tie directly to cloneindex.


Advantages

One of the biggest advantages to using arrays is for quick indexing. If you needed to zero out, or reset all HP variables it would require you to set each one. In script, using these can get very convoluted. Here is an example of a simple set stats of an actor. This uses enemyHP and enemyMP.

enemyHP_1 = 10;
enemyHP_2 = 15;
enemyHP_3 = 20;
enemyHP_4 = 25;
enemyHP_5 = 30;
enemyHP_6 = 35;
enemyHP_7 = 45;
enemyHP_8 = 50;
enemyHP_9 = 55;
enemyMP_1 = 5;
enemyMP_2 = 6;
enemyMP_3 = 7;
enemyMP_4 = 8;
enemyMP_5 = 9;
enemyMP_6 = 10;
enemyMP_7 = 11;
enemyMP_8 = 12;
enemyMP_9 = 13;

Now this is not entirely difficult to read, but this is assuming that there are only 9 enemies. Also, this might be for a type of enemy, explaining the reason for different HP, MP sets. So, this would mean if you wanted multiple enemies of each type you would need to multiply that all by x more, being the number of enemies from each type. Later in game development, you might want to change the values, but then you would need to change everything. You could use actor variables, and/or CreateActor event to set these, but the problem can still occur if you aren't paying attention. If you were to use arrays, you could do it this way.

int i;
for(i=0; i<9; i++)
{
    enemyHP[i] = 5*(i+2);
    enemyMPpi] = 5+i;
}

This would set all of those variables to the same values. Again be warned that the variables go from 0-8, not 1-9. If you were to have multiple enemies of each type, you could simply add another dimension to the array. Do not add dimensions freely, as each dimension is consuming memory. If you can avoid dimensions above 3, do so.

int i;
int j;
for(i=0; i<9; i++)
{
  for(j=0; j<10; j++)
  {
    enemyHP[i][j] = 5*(i+2);
    enemyMPpi][j] = 5+i;
  }
}

This would set 10 enemies from each type. Using individual variables to hold each one of these would require 90 variables, meaning at least 90 lines of code. This requires 10, 12 including the array declarations.

Extra Dimensions

To create another dimension, simply add it to the end.

enemyHP[2][3];

Do note that it acts sort of like a grid.

[0][0] [1][0]

[0][1] [1][1]

[0][1] [1][2]