That's not a bad idea... I've been thinking about writing an "
everything I know about OFP Editing" tute for quite a while now, but struggle to find the time.
In the mean time, here's 5 minutes worth for ya...
Sui's quick and dirty Array TutorialWhat is an Array?Arrays are simply a list of objects, in a certain order. OFP expresses arrays by putting square brackets at either end of the list. eg.
That array contains three items. To reference (grab an item) from that array, we use the
select command.
If the above array ([
item1, item2, item3 ]) was called
MyArray, we could use this command to grab the first item:
MyArray select 0
Just note the number for the first item is
0. Arrays are numbered starting from 0. If we wanted the second item (
item2), we would use:
MyArray select 1
And so on.
So why are arrays so useful? Because it is like an ordered list which we can use to run checks/functions and code on.
We can count the number of objects in the array, we can use it to keep track of a list of units, or we can simply use loops to modify each item in the array.
Common Examples you may already useThe count and foreach commands are some examples of how we do this, eg.
"_x setdammage 1" foreach [
unit1, unit2, unit3 ]
or
? "alive _x" count [
unit1, unit2, unit3 ] > 0
With these commands (count and foreach), the _x is subsituted for each item in the array, in turn.
The foreach command goes through each item and applies the command in quotes to it (in this case: setdammage 1).
The count command goes through each item and checks to see if the condition is true. If so, it adds one to the 'count', and at the end compares the 'count' to the other number (> 0).
The other common usage of arrays is when they don't have square brackets. This is mainly used with groups. Eg.
"_x moveincargo
vehicle" foreach
units groupThe underlined bit is an array. It doesn't have square brackets, but each item (soldier in the group) has the command executed on it.
The "units" in that line converts
group from a group to an array.
Foreach and
count only accept arrays, so you will get an error if you try to use them on groups.
You could also use the line:
"_x moveincargo
vehicle" foreach [
unit1, unit2, unit3... ]
To get the same effect (assuming
unit1, unit2 etc. are the names of the soldiers in the group). The first way is much faster, and doesn't rely on the mission editor knowing which units will be alive
Creating/Modifying ArraysCreating an array is as simple as:
MyArray = [
item1, item2, item3 ]
If you wanted to add or subtract an item, you can use:
MyArray =
MyArray + [
item4 ]
MyArray =
MyArray - [
item3 ]
It can get confusing working out which names need square brackets around them, but try to remember
MyArray already has brackets (you gave it brackets when you defined it)
There is also the
set command, which is used to change items in an array. For instance, if we wanted to change the first item in
MyArray (called
item1) to
item10, we would use the command:
MyArray set [0,
item10 ]
(Remember we use 0 for the first item in the array).
This is useful if we need the array to be in a certain order.
Anyway, that's quick and nasty. Arrays are a very useful tool, and you've no doubt used them without even knowing it
Feel free to ask questions to patch up holes in my explaination, and if anyone else has anything to add, please do...