[Trigger] Chance for a periodic random event not looping

Level 3
Joined
May 16, 2024
Messages
13
Hi there!
So, I tried to make a trigger that when every x seconds passes, it has a 33% chance that an event will happen. To acheive that, I tried using a random integer number between 1 and 3. After that, if the chance for a random event hits, it again picks a random number (this time between 1 and 2) to specify which event will take place - or at least that's what I was hoping to achieve. The trigger fires off only once, while it should loop after the right number (in this case, it is 1) for an event isn't picked. I feel like this should be easy to do and yet, something is definitely wrong and I am not sure what. I will appreciate any help.
Here are the triggers themselves:

  • EventsLoop
    • Events
      • Time - Every 5.00 seconds of game time
    • Conditions
    • Actions
      • Trigger - Run EmitRandomEvent <gen> (checking conditions)
      • Trigger - Turn off (This trigger) \\ -------------> I want this trigger to be turned off after it fires, so there wouldn't be two events taking place at the same time

  • EmitRandomEvent
    • Events
    • Conditions
    • Actions
      • Set RandomEventInteger = (Random integer number between 1 and 3)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • RandomEventInteger Equal to 1
        • Then - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
            • Then - Actions
              • Set RandomEventInteger = (Random integer number between 1 and 2)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • RandomEventInteger Equal to 1
                • Then - Actions
                  • Trigger - Turn on CaravanSpawn <gen>
                  • Trigger - Run CaravanSpawn <gen> (checking conditions)
                  • Trigger - Turn off EventsLoop <gen>
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • RandomEventInteger Equal to 2
                • Then - Actions
                  • Trigger - Run OrcWarbandSpawn <gen> (checking conditions)
                • Else - Actions
            • Else - Actions
        • Else - Actions
          • Trigger - Turn on EventsLoop <gen> \\ ------------> This was supposed to make it loop when the random integer number draws either 2 or 3


Thanks in advance!
 
Last edited:

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
Hello! So I'd use a Timer variable here and manage it all in one trigger. Timers give you far greater control over how and when your triggers run:
  • EmitRandomEvent
    • Events
      • Time - RandomEventTimer expires
    • Conditions
    • Actions
      • Set RandomEventInteger = (Random integer number between 1 and 3)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • RandomEventInteger Equal to 1
        • Then - Actions
          • Set RandomEventInteger = (Random integer number between 1 and 2)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • RandomEventInteger Equal to 1
            • Then - Actions
              • Trigger - Run CaravanSpawn <gen> (ignoring conditions)
            • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • RandomEventInteger Equal to 2
                • Then - Actions
                  • Trigger - Run OrcWarbandSpawn <gen> (ignoring conditions)
                • Else - Actions
        • Else - Actions
          • Countdown Timer - Start RandomEventTimer as a One-shot timer that will expire in 5.00 seconds
Changes:
  • I deleted a random empty If Then Else from the original trigger.
  • I deleted unnecessary Actions that were paired with CaravanSpawn. You could make that trigger turn itself on at the start of it's Actions.
  • I deleted the EventsLoop trigger. Note that you'll need to Run the above trigger to get the whole process up and running.
  • I start the Timer again if you fail to get an Event.
  • I use the Else - Actions section for a slight optimization. It'll only check to see if RandomEventInteger equals 2 IF it didn't equal 1.
  • I changed (checking conditions) to (ignoring conditions). Only use (checking) if you rely on the initial Conditions section, those are the only Conditions it's referring to.
Tip: If the Events have a time limit then you could Start the RandomEventTimer along with running your Spawn trigger. For example, say that CaravanSpawn is a 30.00 second long Event and RandomEventTimer should be Paused during that, you can Start the Timer again for 35.00 seconds to sync it up:
  • Trigger - Run CaravanSpawn <gen> (ignoring conditions)
  • Countdown Timer - Start RandomEventTimer as a One-shot timer that will expire in 35.00 seconds
But if the "Random Events" are designed around objectives needing to be completed then you could simply Run the above trigger again once that happens.
 
Last edited:
Level 3
Joined
May 16, 2024
Messages
13
Thank you very much for your reply!

So I'd use a Timer variable here (...)
Somehow, I completely forgot that I could use those :hohum:

  • I use the Else - Actions section for a slight optimization. It'll only check to see if RandomEventInteger equals 2 IF it didn't equal 1.
Yes, that will certainly do the trick with only two events, however the reason I resorted to multiple If/Then/Else functions is because I plan to add more events along the way (I probably should have specified that earlier, sorry) and I'm not sure if I'll be able to pack them into a single Else section for that to work.

Only use (checking) if you rely on the initial Conditions section, those are the only Conditions it's referring to.
That's actually great to know, thanks again!
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
Yes, that will certainly do the trick with only two events, however the reason I resorted to multiple If/Then/Else functions is because I plan to add more events along the way (I probably should have specified that earlier, sorry) and I'm not sure if I'll be able to pack them into a single Else section for that to work.
You can chain as many If Then Elses together as you want. The logic would go like this:

If Condition A is True THEN (do something cool)
Else if Condition B is True THEN (do something cool)
Else if Condition C is True THEN (do something cool)

If A is True then B and C will never get checked, saving on operations. If B is True then C won't get checked, saving on an operation. This pattern can continue on and on.

That being said, this kind of optimization isn't all that necessary for such a lightweight trigger as your own and you may prefer to separate the If Then Elses for visual clarity. It's wise to do this optimization in a trigger that runs often especially if the Conditions are complex. That is not the case for your trigger, at least not at the moment.
 
Last edited:
Level 3
Joined
May 16, 2024
Messages
13
Tried this today and it works! Thanks a lot!

Also, I don't know if this is common knowledge or not, but I have noticed something funny while testing it.
When testing things that depend on a random chance, such as this one, you shouldn't do this via the Editor.
Why? Because when you do it this way, random sequences are not really random - or at least they weren't in my case.
You get the exact same combinations every time you test the map. This is not an issue when launching the map from the game menu.
For example:
When the timer expired every 5 sec, It always picked the events in the exact same sequence: Nothing, OrcWarband, Nothing, Nothing, Caravan
Even when I restarted the editor.
But when launching the map from the game menu, it wasn't the case - It was truly random every time I reloaded the map.
Tested on version 1.30.4
I think that's pretty weird :p
 
Level 30
Joined
Aug 29, 2012
Messages
1,382
In File > Preferences, make sure you have this option disabled, otherwise random won't be random ;)

1738942767033.png
 
Top