• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Closest unit of a point?

Status
Not open for further replies.
Level 11
Joined
Jan 23, 2015
Messages
788
Hey guys, I know the complicated way to calculate the closest unit of a point (by storing all units nearby in a variable with an index and comparing the distance), but isn't there a simplier way?

edit:
This should perfectly work right?
  • Unit Group - Pick every unit in UnitsNearby and do (Actions)
    • Loop - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Distance between (Position of (Picked unit)) and TempPoint1) Less than Distance
        • Then - Actions
          • Set Distance = (Distance between (Position of (Picked unit)) and TempPoint1)
          • Set Target = (Picked unit)
        • Else - Actions
Distance is the maximum range by default.
 
Last edited:
Hey guys, I know the complicated way to calculate the closest unit of a point (by storing all units nearby in a variable with an index and comparing the distance), but isn't there a simplier way?

edit:
This should perfectly work right?
  • Unit Group - Pick every unit in UnitsNearby and do (Actions)
    • Loop - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Distance between (Position of (Picked unit)) and TempPoint1) Less than Distance
        • Then - Actions
          • Set Distance = (Distance between (Position of (Picked unit)) and TempPoint1)
          • Set Target = (Picked unit)
        • Else - Actions
Distance is the maximum range by default.
Yeah this will work, but you should take care of the location leaks.
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
JASS:
    globals
        //prevent leak
        unit closestUnit    = null
    endglobals
    function GetClosestUnit takes real x, real y, real maxDistance returns unit
        local group g = CreateGroup()
        local real distance
        local unit FoG
        local real closestDistance = maxDistance +1
        set closestUnit = null
        
        call GroupEnumUnitsInRange(g, x, y, maxDistance, null)
        
        loop
            set FoG = FirstOfGroup(g)
            exitwhen FoG == null
            call GroupRemoveUnit(g, FoG)
            
            set distance = DistanceBetweenCoordinates(x, y, GetUnitX(FoG), GetUnitY(FoG))
            if distance < closestDistance then
                set closestDistance = distance
                set closestUnit = FoG
            endif
        endloop
        
        call DestroyGroup(g)
        set g = null
        return closestUnit
    endfunction

I searched via google but couldnt find the GetClosestUnit() function...
I have this one in my snippet library
(didnt test it but should work as intended)
 
Last edited:
Level 24
Joined
Aug 1, 2013
Messages
4,658
sorry, I forgot to upload DistanceBetweenCoordinates() as well which is used in that script of mine.

JASS:
    function DistanceBetweenCoordinates takes real x1, real y1, real x2, real y2 returns real
        local real x = x2 - x1
        local real y = y2 - y1
        return SquareRoot(x*x + y*y)
    endfunction
 
Status
Not open for further replies.
Top