Image not available
How to create and connect a Timer in GDScript - Godot 4
Published: May 29, 2024
Last updated: June 4, 2024
- Code snippet
Here's how to create a timer along with different options in GDScript:
# creating a timer can be done in any function, not just _ready
func _ready() -> void:
var timer = Timer.new()
# the timer will last 2.5 seconds
timer.wait_time = 2.5
# if you want to start the timer automatically
timer.autostart = true
# or you can start it manually
timer.start()
# if you want to run the timer only once
timer.one_shot = true
# _on_timer_timeout is the function that will be called when the timer
# finishes
timer.timeout.connect(_on_timer_timeout)
# add the timer to the scene
add_child(timer)
func _on_timer_timeout() -> void:
print("Timer finished!")
About the author
Hey there 👋
I'm Blaubessen, a web dev learning Godot. This website is my way of documenting my learnings, and hopefully be useful to other folks as well.
If you find mistakes, disagree with something written, or just have general feedback please feel free to leave a comment on this form!