Variable isnt properly checked in if statement

33 Views Asked by At

enter image description here

Somehow I can spam "Q" to have the if statement run infinetly as if the cooldown never gets checked for or never Updated

I wanted to have like a cooldown timer of 60 Frames and only have this Sequences playable when the cooldown is off. But smh I can Spam it as often as I want

1

There are 1 best solutions below

4
Steven On

You should define the meele_cooldown = 60 (melee?) in the Create Event, rather than inside the function itself. Because as you mention it, if you repeately press 'Q', it'll get redefined.

You should then set the meele_cooldown back to 60 after when it hits 0.

Edit:

With the added context from the comment, I've expanded my answer a bit further:

You should put the countdown condition if (attack_cooldown >= 1) and place it outside your Keyboard_Check_pressed(), because you want that to cooldown regardless if you've pressed or not. You should also improve your condition to make sure it's 0 if it's done counting down. As the variables are decimals.

So the end result may look like this:

if (attack_cooldown >= 0)
{
    attack_cooldown -= 1;
}
else 
{
    attack_cooldown = 0;
}

if (keyboard_check_pressed() ... && attack_cooldown == 0)
{
    attack_cooldown = 60;
    //create sequence object
}

As a sidenote: I do notice that you're using a sequence now, I'm not familiar with that myself, but I assume you want that to play during the cooldown. I think that fits better if it's in it's own Step Event rather than the event of an other object. Because a temporary variable will lose it's value after the Step Event is done.