Displaying a timer count down in a label minutes : seconds : milliseconds

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

This is what I have so far.
fruit ninja(ish) type game

timer is set to 180 (3mins)
with one shot and auto start selected

onready var label = $ContDownLabel
onready var gameTimer = $ContDownLabel/GameTimer

func _process(delta):
label.set_text(str(gameTimer.get_time_left()).pad_decimals(2))

This works but shows the count down in seconds starting at 180.00 how can I get to show the count down in 00:00:00 (minutes seconds milliseconds)?

:bust_in_silhouette: Reply From: timothybrentwood

Format as you see fit:

func time_to_minutes_secs_mili(time : float):
	var mins = int(time) / 60
	time -= mins * 60
	var secs = int(time) 
	var mili = int((time - int(time)) * 100)
	return str(mins) + ":" + str(secs) + ":" + str(mili)

Thanks for the push in the right direction timothybrentwood, much appreciated.

The final look of the code.

onready var label = $ContDownLabel
onready var gameTimer = $ContDownLabel/GameTimer

func _physics_process(delta):
label.set_text(str(time_to_minutes_secs_mili(gameTimer.get_time_left())))

func time_to_minutes_secs_mili(time : float):
var mins = int(time) / 60
time -= mins * 60
var secs = int(time)
var mili = int((time - int(time)) * 100)
return str(mins) + “:” + str(secs) + “:” + str(mili)

dev_Tfer | 2021-05-13 15:45

For those looking for a time format like this: “00:00:00”, you can replace the last line with:

return str("%0*d" % [2, mins]) + ":" + str("%0*d" % [2, secs]) + ":" + str("%0*d" % [2, mili]) 

This adds an extra 0 as padding if there’s only a single digit.

BlackDragonBE | 2022-08-15 15:37