HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

CoutnUnitsInGroup vs FirstOfGroup

08-04-2006, 02:23 AM#1
The_AwaKening
I noticed in a lot of my older triggers, I was using a blizzard.j function to tell a loop when to exit.

exitwhen CountUnitsInGroup(g) == 0

To eliminate that call I'm thinking that this should work

exitwhen FirstOfGroup(g) == null

Should that be ok, or do I need to set a local unit to the FirstOfGroup first and then check to see if that unit is null. The way I showed above saves ok in WE.
08-04-2006, 02:37 AM#2
PipeDream
Yes, that is ok. CountUnitsInGroup is rarely the right function for the job.
08-04-2006, 02:38 AM#3
masda70
Store it in a variable first, if it doesn't return null you will probably need to reference anyways.

You are right to leave countunitsingroup, that function actually uses a ForGroup to count the units in the group...

Given g the group you will loop through:

Collapse JASS:
    loop
        set p=FirstOfGroup(g)
        exitwhen (p==null)
        //do stuff with p
        call GroupRemoveUnit(g,p)
    endloop

g will be an empty group at the end and you will probably want to destroy it.
08-04-2006, 09:15 AM#4
SentryIII
That's interesting to know. I actually use CountUnitsInGroup extensively in my triggers, though I haven't run into any performance issues yet. It's great to know for when I start running into performance problems.
08-06-2006, 12:49 PM#5
DioD
first of group is better.
Cause it return a unit for later use,
08-06-2006, 01:37 PM#6
Rising_Dusk
Uhm..

CountUnitsInGroup has some aspects that can be useful.
Like actually counting units in a group when you need to know that number and can't afford to remove units constantly and need to keep it within the function.
08-06-2006, 02:54 PM#7
The_AwaKening
Agreed, although looking at the blizzard.j, it almost appears as if it would create a leak because it creates a temporary group to remove the units and count them. I don't see that the tempgroup is ever destroyed. I myself made my own countunitgroups function in the custom script in case I needed it.

I never was assuming it was useless, just senseless to check if a group is emtpy by using it.
08-06-2006, 02:57 PM#8
Rising_Dusk
Well also -- I never use it directly.

Collapse JASS:
    set bj_groupCountUnits = 0
    call ForGroup(MyGroup, function CountUnitsInGroupEnum)
    set MyCounter = bj_groupCountUnits

That's how I use it.