OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: 456820 on 03 Nov 2005, 17:42:01

Title: random soldier
Post by: 456820 on 03 Nov 2005, 17:42:01
okay im looking for a way to randomly select any living soldier in my grip with a script function or whatever to have him say a radio sound or say something if he is closer enough to the player
i can do the distance bit but the random soldier remains un sure of
any help would be great
thanks
Title: Re:random soldier
Post by: The-Architect on 03 Nov 2005, 17:56:56
Use the random command.

_random = random 2
?(_random> 0) && (_random <= 1) : goto "Part1"
?(_random> 1) && (_random <= 2) : goto "Part2"

#Part1
Dude1 Say "Radio Message"
goto "Exit"

#Part2
Dude2 Say "Radio Message"
goto "Exit"

#Exit
Exit

Etc, etc, etc.
Something like that, no promises.
Title: Re:random soldier
Post by: 456820 on 03 Nov 2005, 18:01:46
cheers ill have a look with that
Title: Re:random soldier
Post by: Mr.Peanut on 03 Nov 2005, 18:03:54
Code: [Select]
_unitlist = [unit1,unit2,unit3,unit4,...]
_n = random (count _unitlist)
_n = _n - (_n mod 1)
_speaker = _unitlist select _n
Title: Re:random soldier
Post by: 456820 on 03 Nov 2005, 18:04:52
thanks ill try that too
Title: Re:random soldier
Post by: Raptorsaurus on 03 Nov 2005, 22:00:24
Mr. Peanut's is the more code efficient method, but the "(count _unitlist)" needs to be : "((count _unitlist) - 1)".  Also since you are dealing with a group, the array list can be more easily obtained using: "_unitlist = units group player" (or some other unit name in the group, if it is not the player's group).

So the code should be:
Code: [Select]
_unitlist = units group player
_n = random ((count _unitlist) - 1)
_n = _n - (_n mod 1)
_speaker = _unitlist select _n
The reason for this is the first element in an array is:
"_this select 0"
NOT:
 "_this select 1"
and the last element in the array is:
"_this select ((count array) - 1)"

If you try to get an array element using "_this select (count array)", you will get a division by zero error.
Title: Re:random soldier
Post by: THobson on 03 Nov 2005, 22:18:59
I beg to differ.  using random (count _unitlist - 1) will result in you never selecting the last element of the array.

the point to watch is the line:

_n = _n - (_n mod 1)

(Which I write as : _n = _n - (_n % 1) because I was always taught that mod had a differnet meaning)

This line strips out the fractional part of the number so _n will then be an integer from 0 to count _unitlist -1.

For a belt and braces approach I use:

#another
_n = random (count _unitlist)
_n = _n - (_n % 1)
if (_n >= count _unitlist) then {goto"another"}

The chance of the goto being executed is infinitesimally small, to none existent.

After the line:
_n = random (count _unitlist)

_n will be in the range >=0 and < count _unitlist

The line:
_n = _n - (_n % 1)

then rounds this down to the nearest integer

and then the if statement just caters for the remote posibility that in the frst line _n was actually = count _unitlist.

I feel I have laboured this a bit in an attempt to be clear.
Title: Re:random soldier
Post by: Raptorsaurus on 03 Nov 2005, 23:56:04
I stand corrected!!  I see that the "_n = _n - (_n mod 1)" handles the array count being from 0 to (count array - 1).  So the code should be:

Code: [Select]
_unitlist = units group player
_n = random (count _unitlist)
_n = _n - (_n mod 1)
_speaker = _unitlist select _n

P.S. In my understanding "%" is just shorthand for "mod", just as "?" can be used instead of "if".
Title: Re:random soldier
Post by: THobson on 04 Nov 2005, 00:15:59
Quote
In my understanding "%" is just shorthand for "mod", just as "?" can be used instead of "if".
In ofp scripting that is correct.  In my ancient maths lessons millenia ago I was taught that 'mod' meant 'modulus of', which for a number was the absolute value and for a vector was its scalar size.  So forgive me if I stick to the old fashioned definitions - it is in keeping with my age.

On '?' being shorthand for 'if'  well ... I am not sure.  using ? how would you write:

{if (some condition) then {if (some other condition) then { do something} else {do something else}} else {do something completely different}} forEach array
Title: Re:random soldier
Post by: 456820 on 04 Nov 2005, 10:37:26
okay thanks guys i havent tried it and all this info came at once just to check

Code: [Select]
#another
_unitlist = units group player
_n = random (count _unitlist)
_n = _n - (_n % 1)
_speaker = _unitlist select _n
if (_n >= count _unitlist) then {goto"another"}

is that correct then ?
ill try this to find out anyway
thanks for all the help guys
Title: Re:random soldier
Post by: THobson on 04 Nov 2005, 14:09:12
If you want my paranoid belt-and-braces approach you need to reverse the order of the last two lines as follows:

Code: [Select]
#another
_unitlist = units group player
_n = random (count _unitlist)
_n = _n - (_n % 1)
if (_n >= count _unitlist) then {goto"another"}
_speaker = _unitlist select _n
Title: Re:random soldier
Post by: 456820 on 04 Nov 2005, 14:17:09
okay thanks alot Thobson ill go with what you said its best to be safe then get some bugs