Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 36 ยท Nodes and Signals

Godot Autoloads, Timers, Tweens and Scene Changes

Use Godot autoload singletons for global state, Timer nodes and create_timer for delays, Tween for smooth animation, and change_scene_to_file for menus and levels, with examples.

Four small tools show up in almost every Godot game. Autoloads keep data that must survive scene changes, such as score or settings. Timers run code after a delay or at an interval. Tweens animate any property smoothly from one value to another. And scene changes move the player from the menu to a level and back. This lesson shows each one and how they combine.

Godot only runs in the editor; GDScript is read-only here. The Python stand-ins model a countdown timer and the easing curves tweens use, so you can see the numbers.

Autoloads (singletons)

An autoload is a script or scene that Godot loads at startup and keeps alive for the whole game. It becomes a child of /root, next to your current scene, and can be reached from any script by its global name.

To create one: Project > Project Settings > Globals (the Autoload tab), pick res://autoload/game_state.gd, give it the name GameState and press Add. Then:

GDSCRIPT
# autoload/game_state.gd
extends Node

signal score_changed(new_score: int)

var score := 0:
    set(value):
        score = value
        score_changed.emit(score)

var settings := {"music_volume": 0.8}

func reset() -> void:
    score = 0
GDSCRIPT
# any other script
func _on_coin_collected() -> void:
    GameState.score += 10

Because autoloads sit outside the current scene, they survive change_scene_to_file. Good uses:

  • Game state that crosses scenes (score, current level, unlocked items).
  • A save/load manager, an audio manager, a scene-transition manager.
  • A small event bus of signals (Node Communication and Groups).

Bad uses: putting everything global. An autoload makes testing and reuse harder, since any script can reach it. Prefer passing data to nodes, and keep autoloads small and focused. Because autoloads are always loaded and the scene run with F6 does not reset them in the editor, remember to reinitialize their state when starting a new game.

An autoload can also be a scene (for example a pause menu or fade overlay) if it needs child nodes.

Timers

The Timer node

Add a Timer node as a child. In the Inspector set Wait Time (seconds), One Shot (off means it repeats) and Autostart. It emits timeout:

GDSCRIPT
@onready var spawn_timer: Timer = $SpawnTimer

func _ready() -> void:
    spawn_timer.timeout.connect(_spawn_enemy)
    spawn_timer.wait_time = 1.5
    spawn_timer.start()

func _spawn_enemy() -> void:
    print("spawn!")

func stop_spawning() -> void:
    spawn_timer.stop()

start(), stop(), paused, time_left, is_stopped() are the main members. Timers follow the pause state and the process mode.

One-off delays with create_timer

When you need "wait, then continue" once, skip the node:

GDSCRIPT
func hit_flash() -> void:
    sprite.modulate = Color.RED
    await get_tree().create_timer(0.15).timeout
    sprite.modulate = Color.WHITE

create_timer makes a temporary timer that frees itself. The game keeps running during the await. If the node is freed meanwhile, the function simply never resumes. Use a Timer node when you need to stop or restart it; use create_timer for fire-and-forget delays.

A common beginner approach is a manual countdown in _process: cooldown -= delta. It is perfectly good for a weapon cooldown you check each frame.

Tweens

A Tween changes a property from its current value to a target over time, with an easing curve. Create one from code with create_tween():

GDSCRIPT
func pop_in() -> void:
    scale = Vector2.ZERO
    var tween := create_tween()
    tween.tween_property(self, "scale", Vector2.ONE, 0.3) \
        .set_trans(Tween.TRANS_BACK).set_ease(Tween.EASE_OUT)

func fade_and_free() -> void:
    var tween := create_tween()
    tween.tween_property($Sprite2D, "modulate:a", 0.0, 0.5)   # fade alpha to 0
    tween.tween_callback(queue_free)                           # then delete

By default a tween's steps run one after another (sequentially). Use set_parallel(true) for simultaneous steps:

GDSCRIPT
var t := create_tween().set_parallel(true)
t.tween_property(self, "position:y", position.y - 40, 0.4)
t.tween_property(self, "modulate:a", 0.0, 0.4)

Other tween features: tween_interval(seconds) (a pause), set_loops(3) (0 loops forever), chain() to go back to sequential, and await tween.finished. The property name is a string, with a sub-path such as "position:x". Kill a running tween with tween.kill() before starting a competing one on the same property, otherwise they fight.

Easing shapes the movement. Linear is mechanical; TRANS_SINE or TRANS_QUAD with EASE_OUT starts fast and slows down (natural for things arriving), EASE_IN the opposite, and TRANS_BACK, TRANS_ELASTIC, TRANS_BOUNCE add overshoot. UI polish is mostly small tweens: buttons that pop, text that fades, health bars that slide.

Changing scenes

GDSCRIPT
func _on_start_pressed() -> void:
    get_tree().change_scene_to_file("res://levels/level_1.tscn")

func _on_quit_pressed() -> void:
    get_tree().quit()

func restart() -> void:
    get_tree().reload_current_scene()

change_scene_to_file frees the current scene at the end of the frame and loads the new one; autoloads survive. To use a preloaded scene, call change_scene_to_packed(PACKED). Pass data to the next scene through an autoload (GameState.next_level = 3), or instantiate the scene yourself and set its properties before adding it. A fade transition is a tween on a full-screen ColorRect in an autoload: fade to black, change scene, fade back.

Stand-in: a countdown and an easing curve

This is not Godot. It simulates a repeating Timer driven by delta, and prints a tween's value over time for a linear and an ease-out curve.

Python Stand-in (Python): timer ticks and tween easing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The timer spawns 3 enemies in 5 seconds (at 1.5, 3.0 and 4.5). The ease-out column reaches 64, 84 and 96 early while linear moves evenly: it arrives fast, then settles.

Common mistakes

  • Putting too much into autoloads, or forgetting that their state persists between runs of a new game.
  • Using while loops or OS.delay_msec to wait. Use timers and await.
  • Starting a second tween on the same property without killing the first.
  • Writing the tween property as a node path instead of a string: tween_property($Sprite2D, "modulate:a", ...) takes the node as an object and the property as a string.
  • Changing scenes from inside a physics callback without deferring: use call_deferred("_go") if you see errors.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does an autoload keep its data when you change scenes?

    Autoloads are children of /root, siblings of the current scene, and survive change_scene_to_file.
  2. 2. Which is the right way to wait 0.2 seconds without blocking the game?

    await on the timer's timeout signal suspends only this function.
  3. 3. By default, how do multiple tween_property steps on one tween run?

    Steps are sequential unless you call set_parallel(true).
  4. 4. How do you reload the current level?

    SceneTree.reload_current_scene reloads the running scene.

Summary

  • Autoloads are always-loaded global nodes for cross-scene state, managers and event buses; keep them small.
  • Timer nodes (or await get_tree().create_timer(x).timeout) handle delays and intervals.
  • create_tween() animates properties with easing; steps are sequential by default, parallel on request; kill conflicting tweens.
  • change_scene_to_file, reload_current_scene and quit control the flow between menus and levels.
  • Chapters ahead build real 2D and 3D games on these foundations.

Next: Sprites and TileMaps in 2D