- Joined
- Jun 9, 2008
- Messages
- 734
What're differences between DestroyGroup and ClearGroup?
I think clearing group is that you remove units and than you can add new units/or something to it.
Destroying it, destroys it and you must create a new group again if you want to do with the group something again.
function sth takes group g returns nothing
local integer rnd = GetRandomInt(1, CountUnitsInGroup(g)
local unit u
loop
set u = FirstOfGroup(g)
exitwhen rnd == 1
call GroupRemoveUnit(g, u)
set rnd = rnd - 1
endloop
call KillUnit(u) // u is now a random unit from the group.
endfunction
There's a BJ function:
function GroupPickRandomUnit takes group whichGroup returns unit
But you could just as easily simulate a random unit by doing the following:
JASS:function sth takes group g returns nothing local integer rnd = GetRandomInt(1, CountUnitsInGroup(g) local unit u loop set u = FirstOfGroup(g) exitwhen rnd == 1 call GroupRemoveUnit(g, u) set rnd = rnd - 1 endloop call KillUnit(u) // u is now a random unit from the group. endfunction
thanks i have problem with "FirstofGroup", it always return same unit everytime, i want to get a random unit from a unit group, do you have a solution
function foo takes nothing returns nothing
local group temp=CreateGroup()
local unit nextUnit
call GroupAddGroup(groupYouWantToIterateThrough, temp)
loop
set nextUnit=FirstOfGroup(temp)
exitwhen nextUnit==null
// Do your actions for each unit here....
call GroupRemoveUnit(temp, nextUnit)
endloop
call DestroyGroup(temp)
set nextUnit=null
set temp=null
endfunction