Timer
A countdown 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 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 Node 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
. 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
.
属性
bool | autostart |
bool | one_shot |
bool | paused |
TimerProcessCallback | process_callback |
float | time_left |
float | wait_time |
方法
信号
timeout ( )
Emitted when the timer reaches the end.
枚举
enum TimerProcessCallback:
TimerProcessCallback TIMER_PROCESS_PHYSICS = 0
Update the timer every physics process frame (see Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS
).
TimerProcessCallback TIMER_PROCESS_IDLE = 1
Update the timer every process (rendered) frame (see Node.NOTIFICATION_INTERNAL_PROCESS
).
属性说明
bool
autostart = false
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
.
bool
one_shot = false
If true
, the timer will stop after reaching the end. Otherwise, as by default, the timer will automatically restart.
bool
paused
If true
, the timer is paused. A paused timer does not process until this property is set back to false
, even when start
is called.
TimerProcessCallback process_callback = 1
void
set_timer_process_callback ( value: TimerProcessCallback )- TimerProcessCallback get_timer_process_callback ( )
Specifies when the timer is updated during the main loop (see TimerProcessCallback).
float
time_left
float
get_time_left ( )
The timer's remaining time in seconds. This is always 0
if the timer is stopped.
Note: This property is read-only and cannot be modified. It is based on wait_time
.
float
wait_time = 1.0
The time required for the timer to end, in seconds. This property can also be set every time start
is called.
Note: Timers can only process once per physics or process frame (depending on the process_callback
). An unstable framerate may cause the timer to end inconsistently, which is especially noticeable if the wait time is lower than roughly 0.05
seconds. For very short timers, it is recommended to write your own code instead of using a Timer node. Timers are also affected by Engine.time_scale
.
方法说明
Returns true
if the timer is stopped or has not started.
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 ( )
Stops the timer.
本方法通常需要用户覆盖才能生效。
本方法无副作用,不会修改该实例的任何成员变量。
本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。
本方法用于构造某个类型。
调用本方法无需实例,可直接使用类名进行调用。
本方法描述的是使用本类型作为左操作数的有效运算符。
这个值是由下列位标志构成位掩码的整数。
无返回值。