• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

"Random Unit from Group" Does it Leak?

Status
Not open for further replies.
Hello guys,

Does this cause leaks?
  • Unit - Cause Arthas (Evil) 0001 <gen> to damage (Random unit from (Units in Région 012 <gen> owned by Player 2 (Blue))), dealing 550.00 damage of attack type Chaos and damage type Normal
If so, how to fix it?

Another question: Should i add a matching condition "Matched Unit is Alive" so that this action doesn't include dead units in the region? (I mean if i have 3 units alive and 2 dead in the region will the action choose 1 random unit from 5 units or just the 3 alive ones?)
 
Level 39
Joined
Feb 27, 2007
Messages
5,010
That search command is only as smart as you tell it to be. If you don't specify units have to be alive it can pick dead units. If you don't specify non-magic-immune it can pick magic immune units, etc.. That line leaks a group, you need to set it to a variable and then use call DestroyGroup(udg_YourGroupVariable) after that line. You can't use the bj_wantDestroy = true trick here because that only works when you use a Unit Group - Pick every unit in X and do... action. (see post below this one)

For reference: Things That Leak
 
Last edited:
Level 13
Joined
May 10, 2009
Messages
868
In fact, there are some GUI functions which do destroy group objects if you set bj_wantDestroyGroup to true. The function in question is capable of doing that normally. Personally, I would go with the temp var approach because it's better for reading/tweaking it later; I don't like those nested functions within a single line.

See:
JASS:
function GroupPickRandomUnit takes group whichGroup returns unit
    // If the user wants the group destroyed, remember that fact and clear
    // the flag, in case it is used again in the callback.
    local boolean wantDestroy = bj_wantDestroyGroup
    set bj_wantDestroyGroup = false

    set bj_groupRandomConsidered = 0
    set bj_groupRandomCurrentPick = null
    call ForGroup(whichGroup, function GroupPickRandomUnitEnum)

    // If the user wants the group destroyed, do so now.
    if (wantDestroy) then
        call DestroyGroup(whichGroup)
    endif
    return bj_groupRandomCurrentPick
endfunction
There's more:
JASS:
// Actions
    ForGroupBJ(...)          // Unit Group - Pick every unit...
    GroupAddGroup(...)       // Unit Group - Add all units of Source_G to Destination_G
    GroupRemoveGroup(...)    // Unit Group - Remove all units of Source_G from Destination_G
    // Both GroupAddGroup and GroupRemoveGroup functions will destroy the source group, of course.

// Conditions
    IsUnitGroupDeadBJ(...)   // Boolean comparison
    IsUnitGroupEmptyBJ(...)  // Boolean comparison
    CountUnitsInGroup(...)   // Integer comparison
 
Status
Not open for further replies.
Top