HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Loop trough unit group manually

03-22-2008, 04:15 PM#1
MaD[Lion]
im feeling bored so i post here a way to loop trough a group manually for those who wanna know how:

[jass]
You need:
-SourceGroup: The group you wanna loop trough
-TmpGroup: a global group for temporary holding units. Can be declared by globals:
Collapse JASS:
    globals
        group TmpGroup = CreateGroup()
    endglobals


Collapse JASS:
//=== HOW TO CODE IT ===


local group g
local unit u
//...
set u = FirstOfGroup(SourceGroup)
loop
    exitwhen u == null
        //---
        // Do your thiongs here
        //---
    call GroupRemoveUnit(SourceGroup,u)
    call GroupAddUnit(TmpGroup,u)
    set u = FirstOfGroup(SourceGroup)
endloop
//...
set g = SourceGroup
set SourceGroup = TmpGroup
set TmpGroup = g
set g = null
set u = null
03-22-2008, 05:17 PM#2
notsoexpert
Neat, but you can do this:
Collapse JASS:
//=== HOW TO CODE IT ===


local group g
local unit u
//...

loop
    set u = FirstOfGroup(SourceGroup)
    exitwhen u == null
        //---
        // Do your thiongs here
        //---
    call GroupRemoveUnit(SourceGroup,u)
    call GroupAddUnit(TmpGroup,u)
endloop
//...
set g = SourceGroup
set SourceGroup = TmpGroup
set TmpGroup = g
set g = null
set u = null
Nothing big, but saves space and a function call.
Also, have you tested this? I'm pretty noob at this, but I believe I've had experiences where setting group A to group B doesn't exactly make group A point to its own group, and anything you do to group B will happen to group A (like removing units). Or maybe not...
Well, that is why blizzard made the BJ GroupAddGroup.
03-22-2008, 05:24 PM#3
Captain Griffen
This is also considerably slower, but sometimes useful.
03-22-2008, 07:02 PM#4
ADOLF
and why it is impossible to use usual ForGroup ?!
03-22-2008, 07:28 PM#5
Captain Griffen
It isn't, it's just sometimes easier to do it this way to avoid having to copy stuff across to globals.
03-22-2008, 07:48 PM#6
Troll-Brain
a little faster way :

Collapse JASS:
local unit u0 = FirstOfGroup(sourceGroup)
local unit u = u0

    loop // this loop is just for stop the code if the group is empty
    exitwhen u==null
    
        loop
            //---
            // Do your things here
            //---
            call GroupRemoveUnit(sourceGroup,u)
            call GroupAddUnit(sourceGroup,u)
            set u = FirstOfGroup(sourceGroup)
        exitwhen u == u0
        endloop

    exitwhen true // end of the "fake" loop
    endloop
    
set u = null
set u0 = null

But it won't work if they are same units type in the group, so in most of cases it's pretty useless :/
03-22-2008, 08:00 PM#7
ADOLF
imho ForGroup and globals variables is easy... ^^
03-22-2008, 08:03 PM#8
Troll-Brain
OFF TOPIC
imho means ?
03-22-2008, 08:08 PM#9
notsoexpert
IMHO: In my honest/humble opinion
03-22-2008, 08:24 PM#10
darkwulfv
ADOLF, using ForGroup can be a really big hassle and can also be VERY limiting. It also makes MUI harder. I prefer this method indeed.

Here's another method. The difference is that it doesn't save the units looped through (if you've no need for them, that is (such as in a spell that just damages units in X radius)). I use this for when I need to loop through units to damage them, or just do one thing to them and not have a need for them otherwise.

Collapse JASS:
function LoopThing takes nothing returns nothing
  local group g = CreateGroup()
  local unit f

  call GroupEnum... //whatever you use to fill your group

  loop
  set f = FirstOfGroup(g)
    exitwhen f == null
    //Do stuff here
    //------
    //
    call GroupRemoveUnit(g, f)
  endloop

  call DestroyGroup(g)
  set g = null
endfunction
03-22-2008, 10:06 PM#11
MaD[Lion]
Quote:
Originally Posted by Troll-Brain
a little faster way :

Collapse JASS:
local unit u0 = FirstOfGroup(sourceGroup)
local unit u = u0

    loop // this loop is just for stop the code if the group is empty
    exitwhen u==null
    
        loop
            //---
            // Do your things here
            //---
            call GroupRemoveUnit(sourceGroup,u)
            call GroupAddUnit(sourceGroup,u)
            set u = FirstOfGroup(sourceGroup)
        exitwhen u == u0
        endloop

    exitwhen true // end of the "fake" loop
    endloop
    
set u = null
set u0 = null
set sourceGroup=null

But it won't work if they are same units type in the group, so in most of cases it's pretty useless :/


this method rocks... it should work. even with same unit type. cus here we are not comparing type, but we are comparing units.
03-22-2008, 10:09 PM#12
MaD[Lion]
Quote:
Originally Posted by Captain Griffen
This is also considerably slower, but sometimes useful.

I dont think it is slower than ForGroup
03-22-2008, 10:18 PM#13
Captain Griffen
Quote:
Originally Posted by MaD[Lion]
I dont think it is slower than ForGroup

ForGroup is faster than looping through a array.
03-22-2008, 11:57 PM#14
MaD[Lion]
prove it
03-23-2008, 12:22 AM#15
Vexorian
ForGroup is also unnnecessary.

You only need Enums, and keeping permanent groups is ...