Author Topic: Timer: Stopwatch and Countdown  (Read 1086 times)

Gizgord

  • average
  • Level 18: Statix
  • *
  • Posts: 835
    • View Profile
Timer: Stopwatch and Countdown
« on: February 26, 2023, 03:38:02 PM »
Follow the instructions and you'll have set up a working timer in no time! I will explain what functions each command line serves as we go along.
We'll start with a Stopwatch, then tackle a Countdown.



First off, prepare text commands to draw our timer onto the screen. In your area's Start Cmd.:
Code: [Select]
overhead=2
overhead size=small
I'm using overhead text for its modifiable size options. If you wish to use overhead text for something else, make use of <hud> text commands instead.
Code: [Select]
timeline [ID]Everything else is going to be done in a timeline.

Our timeline has two events. First one of which will carry out the entire operation, while the second one only reverts its position. Make sure these two events are exactly one second apart. I recommend simply 0:0:0 and 0:1:0.
We're going to establish two variables: var min and var sec, for counting minutes and seconds respectfully.
Which type of a variable should you use? It depends on your use case. Area variables (var) will only track within said area. Course variables (course var) could work if your level consists of multiple exists to different areas. Finally, game variables (game var) can keep track of an entire playthrough. As an example, it could be used as a built-in tool for speedrunners.



Now, let's carry on with the timeline. First event:
Code: [Select]
^[var min]:[var sec]
if var sec<10
^[var min]:0[var sec]
This is the text that will be drawn for our timer. Square brackets '[ ]' read variables' value, don't confuse them with '( )' or '{ }' !
The conditional checks for when var sec's value is in single digits. In such case, different text will be drawn, where [var sec] is proceeded by a zero.
Code: [Select]
var sec+1This is pretty self-explanatory. For every second that passes, the value increases.
Code: [Select]
if var sec=60
{
var sec=0
var min+1
}
Once the value of var sec reaches 60, a minute is added and seconds are reverted back to zero.

Second event:
Code: [Select]
timeline [ID] position=0.0.0This reverts our timeline back to its beginning, so that it loops continuously.


That's it for our stopwatch!
A countdown is similar, but a bit more tricky to pull off. If you have already created a stopwatch timer, duplicate its timeline to save some time.
We'll start in our area's Startup Cmd. once again:
Code: [Select]
overhead=2
overhead size=small
timeline [ID]
Note: Don't forget to update the timeline ID!
Code: [Select]
var min=2Since this timer counts down, you get to choose its starting value! You can make this anything you want, as long as it isn't negative...

Let's proceed with the first event of our timeline:
Code: [Select]
^[var min]:[var sec]
if var sec<10
^[var min]:0[var sec]
This bit remains the same.
Code: [Select]
if var min=0 & var sec=0
{
stop timeline [ID]
^Time out!
}
Once both [var min] and [var sec] reach zero, we can stop our timeline. Whatever you want to happen aften a time-out (kill or reward the player, reveal a boss, change areas), include it inside of this conditional.
Code: [Select]
var sec-1This time, the value goes down by every second.
Code: [Select]
if var sec<0
{
var sec=59
var min-1
}
Once the value of [var sec] reaches beyond zero, a minute is subtracted and seconds go up to 59.

Once again, the second event serves only to track our timeline back.
Code: [Select]
timeline [ID] position=0.0.0Note: If you duplicated this timeline from the previous timer, update ID's of both timeline commands!

I'm going to leave you with some notes on how to expand this timer to count milliseconds.
First off, you want to transfer all commands that draw text from Event 1 to... area's Looping Cmd. On top of that, add:
Code: [Select]
area looping=constant(looping=staggered will make the timer skip). Of course, now's the time to fill in the gaps with a [var ms].
Our timelines now require 10 events, spaced for every '0.0.1'. Starting from the first event and going down to ninth, add var ms=x. In a stopwatch, replace X with 0, 1, 2... 9. In a countdown, go backwards: 9, 8, 7... 0.



That should do it! Feel free to ask questions if you have any. Otherwise, good luck!
« Last Edit: February 26, 2023, 04:00:35 PM by Gizgord »