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

basic Unitgroup variable questions

Status
Not open for further replies.
Level 13
Joined
Mar 24, 2010
Messages
950
Im pretty sure i already know this but I wanna double check some things about unit group variables.

If I add some units to a unit group and they die are they auto removed from that unit group? or do i need to remove them once they die?

Im pretty sure upon a unit replace event the replaced unit is auto removed from the unitgroup or so it seems..

Also does using a unit group in this way with adding and removing units cause leaks the same as making a temp unitgroup var based on certain conditions?
 
Level 5
Joined
Jan 27, 2014
Messages
164
In the past, it is known that when a unit gets removed, they are automatically removed from the unit group. (Source: AceHart)

Then again, apparently, through some unknown research, it's always good to remove the said unit from the unit group before removal because it somehow takes up a lil memory space if you don't do so. (Source: Unknown)

In your case, dead units are still units. Removal from the group is still required. Unless, of course, you set the decay time to a low number. Otherwise, they still get picked within the group loop. After a unit's decay, they are virtually removed from the game.

In any case, you can just remove the unit from the group. I don't practice this though. It doesn't affect the game performance much... at least thus far.
 
If I add some units to a unit group and they die are they auto removed from that unit group?
No they are not automatically removed.
or do i need to remove them once they die?
Yes you need to remove them. When looping through a unit group it will still loop through those dead / removed units.
Im pretty sure upon a unit replace event the replaced unit is auto removed from the unitgroup or so it seems..
No replaced unit does the same thing as removing / killing a unit. It does not remove that unit from any unit groups it is in.
Also does using a unit group in this way with adding and removing units cause leaks the same as making a temp unitgroup var based on certain conditions?
There are only 2 ways that unit groups leak.
1) creating a new unit group and overwriting the variable. Example:
  • Set UnitGroup = unit in range
All the Set unit groups in GUI creates a new unit group and will cause a leak if old unit group was not destroyed.
2) Setting the unit group to null or to another unit group. Example:
  • Custom script: set udg_UnitGroup = null
  • // or
  • Set UnitGroup = UnitGroup1
 
Level 13
Joined
Mar 24, 2010
Messages
950
hmm good point, ok lets say i dont want to remove the unit till it is decayed fully and now off the map, how will i do that? Becuz i cant just remove the unit now when it dies i need to remove it when the dead body is gone.
 
Level 13
Joined
Mar 24, 2010
Messages
950
well mainly the only reason i wouldnt want to remove that unit from the group right when it dies is in case it was resurrected by the resurrection spell. Is there an easy way to just detect if a certain unit is revived by that spell and then just re-add it to the group?
i dont think theres anyway to detect which units are revived by that spell is there?
 
I believe someone made a script for that

and of course you can perform other checks rather than just simply checking if it's dead... like for example death's suggestion of checking the unit type id of the unit

JASS:
//something like this
//assuming you run it on a periodic timer
function periodic
  local unit x
  loop
    set x = FirstOfGroup(group)
    exitwhen x == nul
    //IDK if this is a valid function
    if GetUnitTypeID(x) == 0
      call GroupRemoveUnit(x)
    endif
  endloop
endfunction
 
Level 13
Joined
Mar 24, 2010
Messages
950
Just to follow up, i did different tests on the group to see the names of units # of units in a group after one dies (and the body is fully decayed and gone)
And there is no way to detect or reference that unit in that group, so i think when a unit dies it is auto removed from a unit group that it was in. Which is logical Blizzard would make it work that way..
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Just to follow up, i did different tests on the group to see the names of units # of units in a group after one dies (and the body is fully decayed and gone)
And there is no way to detect or reference that unit in that group, so i think when a unit dies it is auto removed from a unit group that it was in. Which is logical Blizzard would make it work that way..

Test that :

JASS:
library Test initializer init

    private function init takes nothing returns nothing
        local integer i = 10
        local unit u
        local group grp = CreateGroup()
        loop
        exitwhen i == 0
        set i = i-1
            set u = CreateUnit(Player(0),'hfoo',0,0,0)
            call GroupAddUnit(grp,u)
        endloop
        call RemoveUnit(u)
        call TriggerSleepAction(0)
        loop
        set u = FirstOfGroup(grp)
        call GroupRemoveUnit(grp,u)
        exitwhen u == null
            set i = i+1
        endloop
        call BJDebugMsg(I2S(i))
    endfunction

endlibrary
 
Last edited:
Level 1
Joined
Apr 5, 2014
Messages
2
Test that :

JASS:
library Test initializer init

    globals
        private group Grp = CreateGroup()
    endglobals

    private function init takes nothing returns nothing
        local integer i = 10
        loop
        exitwhen i == 0
        set i = i-1
            set u = CreateUnit(Player(0),'hfoo',0,0,0)
            call GroupAddUnit(Grp,u)
        endloop
        call RemoveUnit(u)
        call TriggerSleepAction(0)
        loop
        set u = FirstOfGroup(Grp)
        call GroupRemoveUnit(Grp,u)
        exitwhen u == null
            set i = i+1
        endloop
        call BJDebugMsg(I2S(i))
    endfunction

endlibrary

not bad actually
 
Status
Not open for further replies.
Top