• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[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