Skip to reading
GDScript Programming & Godot

39. Processing and a frame-based countdown

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
var countdown: float = 0.0
var timer_running: bool = false
Code example
func run() -> void:
    countdown = 10.0
    timer_running = true
Code example
func _process(delta: float) -> void:
    if not timer_running:
        return
    countdown = maxf(0.0, countdown - delta)
    if countdown <= 0.0:
        timer_running = false
        Log("Countdown finished.")

Keep the instance alive. delta is elapsed seconds for this frame, so the countdown follows time rather than frame count. Use _physics_process for fixed-step physics work, not simply because a script changes a number.

A process callback should return quickly. Do not start an awaited long routine every frame; it can create many overlapping routines. Set a state flag before starting a job, or use a dedicated Timer/signal.

PCA can pause/suspend preview workspaces and runtime processing. Time scale and pause settings matter. A global checkbox keeps the instance; it does not force every callback to run while its tree/parent is disabled.

Page 48 of 378Permalink
Go to page