0 votes

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.

in Engine by (235 points)

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?

Minutes:Seconds:Miliseconds

1 Answer

+1 vote

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)

print(time)

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

by (3,501 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.