[JASS] How to loop through units in range of unit

Status
Not open for further replies.
Level 4
Joined
Aug 1, 2007
Messages
66
I'm trying to calculate capture point progress. I could do it in gui but I already have a trigger for every second in jass and I'd like to add it to that instead of making another gui trigger running at the same time. I'd like to create a local group of units that aren't dead in range of a unit. Then for each unit add 2 to 1 of 2 global integer variables depending on what team they're on.
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
Here is an example:
JASS:
globals
    integer force1count=0
    integer force2count=0
endglobals
function Enum_cond takes nothing returns boolean
    return GetUnitState(GetFilterUnit(),UNIT_STATE_LIFE)>0.405
endfunction
function forfunc takes nothing returns nothing
    local integer a=GetPlayerId(GetOwningPlayer(GetEnumUnit()))
    if(a==1 or a==2 or a==3 or a==4 or a==5 or a==6) then
        set force1count=force1count+1
     elseif(a==7 or a==8 or a==9 or a==10 or a==11 or a==12)
        set force2count=force2count+1
    endif
endfunction
function Trig_Loop_Actions takes nothing returns nothing
    local group g = CreateGroup()
    local unit u = GetTriggerUnit()//For example
    call GroupEnumUnitsInRange(g,GetUnitX(u),GetUnitY(u),400,Condition(function Enum_cond))
    call ForGroup(g,function forfunc)
endfunction
So you should add those functions:
JASS:
function Enum_cond takes nothing returns boolean
    return GetUnitState(GetFilterUnit(),UNIT_STATE_LIFE)>0.405
endfunction
function forfunc takes nothing returns nothing
    local integer a=GetPlayerId(GetOwningPlayer(GetEnumUnit()))
    if(a==1 or a==2 or a==3 or a==4 or a==5 or a==6) then // change the numbers to whatever you want
        set force1count=force1count+1//change the variable name
    elseif(a==7 or a==8 or a==9 or a==10 or a==11 or a==12) then//numbers here too
        set force2count=force2count+1//here too
    endif
endfunction
and insert this in the actions function of the trigger:
JASS:
local group g = CreateGroup()
    local unit u = GetTriggerUnit()//For example
    call GroupEnumUnitsInRange(g,GetUnitX(u),GetUnitY(u),400,Condition(function Enum_cond))//change the group name to whatever you want, the range and the unit variable name to whatever you want
    call ForGroup(g,function forfunc)//group name
 
Status
Not open for further replies.
Top