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

[vJASS] Group Enumeration

Status
Not open for further replies.
Level 17
Joined
Feb 11, 2011
Messages
1,860
Hi guys,

I have a question: when you set group A = group B and you use a FirstOfGroup loop to count the units in group B, does it also remove the units from group A? For example:

JASS:
function GetAliveCreeps takes nothing returns integer
    local unit temp
    local integer count = 0
    
    call GroupClear(enumG)
    set enumG = creeps
    
    loop
        set temp = FirstOfGroup(enumG)
        exitwhen temp == null
        set count = count + 1
        call GroupRemoveUnit(enumG, temp)
    endloop
    
    return count
endfunction

The first time I run this, it is fine. The second time it returns 0. I have a feeling that units are removed from the creeps group when they are removed from the enumG group. Is this true?

Thanks,

Mr_Bean
 
Level 5
Joined
Jun 16, 2004
Messages
108
Yes, they are removed from both "groups". In actuality it is just one group, basically given two names (variables) to the same object.

A simple way to get around this is to add to a second group while removing from the one you are looping through, like so:

JASS:
function GetAliveCreeps takes nothing returns integer
    local unit temp
    local integer count = 0
    local group g
    
    set g = CreateGroup()

    loop
        set temp = FirstOfGroup(creeps)
        exitwhen temp == null
        set count = count + 1
        call GroupRemoveUnit(creeps, temp)
        call GroupAddUnit(g, temp)
    endloop
    call DestroyGroup(creeps)
    set creeps = g

    return count
endfunction
 
Status
Not open for further replies.
Top