How can I implement a cooldown between events?

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

Hi folks,

I have a player node that has an attack (we’ll call it kick) which I need a cooldown for between triggering the attack and the next time I use it. So, if I kick, it should be 0.5 seconds until I can kick again.

Currently I am using a timer to achieve this. When I enter my kick state, the kick timer may only trigger is the timer has stopped:

states.kick:
	if parent.kick_cooldown.is_stopped():
		parent.kick_cooldown.start(0.5)

This actually works; I cannot enter the state until the timer hits zero. Unfortunately, it only works if I do not spam the kick button. If I do spam input, it is impossible to kick again. I have to allow the timer to spend some time having stopped. This is how that looks when I print the output:

time left: 0.05
time left: 0.05
time left: 0.033333
time left: 0.033333
time left: 0.016667
time left: 0.016667
time left: 0
time left: 0
time left: 0.483333
time left: 0.483333
time left: 0.466667
time left: 0.466667

Clearly, the timer stops, and thus I should be able to kick again, but this is not the case.

Any help here is much appreciated, and if I can provide any more information, then I will. Thanks.

:bust_in_silhouette: Reply From: deaton64

Hi,
I’d use a bool to say when your player can kick.

Do something like (obviously not GD script):

Set bool can_kick = true

if the states bit, check can_kick is true, if it is, kick and start the timer. Set can_kick to false.

In the timer timeout, set can_kick to true.

You can’t kick again until the timer times out.