Triggers - Countdown Timers

Tutorial By Ghan_04

I recently searched the tutorials for one on timers and came up dry, so I thought I'd create this basic overview for new mappers.


TIMERS

I. What is a Timer?
II. Why are Timers used?
III. How to create a Timer
IV. Extras on Timers
V. Counting Up




I. What is a Timer?
A timer is a window that appears after a certain event that then counts down toward 0 in seconds from a specified time. There are two parts to a timer, the timer itself, which is to say the numbers that are counting down, and the timer window, which is the box that contains the numbers.

II. Why are Timers used?
Timers have many uses, and new ones are being developed all the time. A timer can be used to count down the time left to accomplish an objective, or maybe to show the time until reinforcements arrive. But these are just two common examples of timers. Many more are out there to be discovered.

III. How to create a Timer
Now, to the fun part. Triggering a timer. As I said, a timer has two parts, the timer and the window. Both of these will need to be taken care of in order to properly incorporate a timer into your map. Before you start creating triggers at random though, you'll need to create some variables to store the information. You should create one variable for the timer, and one for the window. The variables allow the computer to keep track of your timer, in the event that you have more than one on the map either at the same time or different times. Once you have created your variables, then you can create your triggers. The first trigger should have the event that you want to trigger the timer to display. This can be any of the events from the list in the trigger editor. For the actions, first, you need to create a window for your timer. (Remember, we just created the variable for the window, we didn't actually create the window.) Enter your timer variable so the computer can associate the timer with its proper window. This action should look like this:

Trigger:
  • Countdown Timer - Create a timer window for MyTimer with title Time Left


Now you should store the window you just created so you can get rid of it later. (If you don't do this, you'll end up with a window displaying 0's just sitting at the top of the screen.) Create an action like this:

Trigger:
  • Set MyTimerWindow = (Last created timer window)


Now you should show the window so players can see it:

Trigger:
  • Countdown Timer - Show MyTimerWindow


Next, you can start the timer with your specified time remaining. This can only be done in seconds, so make sure to convert the minutes you want into seconds:

Trigger:
  • Countdown Timer - Start MyTimer as a One-shot timer that will expire in 600.00 seconds


So your finished trigger will look like this:

Trigger:
  • Start Timer
    • Events
    • <Add your event here>
    • Conditions
    • Actions
      • Countdown Timer - Create a timer window for MyTimer with title Time Left
      • Set MyTimerWindow = (Last created timer window)
      • Countdown Timer - Show MyTimerWindow
      • Countdown Timer - Start MyTimer as a One-shot timer that will expire in 30.00 seconds


Now, to get rid of the timer, it is a simple matter of adding an action to the trigger that will go off when the timer expires. This is a simple event, and you can add any actions you like to it. Make the trigger look like this:

Trigger:
  • Timer Finish
    • Events
      • Time - MyTimer expires
    • Conditions
    • Actions
      • Countdown Timer - Destroy MyTimerWindow
      • <Add actions here>


This will destroy the window when it's done. That's it, you're finished! :D

Here's something else. It's possible to create and use a timer without showing a window. This can be useful if you want a timer to count down something for a trigger event, but you don't want the players to see it. In this case, you would simply eliminate the window variable and actions. Other than being invisible, it will still function like a normal timer.


IV. Extras on Timers
Just a couple other interesting things to do with timers. (Feel free to post other ideas here, as well.)

1. Use a timer, and randomly give players a powerful unit for a set amount of time.
2. Use a timer to keep track of the time of day, and have a special action happen during the shift between night and day (such as opening and closing a gate).
3. Use a timer to end the game after a set period of time, giving victory to the player with the greatest overall score.
4. Allow players a certain amount of time to build before allowing them to attack.
5. Give players kill quotas to meet after set times in the game.
6. Only allow more advanced units to be built after a timer goes off.

Here's something else about timers. Say you want to measure how much time something took. If you have a timer set for 1 min to complete a task, then when the task is finished, the time left is 15 seconds. How do you get the time remaining? Simple. When the timer stops:
Trigger:
  • Set MyRealVariable = (Elapsed time for (MyTimer))





V. Counting Up

This question has come up in the past, and there hasn't (to my knowledge) been a satisfactory answer. How do you create a timer that tracks the ELAPSED time of a game? First of all, there is no magic action that tells a timer to count up. But, with a little bit of a workaround, you can accomplish the same thing without having to worry about having integers track the minutes, seconds, and hours, and then have to put the colons in between. You can use the timer's own system to take care of that. Now to the triggers. First, for a count up timer, create and display the window at map initialization. Insert a wait after the melee actions because a window cannot be displayed at map initialization. So your trigger should look like this:

Trigger:
  • Melee Initialization
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Melee Game - Use melee time of day (for all players)
      • Melee Game - Limit Heroes to 1 per Hero-type (for all players)
      • Melee Game - Give trained Heroes a Scroll of Town Portal (for all players)
      • Melee Game - Set starting resources (for all players)
      • Melee Game - Remove creeps and critters from used start locations (for all players)
      • Melee Game - Create starting units (for all players)
      • Melee Game - Run melee AI scripts (for computer players)
      • Melee Game - Enforce victory/defeat conditions (for all players)
      • Wait 1.00 seconds
      • Countdown Timer - Create a timer window for MyTimer with title Time Elapsed
      • Set MyTimerWindow = (Last created timer window)
      • Countdown Timer - Show MyTimerWindow


Now, how do you keep track of the increasing time in the game? Well, you will need ONE real variable. So, create a real variable. Next, create a trigger whose event is a periodic event every one second:

Trigger:
  • Time - Every 1.00 seconds of game time


Then, for the first action, set the real to the real + 1 like this:

Trigger:
  • Set GameTime = (GameTime + 1.00)


Next, just like before, start your timer, but this time have the time equal to the variable:

Trigger:
  • Countdown Timer - Start MyTimer as a One-shot timer that will expire in GameTime seconds


Then immediately pause the timer because we DON'T want it to count down, right? So use this action:

Trigger:
  • Countdown Timer - Pause MyTimer


Your finished trigger should look like this:

Trigger:
  • Count Up Timer
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Set GameTime = (GameTime + 1.00)
      • Countdown Timer - Start MyTimer as a One-shot timer that will expire in GameTime seconds
      • Countdown Timer - Pause MyTimer



Now you're set! You'll have a timer that will keep up with the time elapsed.

BUT WAIT!!! You might be saying, "I want to put my time in a multiboard!" Well, let's take a look at how to do that.
First, if you think about it, you realize that you will need to use more variables because the timer system cannot operate in a multiboard. So, first, you'll need to create 3 variables, one for the seconds, one for the minutes, and one for the hours. Name these Integer variables Seconds, Minutes, and Hours. Now if you're really on top of things, you'll realize that these integers will not have the 'filler' 0's that come with a timer. It will display something like 2:5:9 instead of the cleaner 02:05:09. This presents a problem. You need to be able to insert a 0 before the integers, but only if they are below 10. For this reason, create ANOTHER set of 3 variables, but make these string variables and name them SecondString, MinuteString, and HourString. If you are completely lost as to why you should create these strings, be patient, as it will become clear later. As there are several tutorials on how to create multiboards, I will not go into details there. I will focus instead on the triggers needed to run the time elapsed. So, create one trigger for timekeeping. Make it's event every one second of game-time because you will need to increase the value of the seconds every second (unexpected, huh?). Then add the action to increase the seconds, like this:

Trigger:
  • Set Seconds = (Seconds + 1)


The next part is a tad more difficult. You need to increase the minutes, but only when the seconds are at sixty. So add an IF, THEN, ELSE function after the seconds. For the condition, you want when the seconds variable becomes equal to 60, and for the action, you want to increase the minutes by one. So, do this:

Trigger:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • Seconds Equal to 60
    • Then - Actions
      • Set Seconds = 0
      • Set Minutes = (Minutes + 1)
    • Else - Actions


This takes care of the minutes as well as setting up the seconds for another round. Our next order of business is to do the hours. It is done much the same way as the seconds/minutes function, this time with minutes/hours. So add this action under the current IF, THEN, ELSE:

Trigger:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • Minutes Equal to 60
    • Then - Actions
      • Set Minutes = 0
      • Set Hours = (Hours + 1)
    • Else - Actions


So the trigger up to this point should look like:

Trigger:
  • Actions
    • Set Seconds = (Seconds + 1)
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • Seconds Equal to 60
      • Then - Actions
        • Set Seconds = 0
        • Set Minutes = (Minutes + 1)
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Minutes Equal to 60
          • Then - Actions
            • Set Minutes = 0
            • Set Hours = (Hours + 1)
          • Else - Actions
      • Else - Actions


Unfortunately, the next part is harder. Dealing with the 'filler' 0's. Here is where the strings come into play. Now, in the event that the seconds, minutes, and/or hours are lower than 10, you need to add the zero. So, first is to create an IF, THEN, ELSE for the seconds that will check when they are below 10. The actions will set the second STRING to a 0 plus the integer itself so that when the STRING is displayed, it will look like a 0 and another number right after it. If you're confused, have a look:

Trigger:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • Seconds Less than 10
    • Then - Actions
      • Set SecondString = (0 + (String(Seconds)))
    • Else - Actions
      • Set SecondString = (String(Seconds))


This function says that, if the seconds are below 10, set the string equal to a 0 plus the seconds. If it is not below 10, then just set the string equal to the seconds. The functions for the minutes and hours are essentially the same:

Trigger:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • Minutes Less than 10
    • Then - Actions
      • Set MinuteString = (0 + (String(Minutes)))
    • Else - Actions
      • Set MinuteString = (String(Minutes))


Trigger:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • Hours Less than 10
    • Then - Actions
      • Set HourString = (0 + (String(Hours)))
    • Else - Actions
      • Set HourString = (String(Hours))


Your finished trigger should look like this:

Trigger:
  • Time
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Set Seconds = (Seconds + 1)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Seconds Equal to 60
        • Then - Actions
          • Set Seconds = 0
          • Set Minutes = (Minutes + 1)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Minutes Equal to 60
            • Then - Actions
              • Set Minutes = 0
              • Set Hours = (Hours + 1)
            • Else - Actions
        • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Seconds Less than 10
        • Then - Actions
          • Set SecondString = (0 + (String(Seconds)))
        • Else - Actions
          • Set SecondString = (String(Seconds))
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Minutes Less than 10
        • Then - Actions
          • Set MinuteString = (0 + (String(Minutes)))
        • Else - Actions
          • Set MinuteString = (String(Minutes))
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Hours Less than 10
        • Then - Actions
          • Set HourString = (0 + (String(Hours)))
        • Else - Actions
          • Set HourString = (String(Hours))


Ok, now all that's left is to display this in the multiboard. Use this action that will add colons in between the strings to make it look nicer:

Trigger:
  • Multiboard - Set the text for MyMultiboard item in column 1, row 1 to (HourString + (: + (MinuteString + (: + SecondString))))


Notice how there are no Integer-to-String conversions, because it uses the 3 string variables. The conversions are taken care of in the tracking trigger.




Well, that's all folks! Have fun! Feedback is welcomed.

Click here to comment on this tutorial.
 
 
Blizzard Entertainment, Inc.
Silkroad Online Forums
Team Griffonrawl Trains Muay Thai and MMA fighters in Ohio.
Apex Steel Pipe - Buys and sells Steel Pipe.