Simple timer

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

How would I go about implementing a timer during a boss fight that displays minutes, seconds, and milliseconds elapsed in the following format? 00:00:00

I have the start of the code going and the game DOES count how many milliseconds have passed since the start of the boss fight, I’m just having trouble converting it into the format above.

So, you already have the time, but in miliseconds right? and you want to put it in hours:minutes:seconds format? or minutes:seconds:miliseconds?

p7f | 2020-07-20 17:13


9BitStrider | 2020-07-20 17:25

:bust_in_silhouette: Reply From: p7f

So you have miliseconds. To get the number of minutes you divide miliseconds by 60000. To get the number of seconds, you divide miliseconds by 1000, but as seconds go up to 60, you need to take the modulo of that. To get the remaining miliseconds, just take the modulus of the miliseconds ammount and 1000. In summary:

# assuming t has the miliseconds measured value
var minutes = int(t / 60 / 1000)
var seconds = int(t / 1000) % 60
var miliseconds = int(t) % 1000

var time = ("%02d" % minutes) + (":%02d" % seconds) + (":%03d" % miliseconds)

time is de formatted string. For example in “%02d”, 02 means quantity of digits, and d means integer, the % minutes after that means “use minutes number with that format”.

Maybe there are better ways… i just use that.

Also, miliseconds update to fast for you to being able to keep up showing smooth time… perhaps you should use just minutes:seconds