- Joined
- Nov 1, 2006
- Messages
- 1,612
This is a snippet of the code I was working on.
It definitely works and picks a completely random unit every time. I was looking to get around using the BJ function GroupPickRandomUnit() which also calls another BJ GroupPickRandomEnum()
Before I continue with it, what do you guys think? Does it look like it would be more efficient than just calling the random unit function?
It definitely works and picks a completely random unit every time. I was looking to get around using the BJ function GroupPickRandomUnit() which also calls another BJ GroupPickRandomEnum()
Before I continue with it, what do you guys think? Does it look like it would be more efficient than just calling the random unit function?
JASS:
function Random_Filter takes nothing returns boolean
//Add your qualifiers here for what type of units you want to pick randomly from
return GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0 and GetUnitTypeId(GetFilterUnit()) == 'e000' or GetUnitTypeId(GetFilterUnit()) == 'e002'
endfunction
function Trig_PickRandom_Actions takes nothing returns nothing
local unit t = GetTriggerUnit()
local unit u
local player p = GetOwningPlayer(t)
local real x
local real y
local integer i = 0
local integer j = 0
local integer e
local integer v
local integer z
local group g = CreateGroup()
local group h = CreateGroup()
call GroupEnumUnitsInRange(g, x, y, 99999.00, Condition(function Random_Filter))
//Replace the upper limit integer with how many random units you would like picked
set v = GetRandomInt(0, 1)
loop
exitwhen j == v
set j = (j + 1)
set z = CountUnitsInGroup(g)
//A random unit is picked below
set e = GetRandomInt(0, z)
set i = 0
loop
exitwhen i == e
set i = (i+1)
set u = FirstOfGroup(g)
call GroupRemoveUnit(g, u)
if i == e then
//Do Actions to the random unit
set x = GetUnitX(u)
set y = GetUnitY(u)
call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Demon\\DarkPortal\\DarkPortalTarget.mdl", x, y))
else
//Adds unused (counted) units to second group
call GroupAddUnit(h, u)
endif
endloop
set e = CountUnitsInGroup(h)
set i = 0
//Adds unused removed (counted) units back into first group
loop
exitwhen i == e
set i = (i+1)
set u = FirstOfGroup(h)
call GroupAddUnit(g, u)
call GroupRemoveUnit(h, u)
endloop
endloop
call DestroyGroup(g)
call DestroyGroup(h)
set g = null
set h = null
set t = null
set u = null
set p = null
endfunction
Last edited: