Arrays

From COD Modding & Mapping Wiki
Jump to navigation Jump to search
Arrays are objects that store several variables. These can be integers, strings, entities or even other arrays.

Arrays

Arrays are extremely helpful when it comes to doing the same thing to several different objects, such as moving a few platforms by a certain amount in a map, or storing lots of related variables.

To create an array we simply type...

 arrayName = [];

Now, to add an object/variable to this array, we can simply use:

 array(array.size) = "a string variable";

Whenever you see array.size, the .size does not mean it's dimensions, but the amount of items in the array. So if an array contains 1 piece of data, the array.size is 1. To retrieve an items from an array, you can use array[0] for the first object/var stored in it, and then array[1] and so on for successive items.

Going back to the example above, we could retrieve the data by using array[0]. The first item in an array is listed as 0, the second as 1, and so on. This means that by using array(array.size) = "blah";, we can add new items to it because of the offset of 1.


A common use for an array is to call a function on all of the players in the game. This can be done by using an array with a FOR loop. To start with we must get our array of players, and instead of using the above method of defining an array and adding custom data, we can use a builtin function.

players = getEntArray("player", "classname");

So, our array has been defined as "players", or more accurately, "players[]".

Inside "players[]" is every player on the server, and now all we need to do is use the array to thread a function to each player. So, here we have a for loop to do such a thing:

 for(i = 0; i < players.size; i++)
 {
   players[i] thread function();
 }

That's our loop to go through every player and call function(); on them.

Remember, 'i' is a variable not a letter, so 'i' is substitued with the number of the loop. In the first loop, 'i' equals 0, so:

players[0] thread function();

is executed.

Arrays are a complicated part of scripting, but once you have them understood, they are one of the most useful things in the game.