I'm trying to make a digital clock in my game and need the current time (hours and minutes), formatted as so: HH:MM:SS
How can I get the time necessary for this?
How can I get the current time in Godot?
You can use the Time
singleton, with the get_datetime_dict_from_system
method to get the time dictionary from the current system time.
# Get the time dictionary
var time = Time.get_time_dict_from_system()
print(time) # {day:X, dst:False, hour:xx, minute:xx, month:xx, second:xx, weekday:x, year:xxxx}
In your case, to format it as HH:MM
, you can use the less noisy function get_time_dict_from_system
which only has hours, minutes, and seconds
var time = Time.get_time_dict_from_system()
# we can use format strings to pad it to a length of 2 with zeros, e.g. 01:20:12
print("%02d:%02d:%02d" % [time.hour, time.minute, time.second])
For older versions of Godot (-3.4
), OS.get_time()
also works but in 3.5+
it's deprecated and 4.x
it's removed:
If you use Godot 4.x you can use Time.get_time_string_from_system()
which returns a string of system time formatted as HH:MM:SS
.
© 2022 - 2024 — McMap. All rights reserved.