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

Eredita: Node < Object

Un timer per il conto alla rovescia.

Descrizione

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.

Tutorial

Proprietà

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

Metodi

bool

is_stopped() const

void

start(time_sec: float = -1)

void

stop()


Segnali

timeout() 🔗

Emesso quando il timer raggiunge la fine.


Enumerazioni

enum TimerProcessCallback: 🔗

TimerProcessCallback TIMER_PROCESS_PHYSICS = 0

Aggiorna il timer a ogni frame di processo di fisica (vedi Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS).

TimerProcessCallback TIMER_PROCESS_IDLE = 1

Aggiorna il timer a ogni frame di processo (renderizzato) (vedi Node.NOTIFICATION_INTERNAL_PROCESS).


Descrizioni delle proprietà

bool autostart = false 🔗

  • void set_autostart(value: bool)

  • bool has_autostart()

Se true, il timer si avvierà immediatamente quando entra nell'albero di scene.

Nota: Dopo che il timer entra nell'albero, questa proprietà viene automaticamente impostata su false.

Nota: Questa proprietà non fa nulla quando il timer è in esecuzione nell'editor.


bool ignore_time_scale = false 🔗

  • void set_ignore_time_scale(value: bool)

  • bool is_ignoring_time_scale()

Se true, il timer ignorerà Engine.time_scale e si aggiornerà con il tempo realmente trascorso.


bool one_shot = false 🔗

  • void set_one_shot(value: bool)

  • bool is_one_shot()

Se true, il timer si fermerà dopo aver raggiunto la fine. Altrimenti, come impostazione predefinita, il timer si riavvierà automaticamente.


bool paused 🔗

  • void set_paused(value: bool)

  • bool is_paused()

Se true, il timer è in pausa. Un timer in pausa non elabora finché questa proprietà non viene reimpostata su false, anche quando viene chiamato start(). Vedi anche stop().


TimerProcessCallback process_callback = 1 🔗

Specifica quando il timer è aggiornato durante il ciclo principale.


float time_left 🔗

Il tempo rimanente del timer in secondi. È sempre 0 se il timer è fermo.

Nota: Questa proprietà è di sola lettura e non può essere modificata. Si basa su wait_time.


float wait_time = 1.0 🔗

  • void set_wait_time(value: float)

  • float get_wait_time()

Il tempo richiesto per allo scadere del timer, in secondi. Questa proprietà può anche essere impostata ogni volta che viene chiamato start().

Nota: I timer possono elaborare solo una volta per frame di fisica o di processo (a seconda del process_callback). Un framerate instabile può causare la fine incoerente del timer, il che è particolarmente evidente se il tempo di attesa è inferiore a circa 0.05 secondi. Per timer molto brevi, si consiglia di scrivere il proprio codice invece di utilizzare un nodo Timer. I timer sono inoltre influenzati da Engine.time_scale.


Descrizioni dei metodi

bool is_stopped() const 🔗

Restituisce true se il timer è fermato o non è iniziato.


void start(time_sec: float = -1) 🔗

Avvia il timer, o reimposta il timer se è già stato avviato. Fallisce se il timer non è all'interno dell'albero di scene. Se time_sec è maggiore di 0, questo valore viene usato per wait_time.

Nota: Questo metodo non riprende un timer in pausa. Vedi paused.


void stop() 🔗

Arresta il timer. Vedi anche paused. A differenza di start(), è possibile chiamare questa funzione in sicurezza anche se il timer non si trova nell'albero di scene.

Nota: Chiamare stop() non emette il segnale timeout, poiché il timer non è considerato scaduto. Se ciò si desidera, utilizzare $Timer.timeout.emit() dopo aver chiamato stop() per emettere manualmente il segnale.