如何获取戈多的当前时间?

问题描述 投票:0回答:2

我正在尝试在游戏中制作一个数字时钟,需要当前时间(小时和分钟),格式如下:

HH:MM:SS
我怎样才能获得所需的时间?

godot gdscript
2个回答
10
投票

您可以使用

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
已被删除:


0
投票

如果您使用 Godot 4.x,您可以使用

Time.get_time_string_from_system()
,它返回格式为
HH:MM:SS
的系统时间字符串。

© www.soinside.com 2019 - 2024. All rights reserved.