[Trigger] How to improve this trigger?

I made this trigger that gives items and upgrades them (ability items)for each hero of the computer players. However, every time after it's triggered it causes brief lag. An if the computer players are more so is the lag. So, can you advice me how to make the trigger not cause lag?

  • Items for Computers
    • Events
    • Conditions
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Level Less than or equal to 5
        • Then - Actions
          • Unit Group - Pick every unit in Computer_Players_Gr and do (Actions)
            • Loop - Actions
              • Set random[(Player number of (Owner of (Picked unit)))] = (Random integer number between 1 and 100)
              • Hero - Create Item[random[(Player number of (Owner of (Picked unit)))]] and give it to (Picked unit)
              • Item - Set charges remaining in (Last created item) to 2
              • Unit - Increase level of Ability[random[(Player number of (Owner of (Picked unit)))]] for (Picked unit)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • Level Equal to 1
                • Then - Actions
                  • Set random[(Player number of (Owner of (Picked unit)))] = (Random integer number between 1 and 100)
                  • Hero - Create Item[random[(Player number of (Owner of (Picked unit)))]] and give it to (Picked unit)
                  • Item - Set charges remaining in (Last created item) to 2
                  • Unit - Increase level of Ability[random[(Player number of (Owner of (Picked unit)))]] for (Picked unit)
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • Level Greater than 1
                • Then - Actions
                  • Set random[(Player number of (Picked player))] = (Random integer number between 1 and 6)
                  • For each (Integer A) from 1 to 120, do (Actions)
                    • Loop - Actions
                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                        • If - Conditions
                          • (Item-type of (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))])) Equal to Item[(Integer A)]
                        • Then - Actions
                          • Item - Set charges remaining in (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))]) to ((Charges remaining in (Item carried by (Picked unit) in slot (Player number of (Owner of (Picked unit))))) + 1)
                          • Unit - Increase level of Ability[(Integer A)] for (Picked unit)
                        • Else - Actions
                • Else - Actions
        • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Level Greater than 5
        • Then - Actions
          • Unit Group - Pick every unit in Computer_Players_Gr and do (Actions)
            • Loop - Actions
              • Set random[(Player number of (Picked player))] = (Random integer number between 1 and 6)
              • For each (Integer A) from 1 to 120, do (Actions)
                • Loop - Actions
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Item-type of (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))])) Equal to Item[(Integer A)]
                    • Then - Actions
                      • Item - Set charges remaining in (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))]) to ((Charges remaining in (Item carried by (Picked unit) in slot (Player number of (Owner of (Picked unit))))) + 1)
                      • Unit - Increase level of Ability[(Integer A)] for (Picked unit)
                    • Else - Actions
              • Set random[(Player number of (Picked player))] = (Random integer number between 1 and 6)
              • For each (Integer A) from 1 to 120, do (Actions)
                • Loop - Actions
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Item-type of (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))])) Equal to Item[(Integer A)]
                    • Then - Actions
                      • Item - Set charges remaining in (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))]) to ((Charges remaining in (Item carried by (Picked unit) in slot (Player number of (Owner of (Picked unit))))) + 1)
                      • Unit - Increase level of Ability[(Integer A)] for (Picked unit)
                    • Else - Actions
        • Else - Actions
 

KILLCIDE

Arena Moderator
Level 36
Joined
Jul 22, 2015
Messages
3,487
  • Never use Integer A or B to loop; it is muuch slower than using your own custom integer variable
  • You can easily combine your multiple If/Then/Else
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • Level Less than or equal to 5
      • Then - Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Level Greater than 1
          • Then - Actions
            • -------- run "Level Greater than 1" actions here --------
          • Else - Actions
            • -------- run "Level Equal to 1" actions here --------
      • Else - Actions
        • -------- run "Level Greater than 5" actions here; no need for an extra If/Then/Else --------
  • I don't see the need of random[] being an array. All it is doing is selecting a random number for each loop iteration
  • Store (Picked unit), item and item-type of item carried by unit into a variable
  • What is the reason for looping up to 120 times?
 

KILLCIDE

Arena Moderator
Level 36
Joined
Jul 22, 2015
Messages
3,487
All right, sorry about that... now I see what you were doing after you explained it. The mixture of multiple If/Then/Else mixed up what I thought was going on.

  • Items
    • Events
    • Conditions
    • Actions
      • Unit Group - Pick every unit in ComputerPlayers and do (Actions)
        • Loop - Actions
          • Set TempUnit = (Picked unit)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Level Less than or equal to 5
            • Then - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • Level Equal to 1
                • Then - Actions
                  • -------- creating 2 random items (level 1) --------
                  • For each (Integer LoopInt) from 1 to 2, do (Actions)
                    • Loop - Actions
                      • Set Random = (Random integer number between 1 and 100)
                      • -------- --------
                      • Hero - Create Item[Random] and give it to TempUnit
                      • Set TempItem = (Last created item)
                      • Item - Set charges remaining in TempItem to 2
                      • Unit - Increase level of Ability[Random] for TempUnit
                • Else - Actions
                  • -------- creating a random item (levels 2-5) --------
                  • Set Random = (Random integer number between 1 and 100)
                  • -------- --------
                  • Hero - Create Item[Random] and give it to TempUnit
                  • Set TempItem = (Last created item)
                  • Item - Set charges remaining in TempItem to 2
                  • Unit - Increase level of Ability[Random] for TempUnit
            • Else - Actions
              • -------- selecting 2 random items from slot and increasing their levels (levels 6+) --------
              • For each (Integer LoopInt) from 1 to 2, do (Actions)
                • Loop - Actions
                  • Set Random = (Random integer number between 1 and 6)
                  • Set TempItem = (Item carried by TempUnit in slot Random)
                  • Set ItemType = (Item-type of TempItem)
                  • Set TempInt = (Charges remaining in TempItem)
                  • For each (Integer FindItem) from 1 to 100, do (Actions)
                    • Loop - Actions
                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                        • If - Conditions
                          • ItemType Equal to Item[FindItem]
                        • Then - Actions
                          • Game - Display to (All players) the text: Found item!
                          • Item - Set charges remaining in TempItem to (TempInt + 1)
                          • Unit - Increase level of Ability[FindItem] for TempUnit
                          • Custom script: exitwhen true
                        • Else - Actions
                          • Game - Display to (All players) the text: Searching for item....

I tested this a couple time and I didn't experience any lag spikes. If you are encountering a lag spike still, there are probably overlapping actions elsewhere.
 
This is the updated trigger, it follows your pattern.

EDIT: You were right that integer A and B are not reliable.
Maybe the lag at beginning is from the trigger that create the heroes. I'll edit that too.

EDIT 2: I updated also the trigger that creates heroes. The trigger that creates heroes causes lag, but good that you have to create the heroes only once.
The item-giving trigger also lags.

  • Spawn Computer Heroes
    • Events
    • Conditions
    • Actions
      • Player Group - Pick every player in Computer_Players and do (Actions)
        • Loop - Actions
          • Unit - Create 1 (Unit-type of (Random unit from Pick_Up_Types)) for (Picked player) at Start_Loc[(Player number of (Picked player))] facing Default building facing degrees
          • Set Hero[(Player number of (Picked player))] = (Last created unit)
          • Unit Group - Add (Last created unit) to Computer_Players_Gr
          • Unit - Remove Player_Dummy[(Player number of (Picked player))] from the game
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Player number of (Picked player)) Less than or equal to 5
            • Then - Actions
              • Unit Group - Add (Last created unit) to TEAM_HEROES[1]
              • Player - Change color of (Owner of (Last created unit)) to Red, Changing color of existing units
            • Else - Actions
              • Unit Group - Add (Last created unit) to TEAM_HEROES[2]
              • Player - Change color of (Owner of (Last created unit)) to Blue, Changing color of existing units
      • Multiboard - Set the text for Multiboard item in column 1, row 1 to (|c00FF0000 + (Players: + ((String((Number of units in TEAM_HEROES[1]))) + ( | Wins: + (String(Result[1]))))))
      • Multiboard - Set the text for Multiboard item in column 1, row 2 to (|c000042FF + (Players: + ((String((Number of units in TEAM_HEROES[2]))) + ( | Wins: + (String(Result[2]))))))
      • Unit Group - Pick every unit in Computer_Players_Gr and do (Actions)
        • Loop - Actions
          • Custom script: call RunHeroAI(GetEnumUnit())


  • Items for Computers
    • Events
    • Conditions
    • Actions
      • Unit Group - Pick every unit in Computer_Players_Gr and do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Level Equal to 1
            • Then - Actions
              • For each (Integer LoopInt) from 1 to 2, do (Actions)
                • Loop - Actions
                  • Set random[(Player number of (Owner of (Picked unit)))] = (Random integer number between 1 and 108)
                  • Hero - Create Item[random[(Player number of (Owner of (Picked unit)))]] and give it to (Picked unit)
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Charges remaining in (Last created item)) Not equal to 0
                    • Then - Actions
                      • Item - Set charges remaining in (Last created item) to 2
                      • Unit - Set level of Ability[random[(Player number of (Owner of (Picked unit)))]] for (Picked unit) to (Charges remaining in (Last created item))
                    • Else - Actions
            • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • And - All (Conditions) are true
                • Conditions
                  • Level Greater than 1
                  • Level Less than or equal to 5
            • Then - Actions
              • Set random[(Player number of (Owner of (Picked unit)))] = (Random integer number between 1 and 108)
              • Hero - Create Item[random[(Player number of (Owner of (Picked unit)))]] and give it to (Picked unit)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Charges remaining in (Last created item)) Not equal to 0
                • Then - Actions
                  • Item - Set charges remaining in (Last created item) to 2
                  • Unit - Set level of Ability[random[(Player number of (Owner of (Picked unit)))]] for (Picked unit) to (Charges remaining in (Last created item))
                • Else - Actions
            • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Level Greater than 5
            • Then - Actions
              • For each (Integer LoopInt) from 1 to 2, do (Actions)
                • Loop - Actions
                  • Set random[(Player number of (Picked player))] = (Random integer number between 1 and 6)
                  • For each (Integer ItemType_Check) from 1 to 108, do (Actions)
                    • Loop - Actions
                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                        • If - Conditions
                          • And - All (Conditions) are true
                            • Conditions
                              • (Charges remaining in (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))])) Not equal to 0
                              • (Item-type of (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))])) Equal to Item[ItemType_Check]
                        • Then - Actions
                          • Item - Set charges remaining in (Item carried by (Picked unit) in slot random[(Player number of (Owner of (Picked unit)))]) to ((Charges remaining in (Item carried by (Picked unit) in slot (Player number of (Owner of (Picked unit))))) + 1)
                          • Unit - Increase level of Ability[ItemType_Check] for (Picked unit)
                        • Else - Actions
            • Else - Actions

 
Last edited:
  • Items for Computers
    • Events
    • Conditions
    • Actions
      • Unit Group - Pick every unit in Computer_Players_Gr and do (Actions)
        • Loop - Actions
          • Set TempUnit = (Picked unit)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Level Less than or equal to 5
            • Then - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • Level Equal to 1
                • Then - Actions
                  • For each (Integer LoopInt) from 1 to 2, do (Actions)
                    • Loop - Actions
                      • Set random = (Random integer number between 1 and 108)
                      • Hero - Create Item[random] and give it to TempUnit
                      • Set TempItem = (Last created item)
                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                        • If - Conditions
                          • (Charges remaining in TempItem) Not equal to 0
                        • Then - Actions
                          • Item - Set charges remaining in (Last created item) to 2
                          • Unit - Add Ability[random] to TempUnit
                          • Unit - Increase level of Ability[random] for TempUnit
                        • Else - Actions
                • Else - Actions
                  • Set random = (Random integer number between 1 and 108)
                  • Hero - Create Item[random] and give it to TempUnit
                  • Set TempItem = TempItem
                  • Unit - Add Ability[random] to TempUnit
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Charges remaining in TempItem) Not equal to 0
                    • Then - Actions
                      • Item - Set charges remaining in TempItem to 2
                      • Unit - Add Ability[random] to TempUnit
                      • Unit - Increase level of Ability[random] for TempUnit
                    • Else - Actions
            • Else - Actions
              • For each (Integer LoopInt) from 1 to 2, do (Actions)
                • Loop - Actions
                  • Set random = (Random integer number between 1 and 6)
                  • Set TempItem = (Item carried by (Picked unit) in slot random)
                  • Set TempItemType = (Item-type of TempItem)
                  • Set TempCharges = (Charges remaining in TempItem)
                  • For each (Integer ItemType_Check) from 1 to 108, do (Actions)
                    • Loop - Actions
                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                        • If - Conditions
                          • And - All (Conditions) are true
                            • Conditions
                              • TempCharges Not equal to 0
                              • TempItemType Equal to Item[ItemType_Check]
                        • Then - Actions
                          • Item - Set charges remaining in TempItem to (TempCharges + 1)
                          • Unit - Increase level of Ability[ItemType_Check] for TempUnit
                          • Custom script: exitwhen true
                        • Else - Actions
How is it now?
It still lags - frezes is more accurate. Whenever the trigger runs there is some freeze for a moment... the more computer heroes there are the longer is the freeze.

Why don't you test your trigger on 10 heroes and tell me what is the result? My map has 10 players.

EDIT: I tested with 10 players and it crashed! With 8 or less doesn't crash, but 10...

I can even give you the map if you want, to check out the trigger, cause only IT causes this, nothing else.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 58
Joined
Jan 18, 2005
Messages
26,507
Never use Integer A or B to loop; it is muuch slower than using your own custom integer variable
No it is not much slower. It is only slightly slower.

EDIT: You were right that integer A and B are not reliable.
They are reliable if used correctly. You cannot run more than 1 instance of A and 1 instance of B at the same time. This can be a problem if a trigger causes events to fire which interrupt the trigger's execution while inside one of those loops.

It still lags - frezes is more accurate. Whenever the trigger runs there is some freeze for a moment... the more computer heroes there are the longer is the freeze.
Probably the result of it loading the abilities as they are not pre-loaded.

EDIT: I tested with 10 players and it crashed! With 8 or less doesn't crash, but 10...
Could be one or more of the abilities are broken. You will really need to post the map (or an appropriate extract there of) for people to diagnose the cause.

Make sure Computer_Players_Gr is quite small (eg 10 hero units only). If Computer_Players_Gr contains every unit on the map, eg by accident, then there is a good chance of performance problems.
 
I have another idea - to put all abilities on the heroes and disable them at the initialization and to enable them and upgrade them when called

  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • For each (Integer B) from 1 to 108, do (Actions)
        • Loop - Actions
          • Player - Disable Ability[(Integer B)] for (Picked player)
Like this will kinda do but it crashed the map on start.
 
Top