• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Unit Iteration

Status
Not open for further replies.
Of the many ways to iteration over a set a units, which methods should be used in which situations?

Methods:
  1. ForGroup
  2. FirstOfGroup
  3. GroupEnumUnitsInRect
  4. Or simply looping over an array.
Situations:
  • Iterating over a group of units.
  • Iteration over a temporary group of units.
  • Iterating over a group while already iterating over a group, for both above situations.

Because I doubt that a single method is best for everything.
Which methods count towards the OPLimit, and which ones don't (Actions done to EnumUnit)?
 
When iterating over a group of units where you don't need the group afterward, FirstOfGroup() is usually faster (marginally) and often more convenient (see these benchmarks).

ForGroup() would be used if you need the units/group structure to be maintained.

You iterate through the filter if you need to maintain the group afterward but need to iterate through the units at the time of enumeration (controlling who is added/removed from the group). By this, I just mean that performing the actions you need to do directly in the filter is usually better than:
JASS:
call GroupEnum...(g...Condition(function someFilter))
call ForGroup(g)
In that case it is kind of like merging conditions and actions for a trigger.

ForGroup() starts a separate thread so that is another reason why you may want to use it. FirstOfGroup(), of course, runs on the current thread.
 
Status
Not open for further replies.
Top