Page 1 of 1

Arrays and GE for nOObs - small tutorial

PostPosted: Mon Aug 18, 2008 1:29 am
by feral
This is a brief beginners lesson in ARRAYS... for those using GE and scripting for the first time..

I started to write this up to answer a request from another member, and decided it "may" be of use to others..
if any of the more advance programmers spot an error.. and/or have advice on how to improve this please PM me..

Also, I know....Arrays have been explained elsewhere, but I felt that some of the explanations are a bit brief... or too advanced... or too "C programming oriented"

So what is an Array ?

If you have been using GE for a little while now, and, you have already used clones, then you have probably been using the cloneindex..., to select or change clones,.. if so...., then you already know how to use arrays. :D

Or, if you have already used the built-in actor variables such as.... x and y positions,... or animpos,... or directional_velocity, or any of those variables then you already know how to use arrays :D

All the "Actor's" in GE are built using a special type of array called a "struct" ( you don't need to know about structs yet) but essentially ..it means that you have been using arrays already.....

You have been using variables that are called the same thing, but they all have different values depending on which actor they are attached to.

what is an array ?

just imagine this,... you have a player and you want to store the health points (we will call them HP's) and ammo.

you might create a variable called "playerHP" and another called "playerAMMO"

Now... you need an enemy with the same settings,.. so.... you create variables called ENEMY_HP, and ENEMY_AMMO.

This is all good,..... until you want more then one enemy !! at this point (if you don't know about arrays) you would probably do something like
enemy_HP_1 =12;
enemy_HP_2 =10;
enemy_HP_3 = 9;
etc

as you can see this gets very untidy after more then a few enemies. and if you need to use the values ( for example to destroy all enemys if the HP is less then zero)

then you often have to do something like

Code: Select all
if (enemy_HP_1<0)
{
//the code to destroy actor   enemy.1;
}

if (enemy_HP_2<0)
{
//the code to destroy actor   enemy.2;
}

and so on for every enemy



Instead of selecting every different variable for every different enemy...,

you could use an "array".

An array is a special group of variables that can be NUMBERED and INDEXED but CAN ALL BE NAMED THE SAME

If you think of an Array as a "look up" table which contains a "list" of different values, but is indexed by number ( you can find the required value using its index number) , then you are half way there

You have been using something similar in GE with clones, think of ....clone.1 clone.2 clone.3 etc ( note: the clone and cloneindex is part of the Actors "struct" - which a special form of an array, but... you don't need worry about structs now.. however, if you think of arrays as acting the same way as the clone index number you are more then half way there.. :) )

With arrays you do a similar thing as you have been doing with clone indexes, ....except with arrays you refer to each of the different variables using a set of square brackets around the index number eg enemy[1], enemy[2], enemy[3] etc

and NOT the DOT as you have been using with clones ......enemy.1 enemy.2......... but... it works the same way

Arrays allow you to create a group of the "same" type of variable, with the "same name" for every instance that you need it.

so instead of the previous way of doing it...
enemy_HP_1
enemy_HP_2
enemy_HP_3

you can now use
enemy_HP[1];
enemy_HP[2];
enemy_HP[3];

This may seem the same, but, it has one major advantage.. you can now select the enemy you want simply by using the index number of the array

eg: enemyHP[3]=12;

This is very useful when you are using variables to track a certain enemy, or you wish to cycle thru all the enemy's...

because you can now say

i=3;
enemyHP[i]=12; //make the HP of enemy i (number 3) = 12


or
for (i=1;i<=3;i++) //count from 1 to 3
{
enemyHP[i]=12 //make the enemy's HP (1 thru 3) equal to 12;
}

HOW DOES IT WORK ?
When you need an array you have to create it, like any other variable.

for example, to create a normal single variable called HP you would say
int HP;

to create an array of the same variable, you do the same thing, BUT you have to tell GE how many of this type of variable you will need. so... if you want to store the HP for 5 enemies you would say

int HP[5];

what this then does..... is allocate memory space for 5 int (integer) variables ALL called HP... but, it stores them in an "indexed array"

in memory it looks like a "table"
like this

HP = value
____________
HP[0] = 0
HP[1] = 0
HP[2] = 0
HP[3] = 0
HP[4] = 0


And, as you have only just created the table, all of the values =0; (just like creating normal variables)

now... if you were to say
HP[1]=12; //change the HP of index 1
HP[4]=6; //change the HP of index 4

your table would look like this

HP = value
____________
HP[0] = 0
HP[1] = 12
HP[2] = 0
HP[3] = 0
HP[4] = 6

why do it this way ?

well imagine you want to check the Health Points of all the enemies, and destroy any that have a health point less then zero.

WITHOUT arrays.... you would have to do something like
Code: Select all
if (enemy_HP_1<0)
{
//the code to destroy actor   enemy.1;
{

if (enemy_HP_2<0)
{
//the code to destroy actor   enemy.2;
{

and so on... for EVERY enemy... because every enemy_HP_x is a unique variable.....but, as we have now stored all the enemy HP's in a "single" array (or table) we can now say

Code: Select all
for (i=0;1<5;i++)
{
      if (enemyHP[i]<0)   ///go through the table of arrays and check value if less then zero
     {
//the code to destroy actor   enemy.i;
     {
}


and we are done, we have cycled thru the entire "array" of enemys in one loop.

A special note at this point: ...when you create an array of .. say.... 5 enemyHPs ... the INDEX of the array always starts at 0 and finishes at the number BEFORE the amount you asked for.

eg: an array of 5 objects would be numbered
0,1,2,3,4

an array of 10 objects would be numbered
0,1,2,3,4,5,6,7,8,9

So, in summary, an array is simply a way of creating a lot of variables which are all the "same" and then being able to call them , or change them,... by using an index number..

this is a simplified version , but is for beginners..if you don't still understand it let me know (pm me if you can, so we don't make this thread too convoluted or long) and then I will edit this post.....

IF this has been useful for anyone, I will do another example using "2 dimensional arrays", and explain (simply) how arrays relate to "structs" ( structs the way the Actors are made in GE.... and are a special type of array).... let me know..