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

[Solved] Count Units on entire map

Status
Not open for further replies.
Level 13
Joined
Mar 19, 2010
Messages
870
Hi,

which is the best way to count units on the entire map by a specific player + the possibility to filter some units.

Example:

I want the number of units of player 8 but 1-2 unit-types of player 8 i don't want to count. I mean...

Player 8 has 10 Soldiers and 2 Rifleman. But the 2 rifleman i don't want to count.

the function should in the case return 10


How can i do this and leak-free???

Reg.
 
Last edited by a moderator:
Level 26
Joined
Aug 18, 2009
Messages
4,097
native GroupEnumUnitsOfPlayer (group whichGroup, player whichPlayer, boolexpr filter) returns nothing

and specify a filter like

JASS:
function TargetConditions takes nothing returns boolean
    if (GetUnitTypeId(GetFilterUnit()) == 'hrif') then
        return false
    endif

    set udg_count = udg_count + 1

    return true
endfunction

set udg_count = 0
call GroupEnumUnitsOfPlayer(udg_dummyGroup, whichPlayer, Condition(function TargetConditions))

To avoid as much leak-potency as possible, make the group a global, reused one.
 
Something like this:
JASS:
globals
    group g = CreateGroup()
    integer count
endglobals

function GroupFilter takes nothing returns boolean
    /* Add conditions here */
    if GetUnitTypeId(GetFilterUnit()) != 'hfoo' then
        set count = count + 1
    endif
    return false
endfunction

function Example takes nothing returns nothing 
    set count = 0
    call GroupEnumUnitsOfPlayer(g, Player(0), Filter(function GroupFilter))
    call BJDebugMsg(I2S(count))
endfunction

That would probably be the best way to do it. For example, that script will count the number of units owned by player 1 (red) that aren't footmen (rawcode: 'hfoo')

EDIT: WaterKnight beat me to the post.
 
Status
Not open for further replies.
Top