HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

getFirstUnitInDirection(...) - dont know whats wrong :O

10-18-2009, 12:54 PM#1
Chaos-Mapper
Hey guys!!

I made a small function which requires a few parameters and then returnes the first unit which is nearest to a line which is constructed by the given parameters!!

I made some Debug Msgs, to find the error but now im at a point where i dont know how to go on!
Bevor i post the function: I know its still pretty unfinished and if you use it from a point to a direction where no unit is it will end in an loop that never stops. atm that doesnt matter for me! I used a spell to cast it on a other unit to make sure that the function will always find a target!!

The problem is, it never finds a target also if i cast it on the caster itself!! I never get an INRANGE or NEAREST Debug message and i dont know what to do now :(

First my testcode:
Hidden information:
Collapse JASS:
function Trig_test_Actions takes nothing returns nothing
    local real angle = AngleBetweenPoints(GetUnitLoc(GetTriggerUnit()), GetSpellTargetLoc()) // DEGREE
    local unit u = null
    call BJDebugMsg(R2S(angle))
    call BJDebugMsg("RUN")
    
    set u = getFirstUnitInDirection(GetUnitX(GetTriggerUnit()), GetUnitY(GetTriggerUnit()), angle, 200.)
    
    if( u != null) then
        call BJDebugMsg("Found Unit in Direction!")
    endif
    //endif
    endfunction


Is it possible that the angle isnt calculated correctly? I sometimes had propblems on getting the angle between points with some special spell events so here is the event: EVENT_PLAYER_UNIT_SPELL_CHANNEL
The Dummy spell base is banish! (from the bloodmage).

Here is the function code:
Hidden information:

Collapse JASS:
    private function getDistanceBetweenUnitAndPoint takes unit u, real x, real y returns real
        local real x2 = GetUnitX(u) - x
        local real y2 = GetUnitY(u) - y
        return SquareRoot(x2 * x2 + y2 * y2)
    endfunction
    
    public function getFirstUnitInDirection takes real sourcex, real sourcey, real direction, real aoerad returns unit
        local real range = 99999999.
        local unit out = null
        local unit u = null
        local group g = CreateGroup()
        local real cos = Cos(direction * bj_DEGTORAD)
        local real sin = Sin(direction * bj_DEGTORAD)
        local real x = sourcex
        local real y = sourcey
        call BJDebugMsg("TEST")
        loop
        exitwhen out != null
            set x = x + aoerad * cos
            set y = y + aoerad * sin
            call GroupEnumUnitsInRange(g, x, y, 2 * aoerad, Condition(function ReturnTrue))
            if(FirstOfGroup(g) != null) then
                call BJDebugMsg("INSIDELOOP")
                loop
                exitwhen FirstOfGroup(g) != null
                    set u = FirstOfGroup(g)
                    if(getDistanceBetweenUnitAndPoint(u, x, y) < 10 * aoerad) then
                        call BJDebugMsg("INRANGE")
                        if(getDistanceBetweenUnitAndPoint(u, x, y) < range) then
                            call BJDebugMsg("NEAREST")
                            set range = getDistanceBetweenUnitAndPoint(u, x, y)
                            set out = u
                        endif
                    endif
                    call GroupRemoveUnit(g, u)
                    set u = null
                endloop
            endif
            
            call GroupClear(g)
        endloop
        call DestroyGroup(g)
        set g = null
        call BJDebugMsg("Run Funtion")
        return out
    endfunction


Would be great if somen could help me im not that trigger pro (and as you can see im also not that english pro :(, so sorry!)

Thanks!!
10-18-2009, 01:20 PM#2
Tot
you shouldn't use roots and use ForGroup to loop through groups

Collapse JASS:
               loop
                exitwhen FirstOfGroup(g) != null
                    set u = FirstOfGroup(g)
                    if(getDistanceBetweenUnitAndPoint(u, x, y) < 10 * aoerad) then
...
                    endif
                    call GroupRemoveUnit(g, u)
                    set u = null
                endloop

try

Collapse JASS:
               loop
                set u = FirstOfGroup(g)
                exitwhen u== null
                    if(getDistanceBetweenUnitAndPoint(u, x, y) < 10 * aoerad) then
...
                    endif
                    call GroupRemoveUnit(g, u)
                endloop

and everything should work

that's one of the reasons why you should use ForGroup()
10-18-2009, 01:41 PM#3
Chaos-Mapper
seems it works now thank you