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

Counting units in a unit group(jass)

Status
Not open for further replies.
Level 7
Joined
Aug 19, 2009
Messages
278
Is there are a easy way to count the number of units in a unit group?

This is a aoe damage trigger i made.

JASS:
set flamecrash = CreateGroup()
call GroupEnumUnitsInRange(flamecrash, x, y, 250, Filter(function TrueBoolexpr))
    loop
        set tempdmg = FirstOfGroup(flamecrash)
    exitwhen tempdmg == null
            if (GetPlayerId(GetOwningPlayer(tempdmg)) != cvalue) then
            if (GetPlayerId(GetOwningPlayer(tempdmg)) != 15) then
                set np = getvalue(tempdmg)
                set tempdamage = SpellDamage(cvalue, np, 1, 150)
                call UnitDamageTarget( udg_TargetingHero[cvalue], tempdmg, tempdamage, true, false, ATTACK_TYPE_NORMAL , DAMAGE_TYPE_UNIVERSAL, WEAPON_TYPE_WHOKNOWS )
            endif
            endif
                call GroupRemoveUnit(flamecrash, tempdmg)
    endloop
call DestroyGroup(flamecrash)


I want to check the number of units that are present in the group before damaging them. And then damage them.


Another side question. Is there a to check unit alliance quickly.
 
Level 7
Joined
Aug 19, 2009
Messages
278
Instead of making 2 unit groups, what if I make a unit array.

Remove units from the group, count them, store them in an array and damage the array later. Wont it be little more efficient?
 
Level 10
Joined
Jun 6, 2007
Messages
392
^ That's how I would do it. Also, I'm not 100% sure, but wouldn't Malhorne's example create 2 pointers that refer to the same group, instead of creating 2 groups? In that case, the original group becomes empty and no unit is damaged. As said, I'm not sure, but that's easy to test.
 
Level 7
Joined
Aug 19, 2009
Messages
278
I did this... Don't know which method is better. Waiting for some one to reply

JASS:
set flamecrash = CreateGroup()
call GroupEnumUnitsInRange(flamecrash, x, y, 250, Filter(function TrueBoolexpr))
    loop
        set tempdmg = FirstOfGroup(flamecrash)
    exitwhen tempdmg == null
            if (GetPlayerId(GetOwningPlayer(tempdmg)) != cvalue) then
            if (GetPlayerId(GetOwningPlayer(tempdmg)) != 15) then
                set damagegroup[unitcount] = tempdmg
                set unitcount = unitcount + 1
            endif
            endif
                call GroupRemoveUnit(flamecrash, tempdmg)
    endloop
    if  (unitcount>=5) then
        set flamecrashdamage = 300
    else
        set flamecrashdamage = 150*(unitcount/2.5)
    endif
call DisplayTextToForce(GetPlayersAll(), "Units hit = " + I2S(unitcount) + " damage = " + R2S(flamecrashdamage))
set n = 0
    loop
    exitwhen (n == unitcount)
        set np = getvalue(damagegroup[n])
        set tempdamage = SpellDamage(cvalue, np, 1, flamecrashdamage)
        call UnitDamageTarget( udg_TargetingHero[cvalue], damagegroup[n], tempdamage, true, false, ATTACK_TYPE_NORMAL , DAMAGE_TYPE_UNIVERSAL, WEAPON_TYPE_WHOKNOWS )
    set n = n + 1
    endloop

Wait does a unit array leak?
 
unit arrays do leak if they are not nulled. When you loop to damage the unit simply null the variable.

When posting code post the whole function. Malhorne's suggestion above will not work. You do need to do it like this.
Also anything used twice or more should be stored into a variable.
Example: GetPlayerId(GetOwningPlayer(tempdmg))

also please learn how to properly indent your code as it is really hard to read like this. You should also look at how to properly name things.
JASS:
    function example takes nothing returns nothing
        set flamecrash = CreateGroup()
        call GroupEnumUnitsInRange(flamecrash, x, y, 250, Filter(function TrueBoolexpr))
        loop
            set tempdmg = FirstOfGroup(flamecrash)
            exitwhen tempdmg == null
            if (GetPlayerId(GetOwningPlayer(tempdmg)) != cvalue) then
                if (GetPlayerId(GetOwningPlayer(tempdmg)) != 15) then
                    set damagegroup[unitcount] = tempdmg
                    set unitcount = unitcount + 1
                endif
            endif
            call GroupRemoveUnit(flamecrash, tempdmg)
        endloop
        
        if  (unitcount>=5) then
            set flamecrashdamage = 300
        else
            set flamecrashdamage = 150*(unitcount/2.5)
        endif
        
        call DisplayTextToForce(GetPlayersAll(), "Units hit = " + I2S(unitcount) + " damage = " + R2S(flamecrashdamage))
        set n = 0
        
        loop
            exitwhen damagegroup[ n] == null
            set np = getvalue(damagegroup[n])
            set tempdamage = SpellDamage(cvalue, np, 1, flamecrashdamage)
            call UnitDamageTarget( udg_TargetingHero[cvalue], damagegroup[n], tempdamage, true, false, ATTACK_TYPE_NORMAL , DAMAGE_TYPE_UNIVERSAL, WEAPON_TYPE_WHOKNOWS )
            set damagegroup[ n] = null
            set n = n + 1
        endloop
    endfunction

if i missed any just ask about how to name them.
JASS:
CONSTANT_VARIABLES
    FunctionNames
    localVariables
    globalVariables
 
No the thing you suggested above will not work. I said that in my post above. The reason your way won't work is what erkki2 said. Variables are pointers unless you create 2 groups that are the same thing or create 2 groups and loop through the one and add it to the second group your way will not work. By variables being pointers I means the only thing they store is a spot in memory on we're the data is. They do not store the actual data. The data is stored in memory and variables point to that memory location.
 
Status
Not open for further replies.
Top