Home   Help Search Login Register  

Author Topic: random soldier  (Read 1047 times)

0 Members and 1 Guest are viewing this topic.

Offline 456820

  • Contributing Member
  • **
random soldier
« 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

Offline The-Architect

  • Former Staff
  • ****
  • Bite my shiny metal...
    • Bob's Un-official Flashpoint Page
Re:random soldier
« Reply #1 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.
« Last Edit: 03 Nov 2005, 17:57:26 by The-Architect »
James Andrew Wilkinson 1977 - 2005 R.I.P.
"If it ain't the friggin' incoming it's the friggin' outgoing. Only difference is who gets the friggin' grease, and that ain't no friggin' difference at all."

Offline 456820

  • Contributing Member
  • **
Re:random soldier
« Reply #2 on: 03 Nov 2005, 18:01:46 »
cheers ill have a look with that

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:random soldier
« Reply #3 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
urp!

Offline 456820

  • Contributing Member
  • **
Re:random soldier
« Reply #4 on: 03 Nov 2005, 18:04:52 »
thanks ill try that too

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re:random soldier
« Reply #5 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.
« Last Edit: 03 Nov 2005, 22:05:28 by Raptorsaurus »

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:random soldier
« Reply #6 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.
« Last Edit: 03 Nov 2005, 22:23:36 by THobson »

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re:random soldier
« Reply #7 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".
« Last Edit: 04 Nov 2005, 00:01:25 by Raptorsaurus »

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:random soldier
« Reply #8 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

Offline 456820

  • Contributing Member
  • **
Re:random soldier
« Reply #9 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

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:random soldier
« Reply #10 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
« Last Edit: 04 Nov 2005, 14:09:30 by THobson »

Offline 456820

  • Contributing Member
  • **
Re:random soldier
« Reply #11 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