HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Duplicating groups

03-06-2010, 10:39 AM#1
leric
Is there a way to duplicate groups? For example:

Group 1 = Group A
Group 2 = Group A
Group 3 = Group A

loop
//set unit1 = FirstOfGroup(Group 1)
//exitwhen unit1 == null

//call GroupRemoveUnit(Group 1,unit1)
endloop

it seems that once unit from Group1 is removed then unit from Group A is also removed. which means the same unit for all four group is then removed.
03-06-2010, 11:17 AM#2
DioD
use
ForGroup()
03-06-2010, 06:10 PM#3
Ammorth
A group is an object, just like a unit. When you set a variable to a unit, you don't get another unit, you just get a pointer to that unit. It is the same with groups. If you set a variable to a group, you just create a pointer to that group. Therefore, to copy a group, you need to make a new group and point your variable to that new group and then copy all the units into that group (using GroupAddUnit() in a ForGroup or something).
03-07-2010, 06:15 AM#4
leric
so actually do i pass a function over or just put the native into it? if passing function over, then wouldn't Group 2 and Group 1 shares the same unit pointer as well?

Group2 = Group1 (i expect it should be same as how variable are defined)

Group2 -------|
. |--> Unit1
Group1--------|

But then if,

GroupRemoveUnit(Group2, Unit1) (the pointer is removed for both unit even though the unit is still alive or is still exist. I expect it to remove the reference pointer of Group 2 only but it remove Group1 as well. The current situation are like Person A knows my name and Person B get to know my name from Person A. When I ask Person B to forget my name then Person A forget it as well. Doesn't really make sense here.

Group2 -------|
. X Unit1
Group1 -------|

Whereby,
ForGroup (Group2, GroupAddUnit(Group1, thatUnit)) <----seems weird or is there a good example?
03-07-2010, 06:22 AM#5
Ammorth
Collapse JASS:
// copy and paste this into a library or scope

globals
    private group tempGroup = null
endglobals

function GroupCopy_Callback takes nothing returns nothing
    call GroupAddUnit(tempGroup, GetEnumUnit())
endfunction

function GroupCopy takes group source returns group
    set tempGroup = CreateGroup()
    call ForGroup(source, function GroupCopy_Callback)
    return tempGroup
endfunction
03-07-2010, 06:23 AM#6
TheKid
Here's one I just made now:

Collapse JASS:
scope CopyGroup
globals
    // Watch and learn;
    private group tempGroup
endglobals

private function addToGroup takes nothing returns nothing
    call GroupAddUnit(tempGroup, GetEnumUnit())
endfunction
//******************
//* Copy Group 
//* ¯¯¯¯¯¯¯¯¯¯
//* This function will add the units of a group onto another group.
//*
//*
function CopyGroup takes group g returns group
    set tempGroup = CreateGroup()                      
    call ForGroup(g, function addToGroup)                  
    return tempGroup
endfunction
endscope

Another that is included in the API:

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

function GroupAddGroup takes group sourceGroup, group destGroup returns nothing
    // 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_groupAddGroupDest = destGroup
    call ForGroup(sourceGroup, function GroupAddGroupEnum)

    // If the user wants the group destroyed, do so now.
    if (wantDestroy) then
        call DestroyGroup(sourceGroup)
    endif
endfunction

** Edit **

You scoundrel!
03-07-2010, 06:36 AM#7
Ammorth
I laugh how our implementations are virtually identical.
03-07-2010, 06:39 AM#8
TheKid
But I included the one already included in JASS so I win.



*Laugh out loud*
03-07-2010, 09:30 AM#9
DioD
Code:
function GroupAddGroupEnum takes nothing returns nothing
    call GroupAddUnit(bj_groupAddGroupDest, GetEnumUnit())
endfunction

//===========================================================================
function GroupAddGroup takes group sourceGroup, group destGroup returns nothing
    // 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_groupAddGroupDest = destGroup
    call ForGroup(sourceGroup, function GroupAddGroupEnum)

    // If the user wants the group destroyed, do so now.
    if (wantDestroy) then
        call DestroyGroup(sourceGroup)
    endif
endfunction

rep blizzard, they done this 10 years ago...
03-07-2010, 12:09 PM#10
leric
ah.... so the magic keyword is GetEnumUnit() thanks. now i know it. Hopefully the reference of unit aren't linked to the original group so i can keep duplicate groups and process it.
03-09-2010, 01:10 PM#11
leric
btw, why do blizzard's API destroy the source group? functions that takes group are recommend to do so incase of leak?
03-09-2010, 01:14 PM#12
Anachron
They only destroy if you tell them to do.
03-09-2010, 01:25 PM#13
leric
btw are handles passed from functions consider as locals?

will the example below cause memory leak due to returning the group and without clearing and destroying the returned group?

Collapse JASS:
function CopyGroup takes group g returns group
    set tempGroup = CreateGroup()                      
    call ForGroup(g, function addToGroup)                  
    return tempGroup
endfunction
03-09-2010, 01:30 PM#14
Anachron
tempGroup is a global, you can see that with the missing "local" keyword, so its used over and over again. The group g (the parameter var) can leak if it isn't cleaned elsewhere.
03-09-2010, 01:42 PM#15
leric
So for example:

Collapse JASS:
Set g = CopyGroup(Group1)
Set f = CopyGroup(Group1)

//.....do something with g & f

wouldn't this example cause leak since every time CopyGroup() is called it will ask tempGroup to create another group while it still has it reference?

Quote:
The group g (the parameter var) can leak if it isn't cleaned elsewhere.

does it mean if a global group is passed as parameter, it will cause leak since we may call the same function again before even or will never clear the global group?