
Use a timer and signals
In this tutorial we will see how to use timer in godot using the node Timer. But we also need to learn how to use the signals.
- Godot Engine
The first thing to see before starting the project are signals, signals are used to communicate between node.
In Godot, signals are a way to send notifications that something has happened. They are similar to events or callbacks in other programming languages. Signals allow nodes to communicate with each other without needing to reference each other directly — this keeps your code clean, modular, and decoupled.
What is a Signal?
A signal is a message emitted by an object when something happens — like a button being pressed, a character finishing an animation, or a timer reaching zero.
You can:
- Emit a signal (broadcast it).
- Connect to a signal (listen to it and respond).
Built-in Signal Example (Button)
Godot nodes often have built-in signals. For example, a Button has a signal called “pressed”.
# Assume this is attached to a scene with a Button node named "MyButton"
func _ready():
$MyButton.connect("pressed", self, "_on_MyButton_pressed")
func _on_MyButton_pressed():
print("The button was pressed!")In this example we will use the signal timeout to check if the timer has finished.
If you want to learn more about signals you can go to the official documentation about signals.
The first thing to do is to create an empty godot project and then open it.
To create a timer with godot you can simply add it in the inspector:
In Godot, a Timer is a node (Timer) that lets you trigger actions after a certain amount of time has passed, or repeat actions at regular intervals.
Timers are useful for:
- Delaying events
- Creating cooldowns (e.g. weapon cooldown)
- Repeating actions (e.g. spawning enemies every 5 seconds)
- Animating over time
Basic Properties of a Timer
| Property | Description |
|---|---|
| wait_time | Duration (in seconds) before the timer times out |
| one_shot | If true, it runs once and stops |
| autostart | If true, it starts automatically when the scene runs |
| loop | If true, it repeats after each timeout |
Create a timer with code using GDScript
var timer = get_tree().create_timer(1.5) # Create a timer
await timer.timeout # waits for 1,5 second
# Do something afterwardsIf you want to delete the node itself you can use queue_free() at the end.
Create a timer with code using C#
var timer = GetTree().CreateTimer(1.5f); // Create the timer and set it to 1,5s
await ToSignal(timer, "timeout"); // Wait foor the timer to finish
// Do something afterwardsIf you want to delete the node itself you can use QueueFree() at the end.
Create a timer with a callback function
timer = new Timer();
AddChild(timer);
timer.WaitTime = 1.5f; // Waiting time
timer.OneShot = true; // If the timer is executed once
timer.Timeout += timeoutMonsterAttack;
timer.Start();In this example the function timeoutMonsterAttack is a callback function called after the timer ends.