Advanced RMS: Part 6 – Arrays

By Matei of Woad Creations
created 9/10/03

In programming, an array is a kind of variable which stores multiple values under one name. For example, you might have an array of 10 integers called ar. Then you could write ar[1], ar[2], ar[3], etc to access each integer in the array. This feature is vital anywhere you might be dealing with multiple numbered objects – and a Random Map Script\046nbsp;is such a place. For example, instead of having 12 army ID variables, you might want to store players’ army ID’s in an array of 12 ints. Arrays are not supported by the XS scripting system, but luckily there is a way to make them using functions and regular variables :).

For example, say you want an array of integer army IDs, one for each player in your map. Since there will be at most 12 players, numbered 1 to 12, you can create 12 variables called army1, army2, army3, …, army12. To access these variables easily, you can create two functions: getArmy(int playerNum) and setArmy(int playerNum, int value). Then you can, for example, call getArmy(5) to get player 5’s army. Here’s the code for it:

int army1=0; int army2=0; int army3=0; int army4=0; int army5=0; int army6=0; int army7=0; int army8=0; int army9=0; int army10=0; int army11=0; int army12=0; int getArmy(int p=1) {
    if(p==1) {return(army1);} else if(p==2) {return(army2);} else if(p==3) {return(army3);} 
    else if(p==4) {return(army4);} else if(p==5) {return(army5);} else if(p==6) {return(army6);}
    else if(p==7) {return(army7);} else if(p==8) {return(army8);} else if(p==9) {return(army9);} 
    else if(p==10) {return(army10);} else if(p==11) {return(army11);} else if(p==12) {return(army12);}
}
void setArmy(int p=1, int v=0) {
    if(p==1) {army1 = v;} else if(p==2) {army2 = v;} else if(p==3) {army3 = v;} 
    else if(p==4) {army4 = v;} else if(p==5) {army5 = v;} else if(p==6) {army6 = v;} 
    else if(p==7) {army7 = v;} else if(p==8) {army8 = v;} else if(p==9) {army9 = v;} 
    else if(p==10) {army10 = v;} else if(p==11) {army11 = v;} else if(p==12) {army12 = v;} 
}

This is a pretty powerful technique because it shortens code later on quite drastically. However, even typing this can take a lot of time. Therefore I have created the simple form below, where you can create an array of however many objects you want, numbered 1 to N or 0 to N, in a few seconds and copy it into your RMS:

Create array of
numbered to and called.


With this form, you will be able to create arrays quite quickly and efficiently for any use. Just copy and paste the code created to the top of your script. The functions to get and set array elements will be getX(int num) and setX (int num, value) where X is the name you gave to your array.\046nbsp;Note that even though you\046nbsp;can type any max length, you should not really go past\046nbsp;a hundred\046nbsp;or so unless you desperately need it.\046nbsp;Enjoy :)!

Back to the RMS Section