• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Nested iteration detected, HELP!

Status
Not open for further replies.
i get that error ingame when these triggers run, any help?

  • ReturnLoop
    • Events
      • Timer - Every 0.3 seconds of Game Time
    • Local Variables
    • Conditions
    • Actions
      • Unit Group - Pick each unit in ReturningPlanes and do (Actions)
        • Actions
          • Unit - Order (Picked unit) to ( Move (GetNearestCarrier((Picked unit)))) (Replace Existing Orders)
          • General - If (Conditions) then do (Actions) else do (Actions)
            • If
              • (Distance between (Position of (Picked unit)) and (Position of (GetNearestCarrier((Picked unit))))) <= 0.5
            • Then
              • Unit - Remove (Picked unit) from the game
              • Unit Group - Remove (Picked unit) from ReturningPlanes
            • Else
  • GetNearestCarrier
    • Options: Function
    • Return Type: Unit
    • Parameters
      • Plane = No Unit <Unit>
    • Grammar Text: GetNearestCarrier(Plane)
    • Hint Text: (None)
    • Custom Script Code
    • Local Variables
      • NearestCarrier = No Unit <Unit>
      • NearestCarriers = (Empty unit group) <Unit Group>
    • Actions
      • Variable - Set NearestCarriers = (Carrier units in (Entire map) owned by player (Owner of Plane) matching Excluded: Missile, Dead, Hidden, with at most Any Amount)
      • Variable - Set NearestCarrier = (Random Living unit from NearestCarriers)
      • Unit Group - Pick each unit in NearestCarriers and do (Actions)
        • Actions
          • General - If (Conditions) then do (Actions) else do (Actions)
            • If
              • (Distance between (Position of Plane) and (Position of (Picked unit))) < (Distance between (Position of Plane) and (Position of NearestCarrier))
            • Then
              • Variable - Set NearestCarrier = (Picked unit)
            • Else
      • General - Return NearestCarrier
this happens when any unit is added to "returningplanes", it should be ordering the picked plane to move to the nearest carrier unit to it, but it's not working, not even ordering the command.

Help would be appreatiated.
 
Level 1
Joined
Jul 3, 2010
Messages
7
you can't nest with "pick each... and do..."

you need to use "for each..."

it's an editor trap.
 
Level 8
Joined
Aug 4, 2006
Messages
357
The problem is that Galaxy does not let you do nested "Pick each unit" loops because of their underlying implementation. You can see how your trigger resembles two nested loops if you copy/paste the body of your function to where it's called:
  • ReturnLoop
    • Events
      • Timer - Every 0.3 seconds of Game Time
    • Local Variables
      • NearestCarrier = No Unit <Unit>
      • NearestCarriers = (Empty unit group) <Unit Group>
    • Conditions
    • Actions
      • Unit Group - Pick each unit in ReturningPlanes and do (Actions)
        • Actions
          • Variable - Set NearestCarriers = (Carrier units in (Entire map) owned by player (Owner of Plane) matching Excluded: Missile, Dead, Hidden, with at most Any Amount)
          • Variable - Set NearestCarrier = (Random Living unit from NearestCarriers)
          • Unit Group - Pick each unit in NearestCarriers and do (Actions)
            • Actions
              • ...
              • Unit - Order (Picked unit) to ( Move(NearestCarrier)) (Replace Existing Orders)
              • ...
Ya, so Galaxy does not let you do that. Here's how you should fix your GetNearestCarrier function:
  • GetNearestCarrier
    • Options: Function
    • Return Type: Unit
    • Parameters
      • Plane = No Unit <Unit>
    • Grammar Text: GetNearestCarrier(Plane)
    • Hint Text: (None)
    • Custom Script Code
    • Local Variables
      • PickedUnit = No Unit <Unit>
      • NearestCarrier = No Unit <Unit>
      • NearestCarriers = (Empty unit group) <Unit Group>
    • Actions
      • Variable - Set NearestCarriers = (Carrier units in (Entire map) owned by player (Owner of Plane) matching Excluded: Missile, Dead, Hidden, with at most Any Amount)
      • Variable - Set NearestCarrier = (Random Living unit from NearestCarriers)
      • Unit Group - For each unit PickedUnit in NearestCarriers do (Actions)
        • Actions
          • General - If (Conditions) then do (Actions) else do (Actions)
            • If
              • (Distance between (Position of Plane) and (Position of PickedUnit)) < (Distance between (Position of Plane) and (Position of NearestCarrier))
            • Then
              • Variable - Set NearestCarrier = PickedUnit
            • Else
      • General - Return NearestCarrier
All I really did was change your inner Pick each unit loop to a For each unit loop. The For each unit loop does the same thing, but allows you to specify a variable in which to store that (Picked unit). The reason this works is that For each loops use a different mechanism for iterating over each unit, which does not fuck up when put inside any other loop. So you can put a For each inside a Pick each, a For each inside a For each, but not a Pick each inside a Pick each.
 
Status
Not open for further replies.
Top