How to call a method only every 1 second?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Irul
:warning: Old Version Published before Godot 3 was released.

How to call a method only every 1 second? Preferably using Timer node.

:bust_in_silhouette: Reply From: Zylann

From code:

var _timer = null


func _ready():
	_timer = Timer.new()
	add_child(_timer)

	_timer.connect("timeout", self, "_on_Timer_timeout")
	_timer.set_wait_time(1.0)
	_timer.set_one_shot(false) # Make sure it loops
	_timer.start()


func _on_Timer_timeout():
	print("Second!")

You can also do it all from the editor: create Timer child, set same properties, then open the Node tab, and connect the timeout signal to _on_Timer_timeout.

Hell… Why I didn’t think of it :D. Thanks man.

Irul | 2016-10-19 13:05

Hi!
In addition to this, how could the method be called every OS time second? Or maybe better: how could this timer_start be yielded until the next OS time second?

pferft | 2021-04-19 13:18