[General] tell all barracks owned by player to train new unit every 2 seconds

Status
Not open for further replies.
Level 2
Joined
Apr 4, 2020
Messages
16
so I think what I want to do can be done in one or two triggers. I am trying to get it to be:
Every two seconds:
all barracks owned by player 21 train a footman
End Pseudocode loop

based on other questions I think I have to create a unit group first though

Here is what I am trying:
  • enemy train footmen1
    • Events
      • Map initialization
    • Conditions
      • (Unit-type of (Triggering unit)) Equal to Barracks
    • Actions
      • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
        • Loop - Actions
          • Unit - Change ownership of (Triggering unit) to Player 1 (Red) and Change color
          • Unit Group - Add (Triggering unit) to enemybarracks
 
Last edited:
Level 20
Joined
Feb 23, 2014
Messages
1,264
Read this: How To Post Your Trigger

(Unit-type of (Triggering unit)) Equal to Barracks
Your trigger won't work, because:

1) "Triggering Unit" refers to the unit that "started" the excution of the trigger.
2) This trigger is "started" by the "Map Initialization" event, thus there is no unit that "starts" it, so "Triggering Unit" will be equal to "No unit".
3) "No unit" type is obviously not equal to "Barracks", so the condition will always fail and the actions won't be executed.

Recommendation: remove this condition.

Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
This leaks - read more here: Things That Leak and Memory Leaks.

Recommendation: use custom script: set bj_wantDestroyGroup = true before the quoted action.

Unit - Change ownership of (Triggering unit) to Player 1 (Red) and Change color
Unit Group - Add (Triggering unit) to enemybarracks
Both actions refer to a "Triggering Unit", which as explained above is equal to "No Unit". This will result in no units changing ownership or being added to the unit group.

Recommendation: Use (Picked Unit) instead of (Triggering Unit) as it refers to the unit that is being "picked" by an action.

Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
Loop - Actions
Unit - Change ownership of (Triggering unit) to Player 1 (Red) and Change color
Unit Group - Add (Triggering unit) to enemybarracks
This loop doesn't filter out units, so every unit in the map will be added to the unit group and have its color changed (assuming you fixed the above issue).

Recommendation: move both the "Unit - Change Owner" and "Unit Group - Add Unit" actions into a If/Then/Else block to filter out units. The condition in this block should be (Unit-type of (Picked Unit)) Equal to Barracks; you can also filter out units based on other cryteria, for instance units owned/not owned by a specific player.

---

The final result of above changes should look like this:

  • enemy train footmen1
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Unit-type of (Picked unit)) Equal to Barracks
              • <other conditions>
            • Then - Actions
              • Unit - Change ownership of (Picked unit) to Player 1 (Red) and Change color
              • Unit Group - Add (Picked unit) to enemybarracks
            • Else - Actions
 
Last edited:
Level 2
Joined
Apr 4, 2020
Messages
16
thanks very much. I just added a few extra conditions so that it only selected specific barracks but I never would have known to use 'Picked Unit' over 'Triggering Unit'
 
Level 2
Joined
Apr 4, 2020
Messages
16
I tried changing 'Map Initialization to 'Time - Every 2.00 seconds of game time' like I say in the original question but the trigger only fires once. Why is it not firing every two seconds?
  • enemy train foot
    • Events
      • Time - Every 2.00 seconds of game time
    • Conditions
    • Actions
      • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Owner of (Picked unit)) Equal to Player 21 (Coal)
              • (Unit-type of (Picked unit)) Equal to Barracks
            • Then - Actions
              • Unit - Set Rally-Point for (Picked unit) to (Center of enemy rally <gen>)
              • Unit - Order (Picked unit) to train/upgrade to a Footman
              • Unit Group - Add (Picked unit) to enemybarracks
            • Else - Actions
 
Level 20
Joined
Feb 23, 2014
Messages
1,264
I see that you ignored my tip about leaks and this is exactly the type of trigger where leak-removal would be really useful.

As for the trigger, it looks fine - perhaps the player doesn't meet the requirements for training the unit (i.e. not enough food, gold, etc.)
 

Uncle

Warcraft Moderator
Level 69
Joined
Aug 10, 2018
Messages
7,268
Also, you probably don't want to add the picked unit to enemybarracks over and over again.

Instead, add an If Then Else statement that checks if the picked unit is or isn't in enemybarracks already.

  • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
    • Loop - Actions
      • -------- Do some stuff --------
      • -------- Do some stuff --------
      • -------- Do some stuff --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • ((Picked unit) is in enemybarracks Equal to False
        • Then - Actions
          • Unit Group - Add (Picked unit) to enemybarracks
        • Else - Actions
 
Level 2
Joined
Apr 4, 2020
Messages
16
with this I just used create unit in a timer instead of training them. It is a little different but it worked for my purpose.
 
Status
Not open for further replies.
Top