- Joined
- Sep 9, 2009
- Messages
- 658
I wrote a function that will return a random unit. However, I don't know how to make it so that the unit array gets nulled at the end. I'm also thinking of making the function create a copy group so the original group doesn't get it's units removed in case it's still needed. But I also don't know how to that. Using FirstOfGroup loop seems to defeat the purpose and doing
set CopyGroup = g
doesn't add the units of g to CopyGroup. (Or does it? Doesn't seem likely though)
JASS:
function GetRandomUnit takes group g returns unit
local integer i
local unit f
local unit array r[]
loop
set f = FirstOfGroup(g)
exitwhen f == null
set i = i + 1
set f = r[i]
call GroupRemoveUnit(g, f)
endloop
return r[GetRandomInt(1, i)]
endfunction