Home   Help Search Login Register  

Author Topic: Newbie array question  (Read 1161 times)

0 Members and 1 Guest are viewing this topic.

Offline U_Z

  • Members
  • *
Newbie array question
« on: 26 Aug 2006, 09:09:40 »
The OFPEC documentation on arrays isn't very informative, but it did say that the == operator would not work on an array. I would like to know how I could check if 'velocity' is equal to [0,0,0]. I searched these forums but I couldn't find anything on 'looping through an array'...Any help would be appreciated :).

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re: Newbie array question
« Reply #1 on: 26 Aug 2006, 09:19:39 »
The specific question on checking for a zero velocity:

I would use the command speed and check its value instead:
http://www.ofpec.com/COMREF/index.php?action=list&letter=s#speed

However take care not use == 0  Speed is a real value not an integer so 0.0000000001 will fail the test  == 0, but for all practical purposes is small.  Bear in mind that a slow crawl has a speed > 2  so I suggest you check speed <1

Back though to the underlying issue, accessing the elements of arrays.  To do this use the command select
http://www.ofpec.com/COMREF/index.php?action=list&letter=s#selecta

Array elements are numbered from 0 up, so a 10 element array will have elements numbered from 0 to 9 inclusive.

To select the first element from an array called A use
A select 0

To loop though array elements the construct to use is:

Code: [Select]
_i = 0
#loop
_element = _array select _i
.
.
.
_i = _i + 1
if (_i < count _array) then {goto"loop"}
Where _array is the array of items you want to cycle through, note that it must have at least one element in it for the contruct to work.  If you cannot be sure that the array is not empty then you need a slightly more complicated construct.

Arrays are very powerful and I encourage you to use them.  Elements of an array can be arrays themselves, so you can create an array of locations for example, also it is not necessary that all elements of an array are of the same type, so an array of this type is perfectly valid:

["String",Integer,[another,set,of,array,elements],RealNumber,UnitIdentifier]

EDIT:

Also check out forEach:
http://www.ofpec.com/COMREF/index.php?action=list&letter=f#forEach
This is a very powerful way of cycling through the elements of an array:

{block of code} forEach array

The block of code can be many instructions each separated by a semi-colon.  In the block of code occurrances of _x will be replaced by each elemt of the array in turn.  So for example:

Code: [Select]
{_x setPos getPos player;[_x] join player} forEach [unit1,unit2,unit3]
is equivalent to the following code
Code: [Select]
unit1 setPos getPos player
[unit1] join player
unit2 setPos getPos player
[unit2] join player
unit3 setPos getPos player
[unit3] join player
« Last Edit: 26 Aug 2006, 10:54:21 by THobson »

Offline Sparticus76

  • Members
  • *
Re: Newbie array question
« Reply #2 on: 09 Sep 2008, 03:58:38 »
How do you check how many items are in an array, eg...how many "SmokeShell"  are in a magazine array?

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Newbie array question
« Reply #3 on: 09 Sep 2008, 08:32:29 »
Always check the COMREF folks.