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 The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/
  • The Helper The Helper:
    Here is another comfort food favorite - Million Dollar Casserole - https://www.thehelper.net/threads/recipe-million-dollar-casserole.193614/

      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