• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] Loop problem

Status
Not open for further replies.
Level 11
Joined
Sep 14, 2009
Messages
284
In this small section of a trigger I have a loop that never ends and I don't know why. The message in the loop gives 2, 2, 2... and so on and then just stops but the "Loop end" is never displayed.

JASS:
function CombatLostDialogButton01_Actions takes nothing returns nothing
    local group g
    local unit u
    //----------
    set g = GetUnitsInRectAll(udg_CombatRect)
    set u = FirstOfGroup(g)
    call DisplayTextToForce(GetPlayersAll(), "Loop start")
    loop
        exitwhen CountUnitsInGroup(g) == 0
        set u = FirstOfGroup(g)
        call RemoveUnit(u)
        call DisplayTextToForce(GetPlayersAll(), I2S(CountUnitsInGroup(g)))
    endloop
    call DisplayTextToForce(GetPlayersAll(), "Loop end")
    call DestroyGroup(g)
    set g = null
    set u = null
endfunction

EDIT: Solved. Thanks Ceday.
 
Last edited:
Level 12
Joined
Feb 22, 2010
Messages
1,115
You should remove your unit from group before removing it, because if you use FirstOfGroup loops the units that removed from game still counted as in group.

Also you can do this, counting units in group twice is not neccesary.
JASS:
loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupRemoveUnit(g, u)
        call RemoveUnit(u)
        call DisplayTextToForce(GetPlayersAll(), I2S(CountUnitsInGroup(g)))
    endloop
 
Status
Not open for further replies.
Top