function GetUnitsOfPlayerAndTypeId takes player whichPlayer, integer unitid returns group
local group g = CreateGroup()
set bj_groupEnumTypeId = unitid
call GroupEnumUnitsOfPlayer(g, whichPlayer, filterGetUnitsOfPlayerAndTypeId)
return g
endfunction
JASS:function GetUnitsOfPlayerAndTypeId takes player whichPlayer, integer unitid returns group local group g = CreateGroup() set bj_groupEnumTypeId = unitid call GroupEnumUnitsOfPlayer(g, whichPlayer, filterGetUnitsOfPlayerAndTypeId) return g endfunction
It doesn't leak if you save the returned group to a variable and then destroy it.
set udg_CHECKincome = GetUnitsOfPlayerAndTypeId(ConvertedPlayer(GetForLoopIndexA()), 'ogre')
set bj_wantDestroyGroup = true
set udg_PLAYER_POINT[GetForLoopIndexA()] = CountUnitsInGroup(udg_CHECKincome)
call DestroyGroup(udg_CHECKincome)
set udg_CHECKincome = null
// use a global group created on map init that you will just recycle (I'll call it udg_UnitGroup)
function SomeFunction takes nothing returns nothing
local player p = Player(0)
local integer count = 0
local unit u
call GroupEnumUnitsInRange(udg_UnitGroup, someX, someY, someRadius, null)
loop
set u = FirstOfGroup(udg_UnitGroup)
exitwhen (u == null)
if (GetUnitTypeId(u) == 'ogre') and (GetOwningPlayer(u) == p) then
set count = count + 1
endif
call GroupRemoveUnit(g, u)
endloop
endfunction
CountUnitsInGroup
is a good BJ function. However, GetUnitsOfPlayerAndTypeId
is not xD you can easily count the units and filter them out with a simple FirstOfGroup
loop.// use a global group created on map init that you will just recycle (I'll call it udg_UnitGroup)
function CountUnitType takes integer unitType, player p returns integer
local integer count = 0
local unit u
call GroupEnumUnitsInRange(udg_UnitGroup, someX, someY, someRadius, null)
loop
set u = FirstOfGroup(udg_UnitGroup)
exitwhen (u == null)
if (GetUnitTypeId(u) == unitType) and (GetOwningPlayer(u) == p) then
set count = count + 1
endif
call GroupRemoveUnit(g, u)
endloop
return count
endfunction
function SomeFunction takes nothing returns nothing
local integer i = 0
loop
exitwhen (i > blahblah)
set udg_PLAYER_POINT[i] = CountUnitType(Player(i), 'ogre')
set i = i + 1
endloop
endfunction
You fix the leak by making your own functionUnless they leak - but even then you can fix the leak so that's no issue.
I am using one function? I combined bothAs far as I am concerned, one function call is easier to use than a loop. So.. win!
CountUnitsInGroup
and GetUnitsOfPlayerAndTypeId
into one function Instead of using an integer counter that increments everytime you add an unit (assuming that someone adds / removes units dynamically), you can just add and remove units freely and useI'm not sure how Count is any better or worse than GetUnitsOfPlayerAndType
CountUnitsInGroup
when you have to.-->JASS:function CountUnitType takes integer unitType, player p returns integer local integer count = 0 local unit u call GroupEnumUnitsInRange(udg_UnitGroup, someX, someY, someRadius, null) loop set u = FirstOfGroup(udg_UnitGroup) exitwhen (u == null) if (GetUnitTypeId(u) == unitType) and (GetOwningPlayer(u) == p) then set count = count + 1 endif call GroupRemoveUnit(g, u) endloop return count endfunction
native GetPlayerUnitTypeCount takes player p, integer unitid returns integer
//counts only alive units/heroes/structures
//counts also units during training; also counts buildings under construction
//counts building during upgrade (like upgrade town hall-->keep in progress, counts as keep)
//counts also heroes during revieving
Yes it does leak... "local group g" is not null at the time of function return. Hence it leaks a handle reference.It doesn't leak if you save the returned group to a variable and then destroy it.