WC3Jass.com Forum Index WC3Jass.com
"The Jass Vault"
 
 FAQ   Search   Memberlist   Usergroups   Register 
 Profile   Log in to check your private messages   Log in  
 Forum   Scripts   Files   Chat   Pastebin   Function finder  
Affiliates
The HIVE Workshop Games Modding
The Hubb Wc3Campaigns
WC3-Mapping.net

Beginners: Looping through a group

 
Post new topic   Reply to topic    WC3Jass.com Forum Index -> Tutorials
View previous script :: View next script  

Author
KaTTaNa
Site Admin

Joined: 04 Apr 2005
Posts: 655
Back to top
Message
PostPosted: Mon Apr 18, 2005 9:31 pm    Post subject: Beginners: Looping through a group Reply with quote

Most experiences Jass users are already familiar with this method, or some very similiar method.

The problem with ForGroup is that we can't pass local variables directly without using globals or handle vars, which is tiresome work.
Here is the excellent alternative:
Code:
local group myGroup = ... // The group we want to loop through

local group g = CreateGroup() // Temporary group
local unit u
call GroupAddGroup(myGroup, g)
loop
    set u = FirstOfGroup(g)
    exitwhen u == null

    // Actions here... Use   u   as current unit

    call GroupRemoveUnit(g, u)
endloop
call DestroyGroup(g) // Remove temp group again



In case you wonder was FirstOfGroup() does - you are probably right. It does exactly as the name suggests: Returns the first unit in the group.
Be careful that removed units removed using RemoveUnit() will still appear as null entries in a group until removed, so be sure to use GroupRemoveUnit even if you kill or remove the unit from the game.

Finally, you can also nest multiple loops inside each other using this method, something that can be troublesome with ForGroup since GetEnumUnit will only refer to the top-level nesting then.
View user's profile Send private message Send e-mail Visit poster's website

Author
Ryude


Joined: 13 Oct 2005
Posts: 8
Back to top
Message
PostPosted: Thu Oct 13, 2005 2:25 pm    Post subject: Reply with quote

I was wondering if there are any better ways of enumerating a group than what I am using.

Currently I use:

Code:
  local real Level = I2R(GetUnitAbilityLevel(CastUnit, SpinFury_Id()))
  local real DamageTarget = SpinFury_DamageHitTarget(Level)
  local real DamageUnits = SpinFury_DamageHitUnits(Level)
  local real Range = SpinFury_Range()
  local group HitUnits = CreateGroup()
  local unit HitUnit = null

    call GroupEnumUnitsInRange( HitUnits, GetUnitX(TargUnit), GetUnitY(TargUnit), Range, null)

    loop
      set HitUnit = FirstOfGroup(HitUnits)
      exitwhen HitUnit == null
      call UnitDamageTarget( CastUnit, HitUnit, DamageUnits, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS )
      call AddSpecialEffectTargetUnitBJ( "chest", HitUnit, "Objects\\Spawnmodels\\Human\\HumanBlood\\BloodElfSpellThiefBlood.mdl" )
      call DestroyEffectBJ( GetLastCreatedEffectBJ() )
      call UnitDamageTarget( CastUnit, TargUnit, DamageTarget, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS )
      call AddSpecialEffectTargetUnitBJ( "chest", TargUnit, "Objects\\Spawnmodels\\Human\\HumanBlood\\BloodElfSpellThiefBlood.mdl" )
      call DestroyEffectBJ( GetLastCreatedEffectBJ() )
      call GroupRemoveUnit(HitUnits, HitUnit)
    endloop

    call DestroyGroup(HitUnits)
View user's profile Send private message

Author
midiway


Joined: 02 Jun 2007
Posts: 2
Back to top
Message
PostPosted: Tue Jul 24, 2007 12:26 am    Post subject: Reply with quote

If this script is the last of your function, you don't need to null the variable 'u' right?
View user's profile Send private message

Author
Bob666


Joined: 29 Jun 2007
Posts: 192
Back to top
Message
PostPosted: Tue Jul 24, 2007 3:29 pm    Post subject: Reply with quote

no, you dont have to
_________________
Projects:
SupCom | WC3 FlightSim | JASS Benchmark
View user's profile Send private message Visit poster's website

Author
Cokemonkey11


Joined: 24 Jul 2009
Posts: 9
Back to top
Message
PostPosted: Fri Jul 24, 2009 7:41 am    Post subject: Re: Beginners: Looping through a group Reply with quote

A wise man once told me "[harness the power of the FilterFunc]"

It's faster than looping and looks cleaner too.
_________________
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Display posts from previous:   
Post new topic   Reply to topic    WC3Jass.com Forum Index -> Tutorials All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


FSDark by SkaidonDesigns
Powered by phpBB © 2001, 2002 phpBB Group