• 🏆 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!

[Trigger] Why is this loop doing this weird thing?

Status
Not open for further replies.
Level 8
Joined
May 21, 2019
Messages
435
So, I have a map, where if a frog gets too close to a driving car, it explodes.
The frog is then revived and the player gets locked onto it.
However, I wanted the player to see the bloodsplatter when they died, so I decided to put a 1 second wait onto the event. However, as it turns out, this made the map unable to refer to frog when creating a new one for the owner. So apparently, an exploded unit is removed from the memory fairly fast.

In order to fix it, I came up with this abomination (which works perfectly):

  • CrossRoad Frog Killer
    • Events
    • Conditions
      • (Unit-type of (Triggering unit)) Equal to Squishy Frog
    • Actions
      • Player - Add 1 to (Owner of (Triggering unit)) Current lumber
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Owner of (Triggering unit)) Equal to Player 1 (Red)
        • Then - Actions
          • Unit - Explode (Triggering unit)
          • Wait 1.00 seconds
          • Set RespawnPoint = (Random point in CurrentRespawnRegion)
          • Unit - Create 1 GameCharacterType for Player 1 (Red) at RespawnPoint facing Default building facing degrees
          • Custom script: call RemoveLocation(udg_RespawnPoint)
          • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
          • Selection - Select (Last created unit) for (Owner of (Last created unit))
        • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Owner of (Triggering unit)) Equal to Player 2 (Blue)
            • Then - Actions
              • Unit - Explode (Triggering unit)
              • Wait 1.00 seconds
              • Set RespawnPoint = (Random point in CurrentRespawnRegion)
              • Unit - Create 1 GameCharacterType for Player 2 (Blue) at RespawnPoint facing Default building facing degrees
              • Custom script: call RemoveLocation(udg_RespawnPoint)
              • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
              • Selection - Select (Last created unit) for (Owner of (Last created unit))
            • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Owner of (Triggering unit)) Equal to Player 3 (Teal)
                • Then - Actions
                  • Unit - Explode (Triggering unit)
                  • Wait 1.00 seconds
                  • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                  • Unit - Create 1 GameCharacterType for Player 3 (Teal) at RespawnPoint facing Default building facing degrees
                  • Custom script: call RemoveLocation(udg_RespawnPoint)
                  • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                  • Selection - Select (Last created unit) for (Owner of (Last created unit))
                • Else - Actions
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Owner of (Triggering unit)) Equal to Player 4 (Purple)
                    • Then - Actions
                      • Unit - Explode (Triggering unit)
                      • Wait 1.00 seconds
                      • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                      • Unit - Create 1 GameCharacterType for Player 4 (Purple) at RespawnPoint facing Default building facing degrees
                      • Custom script: call RemoveLocation(udg_RespawnPoint)
                      • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                      • Selection - Select (Last created unit) for (Owner of (Last created unit))
                    • Else - Actions
                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                        • If - Conditions
                          • (Owner of (Triggering unit)) Equal to Player 5 (Yellow)
                        • Then - Actions
                          • Unit - Explode (Triggering unit)
                          • Wait 1.00 seconds
                          • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                          • Unit - Create 1 GameCharacterType for Player 5 (Yellow) at RespawnPoint facing Default building facing degrees
                          • Custom script: call RemoveLocation(udg_RespawnPoint)
                          • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                          • Selection - Select (Last created unit) for (Owner of (Last created unit))
                        • Else - Actions
                          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                            • If - Conditions
                              • (Owner of (Triggering unit)) Equal to Player 6 (Orange)
                            • Then - Actions
                              • Unit - Explode (Triggering unit)
                              • Wait 1.00 seconds
                              • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                              • Unit - Create 1 GameCharacterType for Player 6 (Orange) at RespawnPoint facing Default building facing degrees
                              • Custom script: call RemoveLocation(udg_RespawnPoint)
                              • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                              • Selection - Select (Last created unit) for (Owner of (Last created unit))
                            • Else - Actions
                              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                • If - Conditions
                                  • (Owner of (Triggering unit)) Equal to Player 7 (Green)
                                • Then - Actions
                                  • Unit - Explode (Triggering unit)
                                  • Wait 1.00 seconds
                                  • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                                  • Unit - Create 1 GameCharacterType for Player 7 (Green) at RespawnPoint facing Default building facing degrees
                                  • Custom script: call RemoveLocation(udg_RespawnPoint)
                                  • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                                  • Selection - Select (Last created unit) for (Owner of (Last created unit))
                                • Else - Actions
                                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                    • If - Conditions
                                      • (Owner of (Triggering unit)) Equal to Player 8 (Pink)
                                    • Then - Actions
                                      • Unit - Explode (Triggering unit)
                                      • Wait 1.00 seconds
                                      • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                                      • Unit - Create 1 GameCharacterType for Player 8 (Pink) at RespawnPoint facing Default building facing degrees
                                      • Custom script: call RemoveLocation(udg_RespawnPoint)
                                      • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                                      • Selection - Select (Last created unit) for (Owner of (Last created unit))
                                    • Else - Actions
                                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                        • If - Conditions
                                          • (Owner of (Triggering unit)) Equal to Player 9 (Gray)
                                        • Then - Actions
                                          • Unit - Explode (Triggering unit)
                                          • Wait 1.00 seconds
                                          • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                                          • Unit - Create 1 GameCharacterType for Player 9 (Gray) at RespawnPoint facing Default building facing degrees
                                          • Custom script: call RemoveLocation(udg_RespawnPoint)
                                          • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                                          • Selection - Select (Last created unit) for (Owner of (Last created unit))
                                        • Else - Actions
                                          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                            • If - Conditions
                                              • (Owner of (Triggering unit)) Equal to Player 10 (Light Blue)
                                            • Then - Actions
                                              • Unit - Explode (Triggering unit)
                                              • Wait 1.00 seconds
                                              • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                                              • Unit - Create 1 GameCharacterType for Player 10 (Light Blue) at RespawnPoint facing Default building facing degrees
                                              • Custom script: call RemoveLocation(udg_RespawnPoint)
                                              • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                                              • Selection - Select (Last created unit) for (Owner of (Last created unit))
                                            • Else - Actions
                                              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                                • If - Conditions
                                                  • (Owner of (Triggering unit)) Equal to Player 11 (Dark Green)
                                                • Then - Actions
                                                  • Unit - Explode (Triggering unit)
                                                  • Wait 1.00 seconds
                                                  • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                                                  • Unit - Create 1 GameCharacterType for Player 11 (Dark Green) at RespawnPoint facing Default building facing degrees
                                                  • Custom script: call RemoveLocation(udg_RespawnPoint)
                                                  • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                                                  • Selection - Select (Last created unit) for (Owner of (Last created unit))
                                                • Else - Actions
                                                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                                    • If - Conditions
                                                      • (Owner of (Triggering unit)) Equal to Player 12 (Brown)
                                                    • Then - Actions
                                                      • Unit - Explode (Triggering unit)
                                                      • Wait 1.00 seconds
                                                      • Set RespawnPoint = (Random point in CurrentRespawnRegion)
                                                      • Unit - Create 1 GameCharacterType for Player 12 (Brown) at RespawnPoint facing Default building facing degrees
                                                      • Custom script: call RemoveLocation(udg_RespawnPoint)
                                                      • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
                                                      • Selection - Select (Last created unit) for (Owner of (Last created unit))
                                                    • Else - Actions
However, that's a ridiculously long trigger, it looks like crap in the GUI as it keeps indenting to the right, and making changes or refactoring it is harder because it has 12 layers. So, I made slimmer loop based version:

  • CrossRoad Frog Killer Copy
    • Events
    • Conditions
      • (Unit-type of (Triggering unit)) Equal to Squishy Frog
    • Actions
      • Player - Add 1 to (Owner of (Triggering unit)) Current lumber
      • For each (Integer A) from 1 to 12, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Owner of (Triggering unit)) Equal to (Player((Integer A)))
            • Then - Actions
              • Unit - Explode (Triggering unit)
              • Wait 1.00 seconds
              • Set RespawnPoint = (Random point in CurrentRespawnRegion)
              • Unit - Create 1 GameCharacterType for (Player((Integer A))) at RespawnPoint facing Default building facing degrees
              • Custom script: call RemoveLocation(udg_RespawnPoint)
              • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
              • Selection - Select (Last created unit) for (Owner of (Last created unit))
            • Else - Actions
Now that's nicer looking! And it should work, right? Well it does! .... 3 out of 4 times... then about 1 out of 4 times, it creates a frog (GameCharacterType) for a totally different player.
I have no idea why, but it seems like the loop sometimes ends up getting a different Integer A. Maybe the wait is causing it to run and overlap another execution of it? Would make sense, except that I have had this thing pick the wrong player on the first execution so... I am a bit lost. What is making this loop unstable? How do I fix it?
 
Level 5
Joined
Oct 20, 2007
Messages
34
Another option could be to use integer B if you haven't used it a lot already.

I'm not 100% sure but I think moving the wait into a different trigger might also solve your problem.
  • Frog dies
    • Events
      • Unit - A unit Dies
    • Conditions
      • (Unit-type of (Dying unit)) Equal to Squishy Frog
    • Actions
      • Wait 1.00 seconds
      • Set RespawnPoint = (Random point in CurrentRespawnRegion)
      • Unit - Create 1 GameCharacterType for (Owner of (Dying unit)) at RespawnPoint facing Default building facing degrees
      • Custom script: call RemoveLocation(udg_RespawnPoint)
      • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
      • Selection - Select (Last created unit) for (Owner of (Last created unit))
 
Last edited:
Level 8
Joined
May 21, 2019
Messages
435
Another option could be to use integer B if you haven't used it a lot already.

I'm not 100% sure but I think moving the wait into a different trigger might also solve your problem.
  • Frog dies
    • Events
      • Unit - A unit Dies
    • Conditions
      • (Unit-type of (Dying unit)) Equal to Squishy Frog
    • Actions
      • Wait 1.00 seconds
      • Set RespawnPoint = (Random point in CurrentRespawnRegion)
      • Unit - Create 1 GameCharacterType for (Owner of (Dying unit)) at RespawnPoint facing Default building facing degrees
      • Custom script: call RemoveLocation(udg_RespawnPoint)
      • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
      • Selection - Select (Last created unit) for (Owner of (Last created unit))
It might work. I could give it a shot, as it would be a really simple solution. Also, I am not certain that going for integer B would help, as I am not running any other loops than this one when it happens. I'm pretty sure that the wait inside the loop is just fucking things up somehow.
 
Level 8
Joined
May 21, 2019
Messages
435
Just using "Owner of (Triggering Unit)" should work instead of Player1/2/..., or?
Nope, it's described in the OP why. When the unit has exploded, it gets deleted from the game's memory very shortly after, so 1 second later, it can no longer find "triggering unit" because it has been deleted.
 
Level 8
Joined
May 21, 2019
Messages
435
Is it a player unit event? If so TriggeringPlayer might be tried after the wait.
Nope, it's not.
I mean, it could be though, so your suggestion is a totally valid and relatively easy to make solution. I think I'm just gonna do a local variable though, as it seems like the proper thing to do.
 
Level 8
Joined
May 21, 2019
Messages
435
Alright, in case anyone ends up watching this thread, I made the following trigger based on the guide in doom_sheeps reply:

  • CrossRoad Frog Killer
    • Events
    • Conditions
      • (Unit-type of (Triggering unit)) Equal to Squishy Frog
    • Actions
      • Custom script: local player localPlayer = GetOwningPlayer(GetTriggerUnit())
      • Player - Add 1 to (Owner of (Triggering unit)) Current lumber
      • Unit - Explode (Triggering unit)
      • Wait 1.00 seconds
      • Custom script: set udg_PlayerToRespawn = localPlayer
      • Custom script: set localPlayer = null
      • Set RespawnPoint = (Random point in CurrentRespawnRegion)
      • Unit - Create 1 GameCharacterType for PlayerToRespawn at RespawnPoint facing Default building facing degrees
      • Custom script: call RemoveLocation(udg_RespawnPoint)
      • Camera - Lock camera target for (Owner of (Last created unit)) to (Last created unit), offset by (0.00, 0.00) using Default rotation
      • Selection - Select (Last created unit) for (Owner of (Last created unit))
It works flawlessly, and it's pretty damn sleek.
 
Status
Not open for further replies.
Top