Arrays
Arrays
Arrays are "multivariables". You can store several pieces of data within a single array. This can be integers, strings or pieces of data such as targetnames and etc.
Arrays are the answwer to having to use multiple variables or in mapping, you can use a single targetname.
Arrays are the key to more efficient scripts and maps. If your map contains lots of entities which do the exact same thing (such as moving platforms) then you should be using an array to manipulate them.
To create an array we simply type...
arrayName = [];
Now we have an array, we need to add to this array, this would be a pretty advanced process if the IW developers had not built a function for us. The function can be accessed using...
maps\mp\_utility::add_to_array(array, ent);[/code]
So, for example, we have an array called "arrayName" and what we want to add is a string in a variable named "arrayString", put that into the above code...
maps\mp\_utility::add_to_array(arrayName, arrayString);
Now, a couple of things to remember about arrays is their size. Whenever you see, <ent>.size the .size does not mean dimension, but "how many". So if an array contains 1 piece of data, the array.size is 1. Another thing is calling the array. A piece of data is called from an array using the array number...
So, if we go back to the example, we have just put arrayString inside arrayName, so our arrayName.size = 1. To get that information back we use
arrayName[0]
This is where it gets a little confusing. Your array size is always 1 bigger than you array, this is because your array starts at 0. If you had 10 pieces of data in the array, the size would be ten but you would only be able to call [0]-[9].
A common use for the array is to thread a function to all of the players on the server. This can be done using an array with a for loop. So, first off we must get our array, and instead of using the above method of defining an array and adding custom data, we use a builtin command.
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++)
Thats our loop, 'i=0' (do not change this number, it is important). While 'i' is LESS than 'players.size' (remember, the size is always 1 bigger than the final number in the array), 'i++'.
{ players[i] thread function(); }
And that above simply threads the function to each person in the array. Remember, 'i' is a variable not a letter, so 'i' is substitued with the number of the loop. The first loop, 'i' equals 0, so
players[0] thread function();
The second loop, 'i' equals 1 and etc. If the amount of players on the server is 10 then loop will loop while 'i' is less than the size of the array (10). This means the 'i' will equal 0,1,2...,9.
Arrays are a complicated part of scripting, but once you have them understood, they are one of the most useful things.