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
398
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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top