I'm trying to make a cooldown for an action for a game. How do I code it?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ripull125

I looked up multiple methods but they all give errors. This is my latest attempt:

func hide_cooldown():
	var hide_cd = SceneTree.create_timer(1.0).timeout

How would I make a function that makes a one second cooldown happen?

:bust_in_silhouette: Reply From: a_world_of_madness

SceneTree refers to the class. You would use it if you wanted to create a new object of type SceneTree. Replace it with get_tree(), which will give you the current scene tree. (Usually there is only one but Godot supports having multiple at the same time).

Next, the .timeout refers to the timeout signal of the timer you created. You need to actually connect it to something:

func start_timer():
	var timer = get_tree().create_timer(1.0)
	timer.timeout.connect(timeout_function)

func timeout_function():
	# This is called after timeout

Depending on what you want to do with the timer, you can also just delay for the duration of the timer until resuming from the next line with the awaitkeyword:

print("start")
await get_tree().create_timer(5.0).timeout
print("end") # Runs after waiting for 5 seconds

Although its not giving errors, the code isn’t pausing. When I try to toggle, it still toggles and untoggles at the speed of light.

Am I doing this right? I only used the code from the second piece of code u gave.

ripull125 | 2023-06-11 17:10

Although its not giving errors, the code isn’t pausing. When I try to toggle, it still toggles and untoggles at the speed of light.

ripull125 | 2023-06-11 17:10

My mistake, the second example was missing the signal to wait for:

await get_tree().create_timer(1.0).timeout

a_world_of_madness | 2023-06-11 18:26

wait was was your mistake? I don’t see any difference.

edit: nvm I see now

ripull125 | 2023-06-12 15:57