Timer in Godot – Complete Guide

Timers are fundamental components in game development—and mastering their use is key for any aspiring game creator. With the ability to handle intervals, create delays, and synchronize events, timers empower you to bring dynamic interactions to life in your projects. Whether you’re creating a countdown to start a race, delaying a character’s power-up activation, or scheduling waves of enemies, understanding the Timer class in Godot 4 will unlock a new level of control and sophistication in your game design. So let’s dive into the world of timers and discover how this powerful feature can enhance your game development repertoire!

What is a Timer in Godot 4?

A Timer in Godot 4 is simply a node that counts down from a specified interval to zero. When it reaches zero, it emits a signal, which you can then use to trigger any kind of event or action in your game.

What is it Used For?

Timers are incredibly versatile and are used for a variety of functionalities in a game, such as:

  • Creating countdowns before a level starts or an event happens.
  • Delaying actions, like triggering traps or spawning enemies.
  • Measuring time-limited challenges or abilities.
  • Coordinating actions between different game elements.

Why Should I Learn to Use Timers?

Learning to use timers is essential because:

  • They add depth to game mechanics, enhancing player experience.
  • They provide an easy way to add delays and synchronization without complex coding.
  • Understanding timers is foundational for creating well-timed and responsive game behaviors.

By grasping the Timer class functionality, you’ll be taking a significant step towards creating more interactive and engaging games in Godot 4. Let’s proceed and write some code!

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Developer API

The API for Godot version 4.2 is displayed below. For the latest version please visit the official documentation.

Project Example

You can download the project here.

The timer’s behavior primarily revolves around two properties, wait_time and time_left.

  • wait_time is the value at which the timer starts counting down from. By default, when the timer runs out, it will get reset to this value and continue to count down again.
  • time_left is the remaining time in seconds before the timer times out. Note that you should not set this property, you can however read it’s value.

Core idea

When the timer is started, the timer starts counting down from the specified wait_time. When time_left reaches 0, the timer’s timeout signal is emitted. We can connect to this signal to run logic in our code whenever the timer times out.

To put that into practice, you need to create a Timer node. You can do this by clicking on the ‘+’ button in the ‘Scene’ dock or using the shortcut Ctrl + A with the ‘Scene’ dock focused. This will open the ‘Create New Node’ window, where you can search for ‘timer’.

For the purpose of this tutorial, we will be creating a script called timer_example.gd on the root node of the scene.

1 e1716768986818

Connecting to the timeout signal

Lets create a new method called _on_timer_timeout() and print a message. This will be the handler method for our timeout signal.

func _on_timer_timeout():
  print("Timer has timed out!")

At the moment, this is just a normal method. We will need to connect it to the timer node’s timeout signal.

We will need to:

  • Store a reference to the timer node.
    • We do this by creating an @onready variable and assigning a reference to the timer node using it’s NodePath
  • Connect to it’s timeout signal inside the _ready() method.
    • In this example, we will connect to the signal through code. You can do it through the editor instead, if you’d like.
    • We do this by accessing the stored timer reference and calling connect() on it’s signal.
@onready var my_timer = $Timer

func _ready():
  my_timer.timeout.connect(_on_timer_timeout)

We pass in the name of the method we created earlier as a Callable.

Note that you do NOT invoke the method like this my_timer.timeout.connect(_on_timer_timeout).
We only want to pass a reference to the callable like this: my_timer.timeout.connect(_on_timer_timeout) (Without using the ())

Setting the timer’s duration

As discussed earlier, the timer’s wait_time property determines the amount of time before it times out. You can either set this property in the inspector, or through code like this:

  • Access the timer reference.
  • Set it’s wait_time property.
func _ready():
  my_timer.timeout.connect(_on_timer_timeout)
  
  my_timer.wait_time = 3.0

Starting the timer

The timer needs to be started for it to run. You can optionally set the autostart property in the inspector to true, but we will be doing it manually in this example.

We can do this by calling the Timer.start() method.

  • After connecting to the timeout signal,
    • Access the timer reference.
    • Call it’s start() method.
func _ready():
  my_timer.timeout.connect(_on_timer_timeout)
  
  my_timer.wait_time = 3.0
  my_timer.start()

When we run this scene, we can see the message being printed to the output every 3 seconds.

If we want it to only run once, we could either set the one_shot property to true in the inspector. This will make the timer stop automatically after timing out once, it can be started again by calling start(). Alternatively, we can stop the timer manually through code.

To stop the timer manually after it has timed out once, we can call the Timer.stop() method.

func _on_timer_timeout():
  print("Timer has timed out!")
  my_timer.stop()

This will stop the timer from running, and the timer is reset. If start() is called at a later point, the timer will NOT start from where it left off, it will start from wait_time again.

Note: If you want to temporarily pause the timer instead, use the timer’s paused property: my_timer.paused = true
Calling start() will NOT resume the timer. The timer will be paused until paused is set to false again.

Visualizing the timer

To understand how the timer ticks down, we can print out the timer’s time_left.
Let us print only when time_left is greater than 0, so that we print this message only while the timer is running.

func _process(delta: float):
  print(my_timer.time_left)

When we run this scene, we can now see the timer tick down in the span of 3 seconds, before printing the timeout message and stopping.

 

2

 

Full Script Code

You can download the Godot project here.

extends Node2D

@onready var my_timer = $Timer

func _ready():
  my_timer.timeout.connect(_on_timer_timeout)
  
  my_timer.wait_time = 3.0
  my_timer.start()

func _process(delta: float):
  if my_timer.time_left > 0:
    print(my_timer.time_left)

func _on_timer_timeout():
  print("Timer has timed out!")
  my_timer.stop()

Conclusion

As we wrap up this exploration of the Timer class in Godot 4, it’s clear that mastering this tool can significantly enhance the dynamics and polish of your game. Harnessing the power of time through code isn’t just a technical skill, but an art form that breathes life into your gaming worlds. At Zenva, our mission is to empower you with the knowledge and skills to transform your visions into reality. Whether it’s timing the perfect jump, pacing the enemy spawns, or adding tension with a countdown, your newfound proficiency with timers is a game-changing asset.

We invite you to continue honing your game development prowess with our Godot Game Development Mini-Degree and turn those timer-based mechanics into your own signature gameplay experiences. Keep creating, keep learning, and remember that with every line of code, you’re shaping the future of interactive entertainment. Let’s build incredible games together!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.