• 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.

[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