0 votes

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.

in Engine by (17 points)

1 Answer

0 votes

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.

by (1,653 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.