Home   Help Search Login Register  

Author Topic: Tute I'd like to see  (Read 488 times)

0 Members and 1 Guest are viewing this topic.

Offline Zombie

  • Members
  • *
  • Beware the night, the zombie walks among you
    • USI
Tute I'd like to see
« on: 05 Jul 2003, 15:38:47 »
I know that arrays can simplify scripting greatly, but I am unsure how, or when to use them.  The tutes all give the correct syntax, but little if any explanation.  Same with the count command and the foreach thislist command.
  I know the correct syntax, but it the application and explanation of just what it is doing and where to use it is what I am still trying to get my brain around.  For now I just "borrow" other people's work without understanding exactly or do things the long way typing and copy/paste 'ing.  I hope this makes sense!?
  You guys are the greatest around and I'm sure someone can come up with something

Offline Sui

  • Former Staff
  • ****
    • OFPEC
Re:Tute I'd like to see
« Reply #1 on: 07 Jul 2003, 11:20:05 »
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 Tutorial


What 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.
  • [ item1, item2, item3 ]
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 use
The 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 group

The 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 Arrays

Creating 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...

Offline Zombie

  • Members
  • *
  • Beware the night, the zombie walks among you
    • USI
Re:Tute I'd like to see
« Reply #2 on: 07 Jul 2003, 23:14:28 »
Fantastic!  This is just the thing I was needing.  And you are right, I have been using arrays without realizing it.  I always understood the syntax, but now I understand the usage too.  Much appreciated!