Home   Help Search Login Register  

Author Topic: Random weapon  (Read 1611 times)

0 Members and 1 Guest are viewing this topic.

Offline Andor14

  • Members
  • *
Random weapon
« on: 10 May 2011, 19:58:28 »
hi

well i believe i have ask it before, but i cannot find the post and i lost my mission file. so i will ask again.
How do you make a soldier carry a weapon random selected in a list. i remember that it was somthing like this addweapon "m16a4,m4a1(((random)count)2)" , well i totally forgot how it worked.
plz help me, many thanks in advance.



Offline mr_book.PXS.Pvt

  • Members
  • *
Re: Random weapon
« Reply #1 on: 11 May 2011, 07:43:11 »
Hi there,
perhaps this will help:
Code: [Select]
_wpns = ["m4a1","m16a4","m24","m107","m240","m249"];
_wcount = count _wpns;
_rdm = round (random _wcount);
_wpn = _wpns select _rdm;
_this addweapon format ["%1", _wpn];
Not sure if this can be shortened or simplified here and there... Written from memory, ye kno?
Btw, what you see above is to be put into a script. If you want to put that stuff directly into a unit's init-line, make sure to remove all the _'s (as in, "wpns" instead of "_wpns", etc.). Should then work.
Good day,
mr_book

Offline Andor14

  • Members
  • *
Re: Random weapon
« Reply #2 on: 11 May 2011, 16:19:24 »
whoohoo  :D it worked thanks for your help.

edit: i did run into a problem though, there is a chance that it random selects no weapon at all. you will get the message that it can't find the weapon <null>.  how can u solve this?
« Last Edit: 11 May 2011, 16:31:40 by Andor14 »

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Random weapon
« Reply #3 on: 11 May 2011, 17:28:59 »
count counts the elements in the array from 1 to whatever but the first element index is 0 (zero) so you need to use
Code: [Select]
_wcount = (count _wpns) - 1;
The count of the array ["m4a1","m16a4","m24","m107","m240","m249"] is 6 but the indexes start from 0 and end in 5.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Andor14

  • Members
  • *
Re: Random weapon
« Reply #4 on: 11 May 2011, 21:44:59 »
ok it works perfect now. many thanks h- and mr_book :D