GetNearestUnit

Nherwyziant

Be better than you were yesterday :D
Reaction score
96
I got the idea from here. So In my tower, I need to get the nearest unit and my function does not work properly.

Is there something wrong with this?
JASS:
        //I remove some part of my trigger that does not have something to do with this nearest stuff.
        //.r1 will be the X of tower and .r2 is Y of tower
        private static method M4 takes nothing returns boolean
            local thistype this = thistype.this
            local unit u = GetFilterUnit()
            local real x = GetUnitX(u)
            local real y = GetUnitY(u)

            if SquareRoot((x-.r1)*(x-.r1)+(y-.r2)*(y-.r2)) <= .r3 then
                set .r3 = SquareRoot((x-.r1)*(x-.r1)+(y-.r2)*(y-.r2))//
                set .u2 = u
            endif

            return false
        endmethod

        private static method M3 takes nothing returns nothing
            local thistype this = GetTimerData(GetExpiredTimer())

            //And is this safe?  I will call destroy when the tower is dead, will lag when I have many many towers or cause fatal errors?
            if IsUnitType(.u1,UNIT_TYPE_DEAD) == false then
                set .r3 = RANGE //700
                set thistype.this = this
                call GroupEnumUnitsInRange(.g,.r1,.r2,RANGE,Filter(function thistype.M4))
                call KillUnit(.u2)//This kill is a test and it does not kill the nearest unit.  It kills other units.
            else
                call .destroy()
            endif
        endmethod
        //I hope you understand.  I'm freakin lame at explaining lol.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Try :
JASS:

//GetNearestUnit(x,y,range,filter)
library GetNearestUnit initializer Init

    globals
        private group g = CreateGroup()
        private conditionfunc nearest
        private real dx
        private real dy
        private real tx
        private real ty
        private unit tu
        private real tr
    endglobals
    
    function GetNearestUnit takes real x, real y, real aoe, conditionfunc cond returns unit
        local boolexpr cf = And(cond,nearest)
        set tr = aoe * aoe
        set tu = null
        set tx = x
        set ty = y
        call GroupEnumUnitsInRange(g,x,y,aoe,cf)
        call DestroyBoolExpr(cf)
        set cf = null
        return tu
    endfunction
    
    private function GetNearestFunc takes nothing returns boolean
        local unit u = GetFilterUnit()
        local real r = 0.
        set dx = GetUnitX(u) - tx
        set dy = GetUnitY(u) - ty
        set r = dx * dx + dy * dy
        if r <= tr then
            set tr = r
            set tu = u
        endif
        set u = null
        return false
    endfunction
    
    private function Init takes nothing returns nothing
        set nearest = Condition(function GetNearestFunc)
    endfunction
    
endlibrary
 

Nestharus

o-o
Reaction score
84
kingkingyyk3, yours wouldn't work..



[ljass]if (dx * dx + dy * dy) < tr[/ljass]

You aren't updating tr to be dx*dx+dy*dy. With yours, it'll just get whatever unit it ends on t hat happens to be in range. Furthermore, if everything just happens to be 700, it'll return nothing, so <= is needed.

I'm staring at Nher's and it doesn't seem logically flawed ><.

only thing I can say is are you sure u1 is the unit you are testing? I don't see any issues with your code that'd make it break Nher.

I'd try a GetHandleId of the unit you want to test for and a GetHandleId of the unit you happen to be testing for ; ). If they're the same I'll stare at it awhile longer =P.

Also SquareRoot() isn't needed for your thing as you'll get the same results either way, that's just adding an extra unneeded operation.

Honestly, I'd try renaming your variables to things that are easier to read, adding locals like relX and relY, and a few other things. Try making the code a lot easier to read and then see if the problem jumps out at you. At the moment, your code is a serious eyesore and is difficult to decipher without going through it. Your variable and method names are awful ^_^.
 

Nestharus

o-o
Reaction score
84
Not all of it...

[ljass]if r < tr then[/ljass]

<=...

The issue is again that I don't see any problems with Nher's code, so I suggest Nher follow my suggestions before just using the code you wrote =). Debugging your code can be quite fruitful : P.
 

Nherwyziant

Be better than you were yesterday :D
Reaction score
96
I tried this
JASS:
set .u2 = GetNearestUnit(.r1,.r2,RANGE,Condition(function thistype.M4))
//The condition is if owner of blah blah is player 2.
Does not work.

EDIT: Let me try ur code again
EDIT2: Your new code doesn't work either
 

Nestharus

o-o
Reaction score
84
You're using GetNearestUnit wrong ; |. The condition is an extra condition that doesn't seem to do anything useful... don't know why kingkingy has it in there :p.

Also you forget that king's code is still messed up
[ljass]if r < tr then[/ljass]

Furthermore, if everything just happens to be 700, it'll return nothing, so <= is needed.
 

13lade619

is now a game developer :)
Reaction score
399
pretty basic stuff actually.

tweak this code:
JASS:
real prevdist = 1000
real dist
unit NEAREST
call GroupEnumUnitsInRange(GROUP,GetUnitX(SOURCE),GetUnitY(SOURCE),RANGE, Condition(function FILTER))
    loop
        set u = FirstOfGroup(GROUP)
        call GroupRemoveUnit(GROUP,u)
        exitwhen u == null
        set dist = UnitDistance(SOURCE,u)
        if dist &lt; prevdist then
            set prevdist = dist
            set NEAREST = u
        endif
    exitwhen
endloop

^ loops through the group, if there the distance is less than the previous unit's distance, the unit is set to NEAREST.
 

Nherwyziant

Be better than you were yesterday :D
Reaction score
96
pretty basic stuff actually.

tweak this code:
JASS:

real prevdist = 1000
real dist
unit NEAREST
call GroupEnumUnitsInRange(GROUP,GetUnitX(SOURCE),GetUnitY(SOURCE),RANGE, Condition(function FILTER))
    loop
        set u = FirstOfGroup(GROUP)
        call GroupRemoveUnit(GROUP,u)
        exitwhen u == null
        set dist = UnitDistance(SOURCE,u)
        if dist &lt; prevdist then
            set prevdist = dist
            set NEAREST = u
        endif
    exitwhen
endloop

^ loops through the group, if there the distance is less than the previous unit's distance, the unit is set to NEAREST.

I didn't test it, cuz I have now fixed my trig.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top