• 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!

If NOT pressing 2 keys simultaneously, do this

D

Dannyinformal

Guest
Hey lads,

So I'm making a small Metroidvania game for kicks and laughs. I made a boost/sprint key modifier (shift) which bugs out. I was wondering if you guys could help me out. This is what happens:

1. You start with 20 boost power
2. When you press right + boost you dash and the boost power gradually decreases
3. Sub 20, it slowly regenerates

In point 3 lies the problem; the boost power regenerates, but stops regenerating the moment i press right, een without pressing the modifier. This is my code:

Code:
if obj_gegevens.boost > 0 && keyboard_check (vk_right) && keyboard_check (vk_lshift) //when you press shift and right DASH
{
x += 20;
image_speed = 1;
obj_gegevens.boost -=1;
}

if obj_gegevens.boost < 20 && !keyboard_check (vk_right) && !keyboard_check (vk_lshift) //when you DON'T press shift and right, regenerate
{
obj_gegevens.boost +=0.2;
}
Thanks for your help lads!
 

Simon Gust

Member
Try this
Code:
var boosting = false;
if (keyboard_check (vk_lshift))
{
    var side = keyboard_check(vk_right) - keyboard_check(vk_left);
    if (side != 0)
    {
        boosting = true;
        if (obj_gegevens.boost > 0)
        {
            x += 20 * side;
            image_speed = 1;
            obj_gegevens.boost = max(obj_gegevens.boost - 1.0, 0);
        }
    }
}

if (!boosting)
{
    obj_gegevens.boost = min(obj_gegevens.boost + 0.2, 20);
}
 
D

Dannyinformal

Guest
Hey Simon,

Thank you for your quick reply!

The code works but I have to admit I have no idea what's actually happening in it. Would you mind adding annotations explaining the syntax per sentence?

Especially this:
Code:
var side = keyboard_check(vk_right) - keyboard_check(vk_left);
Also, is there a particular reason you write "var" in front of a new variable? It also works without, what's the difference?

What's happening there, exactly? You create a variable "side", right and you assign vk_right minus vk_left to it? o_O

I feel kinda stupid using a piece of code without actualy understanding what's written.

Thanks again for all your help and your patience!
 
Last edited by a moderator:

Simon Gust

Member
Sure.

First though, to why your original code didn't work.
Code:
if obj_gegevens.boost < 20 && !keyboard_check (vk_right) && !keyboard_check (vk_lshift)
This statement requires that the right-key isn't pressed under any circumstances even if you're not pressing shift.
But you only want it to not rgenerate when both keys are pressed together. You need to bundle the statements ->
Code:
if obj_gegevens.boost < 20 && (!keyboard_check (vk_right) && !keyboard_check (vk_lshift))
Now to my code.
First, I set a variable "boosting" to false. (This is my set-unset statement)
It controls the regeneration of the boost. Boost can only regenerate when you're not "boosting".

Then, I first check for the "enable boost" key.
Then I calculate the variable "side". Side is a smart variable.
It turns 1 if you only press the right key.
It turns -1 if you only press the left key.
It turns 0 if you either press no key or both keys at the same time.

This works because keyboard_check() returns either 0 or 1 (false or true).
pressing right but not left -> side = (1) - (0)
pressing left but not right -> side = (0) - (1)
pressing both -> side = (1) - (1)
pressing neither -> side = (0) - (0)

Then, I check if side is not 0 before checking if you have boost at all.
The reason for this is so that boost doesn't regenerate while trying to boost.

And finally I increase x by 20 by whichever side you're going. (another use of the side variable),

To change the boost I use the min() and max().
to set a maximum to a variable, use min().
Code:
A = min(A + 1, maxium_of_A);
to set a minimum to a variable, use max().
Code:
A = max(A - 1, minimum_of_A);
 
D

Dannyinformal

Guest
Sure.

First though, to why your original code didn't work.
Code:
if obj_gegevens.boost < 20 && !keyboard_check (vk_right) && !keyboard_check (vk_lshift)
This statement requires that the right-key isn't pressed under any circumstances even if you're not pressing shift.
But you only want it to not rgenerate when both keys are pressed together. You need to bundle the statements ->
Code:
if obj_gegevens.boost < 20 && (!keyboard_check (vk_right) && !keyboard_check (vk_lshift))
Now to my code.
First, I set a variable "boosting" to false. (This is my set-unset statement)
It controls the regeneration of the boost. Boost can only regenerate when you're not "boosting".

Then, I first check for the "enable boost" key.
Then I calculate the variable "side". Side is a smart variable.
It turns 1 if you only press the right key.
It turns -1 if you only press the left key.
It turns 0 if you either press no key or both keys at the same time.

This works because keyboard_check() returns either 0 or 1 (false or true).
pressing right but not left -> side = (1) - (0)
pressing left but not right -> side = (0) - (1)
pressing both -> side = (1) - (1)
pressing neither -> side = (0) - (0)

Then, I check if side is not 0 before checking if you have boost at all.
The reason for this is so that boost doesn't regenerate while trying to boost.

And finally I increase x by 20 by whichever side you're going. (another use of the side variable),

To change the boost I use the min() and max().
to set a maximum to a variable, use min().
Code:
A = min(A + 1, maxium_of_A);
to set a minimum to a variable, use max().
Code:
A = max(A - 1, minimum_of_A);
Super clear Simon! Thank's for taking the time to explain this in detail, you're the best!
 

TheouAegis

Member
C && (!A && !B) is the exact same thing as C && !A && !B. What you need to do is move the !'s outside the parentheses.

if !( keyboard_check(vk_right) && keyboard_check(vk_lshift) )
 

Simon Gust

Member
C && (!A && !B) is the exact same thing as C && !A && !B. What you need to do is move the !'s outside the parentheses.

if !( keyboard_check(vk_right) && keyboard_check(vk_lshift) )
right, thanks.

EDIT:
Good thing that I don't let myself do these kind of statements or I would have bugs all over my code.
 
Top