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

Unit Group - Pick every unit in Group and do (Actions)

Status
Not open for further replies.
Level 4
Joined
Nov 13, 2019
Messages
47
  • Unit Group - Pick every unit in Group and do (Actions)
    • Loop - Actions
In which order are the units picked? Is it completely random? Is there a way to specify you want them to be picked in the order they were created?

"Oldest" unit gets picked first, then the one created after that and so on..


My problem:
I create a line of units. This line can be of different length each time and is almost always curved because of terrain etc so I can't pick the units simply from left to right..

ooooooooooooooooooooooooooooooooooooooooooooooo
(created first)--------------------------------------->(created last)

These units have to be colored red in the order they were created, 10 at a time
øøøøøøøøøøooooooooooooooooooooooooooooooooooooo

The 10 colored are then destroyed and the next 10 must be selected & colored red.
øøøøøøøøøøooooooooooooooooooooooooooo

Rinse and repeat until the line is empty...


It does not work if they are picked randomly with the "Pick every unit in Group" function which they appear to be. So I need to figure out how to pick them in chronological order.

Does anyone have any suggestions on how to solve this?
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
Is it completely random?
No.
In which order are the units picked?
Probably in internal group order. This order should not mutate unless the group is modified, in which case it might change the order units are iterated in.
Is there a way to specify you want them to be picked in the order they were created?
No.
Does anyone have any suggestions on how to solve this?
Use a Lua table operating in list mode or Unit array turned into a list instead of a group. Lists are iterated (looped through) in a well defined order. This order is strictly controlled.

How one would turn a unit array into a list is by placing units in sequential array indices in the order you want to iterate through them. One can then loop from the start index of the list until the end index of the list, or however many elements you want to select. If removal only occurs at the head or tail of the list, then a dynamic start index and wrapping within in a finite range (implemented with modulo) can be used. One can use a single array to back many such lists as long as each list is allocated a unique index range within the array.

Another sort of list backed by arrays is a linked list. This consists of a set of arrays to form a linked list node. Such node holds the data (unit), and references to other nodes (integers representing indices within the set of arrays). One links the nodes to each other in a chain which can then be iterated given a root node. This is the most common implementation of list in vJASS. It has advantages such as dynamic sizing and better middle removal complexity, however it has the disadvantage of poor seek and length complexity. Length complexity can be improved by caching the node count.

For more information about lists one can visit Wikipedia. Since it is a very common computer science problem, often assessed at universities, there are a lot of resources available by searching google including examples in other programming languages.
 
Level 4
Joined
Nov 13, 2019
Messages
47
Thanks for the replies guys.

I had and extra layer to my problem because each Hero on the map has his own "line of units" or TrailPoints, so I had to figure out how to link each individual point to a certain Hero.

Here is a trimmed down version of my triggers to show how I solved it:

Needed Variables:
TrailNumber - Int
TrailPointsColoredOrDestroyed - Int Array
TempInt - Int
TempUnit - Unit
Trail_HT - Hashtable

Also each Hero on my map has an unique custom value which I use to reference them in Arrays.
Variables OrderedHero and CurrentActiveHero are used to reference the Units and are unimportant beyond the custom value they provide.


First I store each unit in a Hashtable as soon as they are created:


  • Create New Trail Points
    • Events
      • Time - Every 0.15 seconds of game time
    • Conditions
    • Actions
      • Set VariableSet TrailNumber = (TrailNumber + 1)
      • -------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------
      • Unit - Create 1.Trail Point for (Owner of OrderedHero) at ((Position of PreviousTrailPoint) offset by 300.00 towards (Facing of PreviousTrailPoint) degrees.) facing (Facing of PreviousTrailPoint) degrees
      • Hashtable - Save Handle Of(Last created unit) as TrailNumber of (Custom value of OrderedHero) in Trail_HT.
      • Animation - Change (Last created unit)'s vertex coloring to (100.00%, 0.00%, 0.00%) with 0.00% transparency

Next as the hero walks along the trail points and they are destroyed I save that number as "TrailPointsColoredOrDestroyed" :

  • Unit Group - Pick every unit in TrailGroup[(Custom value of OrderedHero)] and do (Actions)
    • Loop - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Distance between (Position of OrderedHero) and (Position of (Picked unit))) Less than 50.00
        • Then - Actions
          • Unit Group - Remove (Picked unit) from TrailGroup[(Custom value of OrderedHero)].
          • Set VariableSet TrailPointsColoredOrDestroyed[(Custom value of OrderedHero)] = (TrailPointsColoredOrDestroyed[(Custom value of OrderedHero)] + 1)
        • Else - Actions
Finally I set the number of trail points I need to color and do a loop based on that, where I reference to the correct units in the hashtable.

  • Actions
    • Set VariableSet TargetReplenishTrailNumber = OrderedHeroMovePoints[(Custom value of CurrentActiveHero)]
    • For each (Integer A) from 1 to TargetReplenishTrailNumber, do (Actions)
      • Loop - Actions
        • Set VariableSet TempInt = (TrailPointsColoredOrDestroyed[(Custom value of CurrentActiveHero)] + 1)
        • Set VariableSet TempUnit = (Load TempInt of (Custom value of CurrentActiveHero) in Trail_HT.)
        • Animation - Change TempUnit's vertex coloring to (100.00%, 0.00%, 0.00%) with 0.00% transparency
        • Set VariableSet TrailPointsColoredOrDestroyed[(Custom value of OrderedHero)] = (TrailPointsColoredOrDestroyed[(Custom value of OrderedHero)] + 1)
    • Set VariableSet TrailPointsColoredOrDestroyed[(Custom value of CurrentActiveHero)] = (TrailPointsColoredOrDestroyed[(Custom value of OrderedHero)] - TargetReplenishTrailNumber)
Basically I figure out exactly which unit needs to be colored by looking at the number of units destroyed before it.

And here it is in action:

 
Last edited:
Status
Not open for further replies.
Top