How can I check if any unit of a type are within X range of another unit?

Tar-Quaeron

New Member
Reaction score
0
Probably (I'm not sure it's the most efficient way):
JASS:
function IsUnitUnitType takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) == yourunittype
endfunction

function check takes nothing returns boolean
    return FirstOfGroup(GetUnitsInRangeOfLocMatching(yourrealradius, GetUnitLoc(your unit), Condition(function IsUnitUnitType))) != null 
endfunction


I don't know how you can remove leaks though, because you can't remove the location and destroy the group as return is the last thing you can do in a function. So this will leak.
 

Kajik

New Member
Reaction score
4
If you want to check diferent unit types:
JASS:

function nullFilter takes nothing returns boolean
    return true
endfunction

function IsUnitTypeInRangeOfUnit takes unit u, unittype ut, real radius returns boolean
    local group g = CreateGroup()
    // you can also used some global group or recycled group
    local unit tmpUnit
    
    call GroupEnumUnitsInRange(g, GetUnitX(u), GetUnitY(u), radius, Filter(function nullFilter))
    
    loop
        set tmpUnit = FirstOfGroup(g)
        exitwhen (tmpUnit == null) or IsUnitType(tmpUnit, ut)
        call GroupRemoveUnit(g, tmpUnit)
    endloop
    call DestroyGroup(g)
    set g = null
    
    return tmpUnit != null
endfunction

If you want to check specific unit type (more efficient):
JASS:

globals
    // Have some global group you will never destroy and never assign any unit to it
    group g = CreateGroup()
    boolean tempBool
endglobals

function IsHeroInRangeOfUnit_callback takes nothing returns boolean
    if IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) then
        set tempBool = true
    endif
    // You never save any unit to your global group
    return false
endfunction

function IsHeroInRangeOfUnit takes unit u, real radius returns boolean
    set tempBool = false
    call GroupEnumUnitsInRange(g, GetUnitX(u), GetUnitY(u), radius, Filter(function IsHeroInRangeOfUnit_callback))
    return tempBool
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top