• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[Solved] Loop not firing correctly

Status
Not open for further replies.
Level 12
Joined
Feb 11, 2008
Messages
809
JASS:
function SpawnEnemyUnits takes nothing returns nothing

        // Creating local variables
        local unit u
        local group g
        local location p
        local real a
        local real x
        local real y
        local real xe
        local real ye
        local PatrolPath Route1
       
        // First debug message
        call BJDebugMsg("Trigger is firing correctly")
   
        // First Lane of Units
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], -2200.00, 2830.00, 270 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], -1750.00, 2400.00, 90 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], -1300.00, 2830.00, 270 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], -850.00, 2400.00, 90 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], -400.00, 2830.00, 270 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 50.00, 2400.00, 90 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 500.00, 2830.00, 270 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 950.00, 2400.00, 90 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 1400.00, 2830.00, 270 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 1850.00, 2400.00, 90 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2300.00, 2830.00, 270 )
       
        // Second Lane of Units
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2680.00, 1920.00, 0 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 3100.00, 1470.00, 180 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2680.00, 1020.00, 0 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 3100.00, 570.00, 180 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2680.00, 120.00, 0 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 3100.00, -330.00, 180 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2680.00, -780.00, 0 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 3100.00, -1230.00, 180 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2680.00, -1680.00, 0 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 3100.00, -2130.00, 180 )
        call CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2680.00, -2580.00, 0 )
       
        // Second debug message
        call BJDebugMsg("Units are created correctly")
       
        // Add patrol system to all filtered enemy units
        call GroupEnumUnitsInRange( g, 0, 0, 256, null)
       
        // Third debug message
        call BJDebugMsg("Units are filtered correctly")
       
        loop
            set u = FirstOfGroup(g)
            exitwhen u == null
            if GetUnitTypeId(u) == udg_EnemyUnitType[udg_GameModeChosen] then
               
                // Fourth debug message
                call BJDebugMsg("Loop is working correctly")
               
                // Set local variables for unit positions
                set p = GetUnitLoc(u)
                set a = GetUnitFacing(u)
                set x = GetLocationX(p)
                set y = GetLocationY(p)
                set xe = x + 450 * Cos(a * bj_DEGTORAD)
                set ye = y + 450 * Sin(a * bj_DEGTORAD)
                set Route1 = PatrolPath.create()
               
                // Create the patrol points for the units
                call Route1.push(PatrolPoint.create(xe, ye))
                call Route1.push(PatrolPoint.create(x, y))
       
                // This adds the patrol settings to the units
                call PatrolUnit.create(u, Route1, PatrolType.NORMAL, PatrolOrder.MOVE, 0, false)
           
            endif
            call GroupRemoveUnit( g, u)
        endloop
       
        // Fifth debug message
        call BJDebugMsg("Trigger has finished correctly")
       
endfunction

// Initializes the trigger and begins running actions
function InitTrig_Spawn_Enemys takes nothing returns nothing

        local trigger t = CreateTrigger()
        call TriggerRegisterPlayerChatEvent( t, Player(0), "-test", true)
        call TriggerAddAction( t, function SpawnEnemyUnits )
        set t = null
       
endfunction

As you can see by the BJDebugMsgs this trigger stops working at GroupEnumUnitsInRange and i cant figure out exactly why>? can someone help me with this?
 
Level 10
Joined
Sep 16, 2016
Messages
269
Are you going to create units at some locations and pick them to order something? If you dont then stop reading this

The range of the group is 256 while all of the udg_EnemyUnitType[udg_GameModeChosen] are created far away from that point. This result an empty group

After creating the unit, you can add them to the group instantly.
Code:
local group g = CreateGroup()
call GroupAddUnit(g, CreateUnit( udg_EnemyController, udg_EnemyUnitType[udg_GameModeChosen], 2680.00, -2580.00, 0 ))
 
Level 10
Joined
Sep 16, 2016
Messages
269
The group is empty, of course the loop cancel. Try this then
JASS:
        loop
            set u = FirstOfGroup(g)
            call BJDebugMsg("Set unit done")
            exitwhen u == null
            call BJDebugMsg("Loop not breaking yet")
            if GetUnitTypeId(u) == udg_EnemyUnitType[udg_GameModeChosen] then
                call BJDebugMsg("Filtering completed")
endif
endloop

If it shows "Set unit done" then my guess is correct.[/code]
 
Level 12
Joined
Feb 11, 2008
Messages
809
Ok its failing after setting the unit on the exitwhen part

EDIT*

Yea the exitwhen u == null seems to be the issue atm because its never firing

EDIT2**

Ok i get what your saying sorry i beleive you are correct im testing rn and ill let you know!

EDIT3***

Yep that fixed it! Thanks again sorry about misunderstanding im new to JASS and am slowly figuring it out!
 
Last edited:
Status
Not open for further replies.
Top