• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Lua : ENTERING REGION firing multiple times for same unit.

Status
Not open for further replies.
Level 12
Joined
Jan 30, 2020
Messages
875
Hello there.

Still working on converting my map from Jass2 to Lua, and I am currently experiencing a strange difference of behavior between Jass2 and Lua.

The attacking creeps (Balls) in my TD follow a path of 8 WayPoints.

These WayPoints are destructables with a model displaying their number. When I create all WayPoints after Map Init, I attach a trigger with the entering region event, the region containing just a rect around the waypoint. WayPoint coordinates are saved in 2 global real arrays WPx and WPy.
The Last WayPoint is at the center of the map and has the number 50 (same for all players).


So when the Ball spawns, I save the first waypoint number (11 for player 1, 21 for player 2, etc...) as Destination[*ball number*]. The Ball number being stored in the Ball 's UnitUserData.
Destination is as you have guessed, a global integer Array. Then I order the Ball to move towards the coordinates of the first waypoint, so : IssuePointOrder(b, "move", WPx[wpNb], WPy[wpNb]), b being the Ball and wpNb as you can imagine is this first waypoint number.

Now here was my NextWayPoint trigger action function in Jass2 that used to work perfectly :
JASS:
function NextWayPointActions takes nothing returns boolean
    local unit ball=GetEnteringUnit()
    local player owner=GetOwningPlayer(ball)
    local integer ballNb
    local integer dest
    local integer moduloDest

    if ( owner == BallsMaster ) then
        set ballNb=GetUnitUserData(ball)
        set dest=Destination[ballNb]
        if IsUnitInRegion(WPRegion[dest], ball) then
            set dest=dest + 1
            set moduloDest=Modulo(dest , 10)
            if moduloDest > 8 then
                set dest=50 // Go to the citadel
            else
                if ( moduloDest == 2 ) then
                    if ( GetUnitAbilityLevel(ball, BuffID[12]) > 0 ) then // Removing the Going Back Home buff
                        call UnitRemoveAbility(ball, BuffID[12])
                    endif
                endif
            endif
            set Destination[ballNb]=dest
            call IssuePointOrder(ball, "move", WPx[dest], WPy[dest])
        endif
    endif
    set owner=null
    set ball=null
    return false
endfunction

and now the translated version in Lua :

Lua:
                TriggerAddAction(NextWayPoint[wn], function ()
                    local b=GetEnteringUnit()
                    if (GetOwningPlayer(b)==BallsMaster) then
                        local bn=GetUnitUserData(b)
                        local d=Destination[bn]
                        if IsUnitInRegion(WPRegion[d], b) then
                            d=d+1
                            if (math.fmod(d, 10)>8) then
                                d=50 -- got to final destination
                            else
                                if (GetUnitAbilityLevel(b, BuffID[9])>0) then -- Removing Going Back Home buff
                                    UnitRemoveAbility(b, BuffID[9])
                                end
                            end
                            Destination[bn]=d
                            IssuePointOrder(b, "move", WPx[d], WPy[d])
print(bn.."->"..d)
                        end
                    end
                end)

Notice the print to display values, I use the same print when I spawn the Ball.
As I said, in Jass this worked perfectly, and when the Ball reaches the waypoint, the event fires and the Ball is ordered to do to the next waypoint.

But in Lua something strange happens, the event fires repeatedly until the Ball leaves the region !!!!

Here are a couple of screenshots to illustrate what happens :

Before entering first waypoint region :

WC3ScrnShot_042320_121414_001.png


When the Ball enters the region, it triggers repeatedly, with the destination growing constantly (but also constantly being reset to 50 everytime its modulo 10 value gets over 8) :

WC3ScrnShot_042320_121425_001.png



Of course, there would be a fix : saving a boolean in a global table indexed with the Ball unit, that would be set to false until the Ball leaves the region, but then of course this would mean adding another trigger with an event for units leaving the regions. Anyways I am sure I could find a way out of this problem without too much trouble, but that is not my question.

Here are my questions :

- Is it ,normal that the ENTER_REGION event behaves differently in Jass2 and Lua ?
What can cause such a difference ?


- Are there other events suffering from the same differences ?

Thanks for your help.
 
Level 12
Joined
Jan 30, 2020
Messages
875
UPDATE !!!

Well nobody could help me, for the simple reason that nothing was wrong with my Lua function.

After banging my head on the wall for hours and hours, I finally found the cause of all this odd behavior.


Truth is, my lack of deep Lua knowledge was (once again) the cause : all the WayPoints regions were....identical !!!

Here is where I initialized them :
Lua:
    WPRegion=__jarray(CreateRegion())
    for i=1,4 do
        for j=0,8 do
            local k=i*10+j
            wpRect=Rect(WPx[k]-WPr, WPy[k]-WPr, WPx[k]+WPr, WPy[k]+WPr)
            RegionAddRect(WPRegion[k], wpRect)
            RemoveRect(wpRect)
        end
    end
I was naively thinking that the global array WPRegion would have its constructor called for every element once they were used in RegionAddRect, but of course it could not as there is absolutely no assignment !!!

Everything was fixed with adding the line WPRegion[k]=CreateRegion() just before calling RegionAddRect .....


I would like to apologize to anyone who actively looked at my function and tried to find anything wrong with it. Sorry to have wasted your time.
 
Status
Not open for further replies.
Top