Timer
倒数计时器。
描述
The Timer node is a countdown timer and is the simplest way to handle time-based logic in the engine. When a timer reaches the end of its wait_time, it will emit the timeout signal.
After a timer enters the scene tree, it can be manually started with start(). A timer node is also started automatically if autostart is true.
Without requiring much code, a timer node can be added and configured in the editor. The timeout signal it emits can also be connected through the Signals dock in the editor:
func _on_timer_timeout():
print("Time to attack!")
Note: To create a one-shot timer without instantiating a node, use SceneTree.create_timer().
Note: Timers are affected by Engine.time_scale unless ignore_time_scale is true. The higher the time scale, the sooner timers will end. How often a timer processes may depend on the framerate or Engine.physics_ticks_per_second.
教程
属性
|
||
|
||
|
||
|
||
|
方法
is_stopped() const |
|
void |
|
void |
stop() |
信号
timeout() 🔗
当计时器计时完成时发出。
枚举
enum TimerProcessCallback: 🔗
TimerProcessCallback TIMER_PROCESS_PHYSICS = 0
在物理处理帧中更新计时器(见 Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS)。
TimerProcessCallback TIMER_PROCESS_IDLE = 1
在处理(渲染)帧中更新计时器(见 Node.NOTIFICATION_INTERNAL_PROCESS)。
属性说明
如果为 true,则计时器将在进入场景树时自动启动。
注意:计时器进入场景树后,该属性会自动设置为 false。
注意:计时器在编辑器中运行时该属性无效。
bool ignore_time_scale = false 🔗
如果为 true,则计时器会忽略 Engine.time_scale,使用真实经过的时间进行更新。
如果为 true,则计时器将在完成时停止。否则默认情况下会自动重新启动。
如果为 true,则计时器处于暂停状态。即便调用了 start(),处于暂停状态的计时器也不会进行处理,必须将这个属性设回 false 才会继续。另见 stop()。
TimerProcessCallback process_callback = 1 🔗
void set_timer_process_callback(value: TimerProcessCallback)
TimerProcessCallback get_timer_process_callback()
指定计时器在主循环的哪个时间点进行更新。
float get_time_left()
计时器的剩余时间,单位为秒。如果计时器处于停止状态,则始终为 0。
注意:这个属性是只读的,无法进行修改。基于的是 wait_time。
计时器完成计时所需的时间,单位为秒。这个属性也可以在每次调用 start() 时设置。
注意:计时器的处理只能在物理帧或处理帧进行一次(取决于 process_callback)。如果帧率不稳定,则计时完成所需的时间也可能不一致,等待时间小于 0.05 秒左右的情况下尤为明显。如果计时器非常短,建议自己编写代码,不要使用 Timer 节点。计时器还会受到 Engine.time_scale 的影响。
方法说明
如果定时器处于停止状态或尚未启动,则返回 true。
void start(time_sec: float = -1) 🔗
如果计时器尚未启动,则启动或重置计时器。如果计时器不在场景树中则会失败。如果 time_sec 大于 0,则会将其用于 wait_time。
注意:这个方法不会恢复已暂停的定时器。见 paused。
void stop() 🔗
停止计时器。另见 paused。与 start() 不同,计时器不在场景树中时可以安全地调用该方法。
注意:调用 stop() 不会发出 timeout 信号,因为计时器并未超时。如果需要信号,请在调用 stop() 后使用 $Timer.timeout.emit() 手动发出。