OFPEC Forum
Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started 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
-
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.
-
cheers ill have a look with that
-
_unitlist = [unit1,unit2,unit3,unit4,...]
_n = random (count _unitlist)
_n = _n - (_n mod 1)
_speaker = _unitlist select _n
-
thanks ill try that too
-
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:
_unitlist = units group player
_n = random ((count _unitlist) - 1)
_n = _n - (_n mod 1)
_speaker = _unitlist select _nThe 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.
-
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.
-
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:
_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".
-
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
-
okay thanks guys i havent tried it and all this info came at once just to check
#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
-
If you want my paranoid belt-and-braces approach you need to reverse the order of the last two lines as follows:
#another
_unitlist = units group player
_n = random (count _unitlist)
_n = _n - (_n % 1)
if (_n >= count _unitlist) then {goto"another"}
_speaker = _unitlist select _n
-
okay thanks alot Thobson ill go with what you said its best to be safe then get some bugs