• 🏆 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!

[JASS] exit in FirstOfGroup loops

Status
Not open for further replies.
Level 6
Joined
Oct 23, 2011
Messages
182
Is it okay to exit in mid of FirstOfGroup loop?
since GroupEnum clears the group everytime

(the group I'm using is global and permanent)

JASS:
                //! runtextmacro GroupMacro("u", "x", "y", "325")
                    if IsEnemy(u, globalUnit) then
                        set x = GetUnitX(u)
                        set y = GetUnitY(u)
                        set u = null
                        exitwhen true
                    endif 
                //! runtextmacro GroupMacroEnd()
 
Level 6
Joined
Oct 23, 2011
Messages
182
GroupEnum what?


If you exit mid first of group loop, you need to do a GroupClear() at the end.

GroupEnumUnitsInRange <-
Is there a reason why I have to use GroupClear? I'm just wondering
It's from Bribe's GroupMacro

JASS:
    //! textmacro GroupMacro takes UNIT, X, Y, RANGE
        call GroupEnumUnitsInRange(G, $X$, $Y$, $RANGE$ + 75, null)
        loop
            set $UNIT$ = FirstOfGroup(G)
            exitwhen ($UNIT$ == null)
            call GroupRemoveUnit(G, $UNIT$)
            if IsUnitInRangeXY($UNIT$, $X$, $Y$, $RANGE$) then
    //! endtextmacro
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
You said you are using a first of group loop... first of group clears the group as you loop through... if you exit early, then you haven't looped through the entire group, thus you haven't cleared the entire group.


A first of group loop is different from a group enumeration loop... in a group enumeration loop, there is no exitwhen true.. it doesn't make sense to have that loop inside. exitwhen true would do absolutely nothing. A premature return would just end the current iteration and go on to the next one, similar to continue.
 
Level 6
Joined
Oct 23, 2011
Messages
182
I'm trying to use FirstOfGroup so I can just pick one random unit that matches filter and leave prematurely, so I don't have to loop through whole group and remove them one by one
As long as i don't have permanent leaks, i'm fine with it (the group is used a lot anyway)
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
As troll said, no perma leaks. Only a temporary pointer leak ^)^.


If you are worried, try this script out
JASS:
struct testtest extends array
    private static method onInit takes nothing returns nothing
        local unit u = CreateUnit(Player(0), 'hpea', 0, 0, 0)
        local group g = CreateGroup()
        
        call GroupEnumUnitsInRange(g, 0, 0, 260, null)
        
        call SetUnitPosition(u, 1000, 1000)
        set u = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
        
        call GroupEnumUnitsInRange(g, 0, 0, 260, null)
        
        loop
            set u = FirstOfGroup(g)
            exitwhen null == u
            call GroupRemoveUnit(g, u)
            call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,GetUnitName(u))
        endloop
    endmethod
endstruct

As you see, the peasant will not be in the group after the second enumeration ;p.
 
Status
Not open for further replies.
Top