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

Trying to count units here...

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
For some reason it loads 0... or only loops 1 time event though more units should be in the group.

JASS:
private function UpdateBlacksmith takes integer city returns nothing
    local group g1 = CityGroup[city]
    local group g2 = CreateGroup()
    local unit u
    local integer array count 
    local integer load
    local integer i = 1
    
    if (IsUnitGroupEmptyBJ(CityGroup[city]) == true) then
        call BJDebugMsg("CityGroup = Empty")
    else 
        call BJDebugMsg("CityGroup = NotEmpty")
    endif
    if (IsUnitGroupEmptyBJ(g1) == true) then
        call BJDebugMsg("g1 = Empty")
    else 
        call BJDebugMsg("g1 = NotEmpty")
    endif
    if (IsUnitGroupEmptyBJ(g2) == true) then
        call BJDebugMsg("g2 = Empty")
    else 
        call BJDebugMsg("g2 = NotEmpty")
    endif
    
    loop
        set u = FirstOfGroup(g1)
        exitwhen u == null
        if (GetUnitState(u, UNIT_STATE_LIFE) > 0) then
            set load = LoadInteger(hash, GetUnitTypeId(u), 'hbla')
            //set load = LoadInteger(hash, 'hbla', GetUnitTypeId(u))
            call BJDebugMsg("l: " + I2S(load))
            if (load > 0) then
                set count[load] = count[load] + 1
                call GroupAddUnit(g2,u)
            endif
        endif
        call GroupRemoveUnit(g1,u) 
    endloop
    call BJDebugMsg("----------")
    loop
        exitwhen blacksmith[i] == null
            call BJDebugMsg(I2S(count[i]))
            
        set i = i + 1
    endloop
    call BJDebugMsg("----------")
    set u = null
endfunction

JASS:
set blacksmith[1] = 'h00B'  
call SaveInteger(hash, blacksmith[1], 'hbla', 1)
etc...

This time I'm 100% sure the hashtable is intitalized...

*EDIT: AH.... Solved it...
JASS:
local group g1 = CreateGroup()
call GroupAddGroup(CityGroup[city], g1)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Be aware that GroupAddGroup is not a trivial function call.
JASS:
function GroupAddGroup takes group sourceGroup,group destGroup returns nothing
    local boolean wantDestroy = bj_wantDestroyGroup
    set bj_wantDestroyGroup = false
    set bj_groupAddGroupDest = destGroup
    call ForGroup(sourceGroup, function GroupAddGroupEnum)
    if (wantDestroy) then
        call DestroyGroup(sourceGroup)
    endif
endfunction

function GroupAddGroupEnum takes nothing returns nothing
    call GroupAddUnit(bj_groupAddGroupDest, GetEnumUnit())
endfunction

This basically means that your entire destructive loop loses all speed benefit over a traditional ForGroup call with locals exposed as register globals.

If the group stored in the array is only stored in the array (no other persistent references to it) you can perform a group swap operation on it. This is where as you destroy the group (remove units from it) you build another group (add the removed units to it) and then swap the group references around. There is a good chance that it would be faster than building up another group just to destroy it as you will only loop through each unit once.
 
Status
Not open for further replies.
Top