Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Timer

继承: Node < Object

倒数计时器。

描述

Timer 即计时器节点,是一种倒计时器,也是引擎中最简单的处理基于时间的逻辑的方法。计时器在等待 wait_time 结束后就会发出 timeout 信号。

计时器进入场景树时,可以使用 start 手动启动。如果 autostarttrue,计时器节点也会自动启动。

可以在编辑器中添加并配置计时器节点,无需编写特别多的代码。计时器发出的 timeout 信号可以在编辑器的“节点”面板中连接:

func _on_timer_timeout():
    print("是时候表演真正的技术了!")

注意:如果只想创建一次性的计时器,不想实例化节点,请使用 SceneTree.create_timer

注意:计时器会受到 Engine.time_scale 的影响。时间缩放值越大,计时器结束得越早。计时器的处理频率取决于帧率或 Engine.physics_ticks_per_second

教程

属性

bool

autostart

false

bool

ignore_time_scale

false

bool

one_shot

false

bool

paused

TimerProcessCallback

process_callback

1

float

time_left

float

wait_time

1.0

方法

bool

is_stopped() const

void

start(time_sec: float = -1)

void

stop()


信号

timeout() 🔗

当计时器计时完成时发出。


枚举

enum TimerProcessCallback: 🔗

TimerProcessCallback TIMER_PROCESS_PHYSICS = 0

在物理处理帧中更新计时器(见 Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS)。

TimerProcessCallback TIMER_PROCESS_IDLE = 1

在处理(渲染)帧中更新计时器(见 Node.NOTIFICATION_INTERNAL_PROCESS)。


属性说明

bool autostart = false 🔗

  • void set_autostart(value: bool)

  • bool has_autostart()

If true, the timer will start immediately when it enters the scene tree.

Note: After the timer enters the tree, this property is automatically set to false.

Note: This property does nothing when the timer is running in the editor.


bool ignore_time_scale = false 🔗

  • void set_ignore_time_scale(value: bool)

  • bool is_ignoring_time_scale()

If true, the timer will ignore Engine.time_scale and update with the real, elapsed time.


bool one_shot = false 🔗

  • void set_one_shot(value: bool)

  • bool is_one_shot()

如果为 true,则计时器将在完成时停止。否则默认情况下会自动重新启动。


bool paused 🔗

  • void set_paused(value: bool)

  • bool is_paused()

如果为 true,则计时器处于暂停状态。即便调用了 start,处于暂停状态的计时器也不会进行处理,必须将这个属性设回 false 才会继续。


TimerProcessCallback process_callback = 1 🔗

指定计时器在主循环的哪个时间点进行更新(见 TimerProcessCallback)。


float time_left 🔗

计时器的剩余时间,单位为秒。如果计时器处于停止状态,则始终为 0

注意:这个属性是只读的,无法进行修改。基于的是 wait_time


float wait_time = 1.0 🔗

  • void set_wait_time(value: float)

  • float get_wait_time()

计时器完成计时所需的时间,单位为秒。这个属性也可以在每次调用 start 时设置。

注意:计时器的处理只能在物理帧或处理帧进行一次(取决于 process_callback)。如果帧率不稳定,则计时完成所需的时间也可能不一致,等待时间小于 0.05 秒左右的情况下尤为明显。如果计时器非常短,建议自己编写代码,不要使用 Timer 节点。计时器还会受到 Engine.time_scale 的影响。


方法说明

bool is_stopped() const 🔗

如果定时器处于停止状态或尚未启动,则返回 true


void start(time_sec: float = -1) 🔗

Starts the timer, or resets the timer if it was started already. Fails if the timer is not inside the tree. If time_sec is greater than 0, this value is used for the wait_time.

Note: This method does not resume a paused timer. See paused.


void stop() 🔗

停止计时器。