Home   Help Search Login Register  

Author Topic: Question on arrays  (Read 783 times)

0 Members and 1 Guest are viewing this topic.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Question on arrays
« on: 27 Nov 2004, 10:09:58 »
I have an array of units.  I would like to create an other array from that which contains units of only a particular type (eg LandVehicles; Tank; Air etc.) contained in the fist array.  

countType tells me how many there are, but how do I identify them?

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:Question on arrays
« Reply #1 on: 27 Nov 2004, 10:14:54 »
Quote
typeOf vehicle

Operand types:
vehicle: Object
Type of returned value:
String
Description:
Returns the class type of a given object

Example:
_class = typeOf _mi24


:beat: *Gets Shot* :beat:

Offline Blanco

  • Former Staff
  • ****
Re:Question on arrays
« Reply #2 on: 27 Nov 2004, 10:25:55 »
Somtin like this?

Quote
_vehicles = [car1,tank1,car2,tank2]

_tanks = []
_cars = []

_i = 0

#check
_veh = _vehicles select _i
? "car" counttype [_veh] > 0 : _cars = _cars + [_veh]
? "tank" counttype [_veh] > 0 : _tanks = _tanks + [_veh]
_i = _i + 1
?_i < count _vehicles : goto "check"

hint format ["Cars are %1\nTanks are %2", _cars,_tanks]

Search or search or search before you ask.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Question on arrays
« Reply #3 on: 27 Nov 2004, 18:42:45 »
Blanco:
Thanks.  Yes something exactly like that.   :)

dmpkatra:
Where did you get that?  It is not in my comref.

would something like
{if (typeOf _x == "tank") then {_tanks = _tanks + [_x]}} forEach _vehicles

work?  I will have a look at it anyway.  I am just getting the hang of the forEach but am still not sure when it work and when it won't
« Last Edit: 27 Nov 2004, 18:43:27 by THobson »

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Question on arrays
« Reply #4 on: 27 Nov 2004, 21:13:26 »
Code: [Select]
_tanks = []
{if ("tank" counttype [_x] > 0) then {_tanks = _tanks + [_x]}} forEach _vehListALL
Works!

Thanks I would not have got there without both of you.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Question on arrays
« Reply #5 on: 27 Nov 2004, 21:17:20 »
Well it does something - but not yet quite what I want.  It ignors empty vehicles.
« Last Edit: 27 Nov 2004, 21:23:25 by THobson »

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:Question on arrays
« Reply #6 on: 27 Nov 2004, 21:19:39 »
dmpkatra:
Where did you get that?  It is not in my comref.

It's because your ComRef sux. Use the online one, or at least get an updated version for 1.96. And it's dmakatra n00by. Just call me Armsty. :P

:beat: *Gets Shot* :beat:

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Question on arrays
« Reply #7 on: 27 Nov 2004, 21:28:37 »
Lol.  Okay, okay. Amsty.  I suppose if I do call you Amsty then it really is dmakatra

I got my comref from the Editors dept - and I also use the online ref that is there as well.  Is there a later version somewhere else?

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:Question on arrays
« Reply #8 on: 27 Nov 2004, 21:39:28 »
Dude, you blind? :P

http://www.ofpec.com/editors/comref.php?letter=T#typeOf

:beat: *Gets Shot* :beat:

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Question on arrays
« Reply #9 on: 27 Nov 2004, 22:20:34 »
I suppose I must be.  I have the 'official' one that I got from Editors dept, and I have a link to the Unofficial one that is there.  Honestly - I never saw this one before.  Ooops

And thanks

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Question on arrays
« Reply #10 on: 27 Nov 2004, 22:35:23 »
I have just been looking at it.  It is very good.  

I see how I missed it.  I was searching on comand reference etc.

All I needed to do was click on Online References/Commands.  I missed it by looking too hard!!

Offline Blanco

  • Former Staff
  • ****
Re:Question on arrays
« Reply #11 on: 28 Nov 2004, 04:22:52 »
Quote
It ignors empty vehicles.

I had the same problem with a satellite script and I solved it like this

Quote
_object = _this select 0

?((side _object == CIVILIAN) && ("tank" counttype [_object] > 0 && (count crew _object == 0)) : hint "tank is empty"

Empty vehicles are on the civilian side.

 
« Last Edit: 28 Nov 2004, 04:23:37 by Blanco »
Search or search or search before you ask.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Question on arrays
« Reply #12 on: 28 Nov 2004, 11:56:28 »
Excellent.  Thanks.  Like most things in OFP it is a case of 'why didn't I think of that?'