• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM How can I cancel an input once the stamina bar is emptied?

J

Jun Bird

Guest
I'm making a game where the player has to press a certain button in order to activate the main mechanic. He can hold it down until the stamina bar is completely empty. Keep also in mind that this bar automatically starts recharging itself as soon as the button is released or once the stamina reaches 0.
My problem is, if the stamina reaches 0 and the player is still pressing the button, this is what happens.



The lack of stamina prevents the main mechanic from being activated just for a really brief moment, because it instantly refills, so the main mechanic can be used again for an instant. immediately afterwards, the bar empties out again and so on, until the player releases the button
So, how can I cancel the input so that the player will have to press the button again when he wastes all the stamina at once?
 
V

VectorStudio

Guest
I don't know how exactly your system works but I would add a condition to check when the player can press a button. But without seeing the code I can't help you much.
 

JackTurbo

Member
Add an additional condition to your stamina regeneration code so it only kicks in if the button isn't pressed
 

TheouAegis

Member
If it starts recharging once the key is released, then technically it shouldn't be recharging. So doesn't sound like your code is what you say it is.
 
J

Jun Bird

Guest
Thank you all for replying!

I don't know how exactly your system works but I would add a condition to check when the player can press a button. But without seeing the code I can't help you much.
I think that the code I wrote for this thing is as simple as it can get.

Code:
if action == true
{
stamina -= 1;
}
else
{
stamina += 1;
}
There are also two other conditions that don't allow the variable to go further than 0 or 100, which are respectively the minimum and maximum values.

Code:
if stamina > 100 stamina = 100;
if stamina < 0 stamina = 0;
That's basically it. I'm not trying to build something too weird, I believe this kind of stuff is pretty basic, isn't it?

By the way, your suggestion sounds like something I already did.

if keyboard_check(vk_space) && stamina == 0
{
action = true;
}
But I'm not sure I get what you mean.

Add an additional condition to your stamina regeneration code so it only kicks in if the button isn't pressed
I already thought about solving this problem that way, but I'm not sure that the game would be very intuitive like this. If the player keeps holding the button when there is no stamina left, the stamina meter would not start recharging until he releases the button. I feel like this wouldn't be very user-friendly at first. I don't know. I could try though, but it's not really what I want to do.

If it starts recharging once the key is released, then technically it shouldn't be recharging. So doesn't sound like your code is what you say it is.
Yeah, technically it doesn't work like that, as you can tell from what I just wrote in this post. I just described it a bit badly I guess.
 
Last edited by a moderator:
V

VectorStudio

Guest
Thank you all for replying!


I think that the code I wrote for this thing is as simple as it can get.

Code:
if action == true
{
stamina -= 1;
}
else
{
stamina += 1;
}
There are also two other conditions that don't allow the variable to go further than 0 or 100, which are respectively the minimum and maximum values.

Code:
if stamina > 100 stamina = 100;
if stamina < 0 stamina = 0;
That's basically it. I'm not trying to build something too weird, I believe this kind of stuff is pretty basic, isn't it?

By the way, your suggestion sounds like something I already did.


But I'm not sure I get what you mean.



I already thought about solving this problem that way, but I'm not sure that the game would be very intuitive like this. If the player keeps holding the button when there is no stamina left, the stamina meter would not start recharging until he releases the button. I feel like this wouldn't be very user-friendly at first. I don't know. I could try though, but it's not really what I want to do.


Yeah, technically it doesn't work like that, as you can tell from what I just wrote in this post. I just described it a bit badly I guess.
I think your condition is wrong and you forgot to set the action variable back to false:
Code:
if keyboard_check(vk_space) && stamina == 0
{
action = true;
}
Try to change it:
Code:
if keyboard_check(vk_space) && stamina != 0
{
action = true;
}
else
action = false;
By the way it's better to control maximum and minimum by the clamp function:
Code:
stamina = clamp(stamina, 0, 100);
 
Last edited by a moderator:

JackTurbo

Member
If the player never needs to release a button, then what is the point of making them hold it to start with?
 
J

Jun Bird

Guest
I think your condition is wrong and you forgot to set the action variable back to false:
Code:
if keyboard_check(vk_space) && stamina == 0
{
action = true;
}
Try to change it:
Code:
if keyboard_check(vk_space) && stamina != 0
{
action = true;
}
else
action = false;
By the way it's better to control maximum and minimum by the clamp function:
Code:
stamina = clamp(stamina, 0, 100);
That clamp function is pretty useful, thank you. But the code still doesn't work how it should. With the "!=" it seems to work exactly how it did with the "==".
By the way, just to be clear, there was already an else condition that turned off the "action" variable. I forgot to paste that part.
If the player never needs to release a button, then what is the point of making them hold it to start with?
He has to release the button because using that mechanic is not always useful or necessary. I just don't want the player to keep the button pressed while that main mechanic nor the auto-recharging stamina aren't working. It doesn't feel fair to me.
 
V

VectorStudio

Guest
That clamp function is pretty useful, thank you. But the code still doesn't work how it should. With the "!=" it seems to work exactly how it did with the "==".
By the way, just to be clear, there was already an else condition that turned off the "action" variable. I forgot to paste that part.

He has to release the button because using that mechanic is not always useful or necessary. I just don't want the player to keep the button pressed while that main mechanic nor the auto-recharging stamina aren't working. It doesn't feel fair to me.
Remove the action variable.
Code:
if (keyboard_check(vk_space))
{
    stamina -= 1;
}
else
{
    stamina += 1;
}

stamina = clamp(stamina, 0, 100);
 

TheouAegis

Member
First you say you want the player to have to release the button to recharge, then you say you don't want the player to have to release the button to recharge....

Code:
if keyboard_check(vk_space) {
    if !action action = 1;
    else if !stamina action == 2;
}
else action = 0;
if !(action & 1) stamina++;
else stamina--;
stamina = clamp(stamina,0,100)
So instead of treating the action variable as a boolean, treat it as a simple three-stage mechanic. When action is 0, it is 1 when the key is held down. But if the stamina reaches 0 while the key is held down, it goes to 2 so the game knows to recharge stamina but not let the player do any more actions until the key is released, at which point it goes back to 0.
 
J

Jun Bird

Guest
Remove the action variable.
Code:
if (keyboard_check(vk_space))
{
    stamina -= 1;
}
else
{
    stamina += 1;
}

stamina = clamp(stamina, 0, 100);
But I really need that variable, I can't do that.

By the way, I think I actually solved my problem.
While casually checking https://docs.yoyogames.com/ I found out about these functions:

https://docs.yoyogames.com/source/dadiospice/002_reference/mouse, keyboard and other controls/mouse input/mouse_clear.html
https://docs.yoyogames.com/source/dadiospice/002_reference/mouse, keyboard and other controls/mouse input/mouse_clear.html


So, I tried this:

Code:
if stamina == 0 keyboard_clear(vk_space);
And it looks like that now it works exactly how I intended it to. Is there something wrong with this? In case this code is actually good to go, I'm sorry I had to post a new thread for something that wasn't really that difficult. I swear that yesterday I spent the entire afternoon looking for a way to do this without finding anything useful.

First you say you want the player to have to release the button to recharge, then you say you don't want the player to have to release the button to recharge....

Code:
if keyboard_check(vk_space) {
    if !action action = 1;
    else if !stamina action == 2;
}
else action = 0;
if !(action & 1) stamina++;
else stamina--;
stamina = clamp(stamina,0,100)
So instead of treating the action variable as a boolean, treat it as a simple three-stage mechanic. When action is 0, it is 1 when the key is held down. But if the stamina reaches 0 while the key is held down, it goes to 2 so the game knows to recharge stamina but not let the player do any more actions until the key is released, at which point it goes back to 0.
This looks pretty complex though. Is there any benefit for using this method instead of the really simple one I just described?
 
Last edited by a moderator:
V

VectorStudio

Guest
But I really need that variable, I can't do this.

By the way, I think I actually solved my problem.
While casually checking https://docs.yoyogames.com/ I found out about these functions:

https://docs.yoyogames.com/source/dadiospice/002_reference/mouse, keyboard and other controls/mouse input/mouse_clear.html
https://docs.yoyogames.com/source/dadiospice/002_reference/mouse, keyboard and other controls/mouse input/mouse_clear.html


So, I tried this:

Code:
if stamina = 0 keyboard_clear(vk_space);
And it looks like that now it works exactly how I intended it to. Is there something wrong with this? In case this code is actually good to go, I'm sorry I had to open a thread for something that wasn't really that difficult. I swear that yesterday I spent the entire afternoon looking for a way to do this without finding anything useful.


This looks pretty complex though. Is there any benefit for using this method instead of the really simple one I just described?
Basically it is doing this:
1. Once the stamina is equal to 0 it will start increasing
2. But when the player still holds spacebar in the next step it will also start decreasing
3. This is repeated
4. So the result is 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, ...

Your solution seems nice
 
R

Rukiri

Guest
Code:
var input = true;
var stam = 100;

if (stam <= 0) input = false; stam = 0;
if (input) {
    if (keyboard_check(vk_space)) {
        stam = stam - 1;
    }
}
something like this should work.
 

Yal

šŸ§ *penguin noises*
GMC Elder
The problem might also be that the player can use the main mechanic no matter how little stamina you have as long as it's above 0, but its cost is pretty substantial, so having it jitter around 1 lets you use the attack(?) repeatedly at a lower cost than normal. Dark Souls get around this kind of abuse by allowing stamina to go into the negatives, so you have to wait equally long for it to recharge no matter if you had 100 or 1 stamina when using the attack... only that if you ended up in the negatives, you have <= 0 stamina, so attacks and stuff won't start if you mash the button; i.e. you're FORCED to wait for it to recharge now. (negative stamina isn't indicated in any way, but it's there)
 
J

Jun Bird

Guest
Your solution seems nice
Well, I think my problem is solved, then. Thank you to everyone who replied to this thread for helping out :)

The problem might also be that the player can use the main mechanic no matter how little stamina you have as long as it's above 0, but its cost is pretty substantial, so having it jitter around 1 lets you use the attack(?) repeatedly at a lower cost than normal. Dark Souls get around this kind of abuse by allowing stamina to go into the negatives, so you have to wait equally long for it to recharge no matter if you had 100 or 1 stamina when using the attack... only that if you ended up in the negatives, you have <= 0 stamina, so attacks and stuff won't start if you mash the button; i.e. you're FORCED to wait for it to recharge now. (negative stamina isn't indicated in any way, but it's there)
I actually did something like that prior to posting this thread, but I don't think it works fine, because the type of game I'm building doesn't work like a Souls-like game at all. I think having that mechanic available to the player even when the stamina meter empties out is better suited for the type of game I'm making. Maybe I'll change this in the future though, I'm still pretty much prototyping.
 
Top