我正在尝试在游戏中制作一个数字时钟,需要当前时间(小时和分钟),格式如下:
HH:MM:SS
我怎样才能获得所需的时间?
Time
单例,通过 get_datetime_dict_from_system
方法从当前系统时间获取时间字典。
# 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}
在您的情况下,要将其格式化为
HH:MM
,您可以使用噪音较小的函数get_time_dict_from_system
,它只有小时、分钟和秒
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])
对于旧版本的 Godot (
-3.4
),OS.get_time()
也可以使用,但在 3.5+
中它已被弃用并且 4.x
已被删除: