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

GameMaker *Title Change* Character not dashing when in the air

demonipo

Member
So I am trying to make a variable to save me time when i call the letter 'Z'

Code:
var zkey = keyboard_check(ord('z'))
From my current knowledge, i know for ord() to work it needs to be in a argument which it is not in the code I wrote. However, writing if keyboard_check(ord('Z')) = true { do something }, despite working, would make the code tiring to read.

Is there another way to assign letters to variables or do I have to write the if statement everytime i need to call a letter?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Your trouble is in that ord("Z") is the valid keycode for Z, while ord("z") isn't - function keys, numpad keys, and other things take up that index space for keyboard_check.
 

Binsk

Member
Edit: May have misunderstood the question but I'll leave my answer up anyway.

All variables are either strings (aka text) or numbers.

ord('Z') essentially converts a string that you understand as a person (the letter Z) into a number that the computer understands (a key code).

This means that you can assign a variable to ord('Z') just fine and it will contain the computer's version of that key.

Then pass the variable into the keyboard check as normal. I'd writeup example but it's a pain to do on the phone.
 

demonipo

Member
So for a letter to work in ord() it needs to be in upper-case? That's strange for me but i'll guess I just need to adapt.
And what about using ' ' instead of " " ? Could it also make the function not work?
 

Tsa05

Member
You can, but I think it's not actually what you're trying to do.
Code:
var zkey = keyboard_check(ord("Z"))
That would totally set zkey equal to the current key state of the z-key on your keyboard (note that z has to be capital, and single-quotes are no longer valid in GMS2).

But zkey isn't going to change values by itself.

For that, you need a macro.
http://docs2.yoyogames.com/source/_build/3_scripting/3_gml_overview/6_scope.html
Code:
#macro zkey keyboard_check(ord("Z"))
Now, when you check zkey, it'll do the check for the keyboard state each time.
 

demonipo

Member
So i tested the #macro zkey and is doing what i wanted to, since the old one was only doing if i held the z key, so that part is done. Thanks.

So now I am having a problem with the code where i am using the z key. I want that, when Z is pressed, for the character to do a dash while in the air. With my current code it is doing the opposite.

Code:
if (zkey) {
if ground = 0 && hspd >= 0
vspd = 0
hspd = 20
}
(hspd is horizontal speed and vspd is the vertical speed)

the ground variable is 1 when on the ground and 0 while on the air. But this code is only working when the player is on the ground. I am certain I am doing this wrong but I don't know which part is wrong: is it the ground variable? is it where it becomes 0 and 1? is it the code on its entirety?
 

Cameron

Member
So i tested the #macro zkey and is doing what i wanted to, since the old one was only doing if i held the z key, so that part is done. Thanks.

So now I am having a problem with the code where i am using the z key. I want that, when Z is pressed, for the character to do a dash while in the air. With my current code it is doing the opposite.

Code:
if (zkey) {
if ground = 0 && hspd >= 0
vspd = 0
hspd = 20
}
(hspd is horizontal speed and vspd is the vertical speed)

the ground variable is 1 when on the ground and 0 while on the air. But this code is only working when the player is on the ground. I am certain I am doing this wrong but I don't know which part is wrong: is it the ground variable? is it where it becomes 0 and 1? is it the code on its entirety?
Your block and code logic is problematic. Try it this way.

Code:
if (zkey) {
     if ground == 0{
          vspd = 0;
          hspd = 20*image_xscale;
     }
}
Or some variation of that.
-I added some extra curly braces to declare your second block explicitly. As it was before you were only running the first line of code.
-I also added a second equal sign to your comparison check for stricter correctness, it's better to save single equal signs for declarations rather than comparisons. GML is rather forgiving in this manner.
-I removed your check for hspd, it may have been leading to your problems, did you mean to check for vspd? Feel free to re-add that bit.
-I multiplied your hspd by image_xscale to send the character the direction they are facing. Feel free to adjust this if you don't like it.
 

demonipo

Member
Your block and code logic is problematic. Try it this way.
Maybe that's why it was so hard to notice what could be wrong. Need to be better at block and code logic so I can make my life easier.

Code:
if (zkey) {
if ground == 0{
vspd = 0;
hspd = 20*image_xscale;
}
}
I tested it but it didn't work. Strange. I messed around with it a bit more, changing the ground variable and checking if image_xscale could be affecting anything but nothing. Literally. Atleast the code is more clean than what it was. ^ ^

-I removed your check for hspd, it may have been leading to your problems, did you mean to check for vspd? Feel free to re-add that bit.
The hspd check was to see if the dash would be forwards or backwards. The ground variable is to check if the character is on the ground. Maybe I should make the code different and check if the character is not on the ground. Not only that, make the character dash to a direction depending on if the left or right key is being pressed. Possibly after If (zkey) do If (rkey) or (lkey).
 

Cameron

Member
At this point it makes sense to use some debug messages. Try this:
Code:
if (zkey) {
    show_debug_message("ground = "+string(ground));
    if ground == 0{
        vspd = 0;
        hspd = 20*image_xscale;
    }
}
And then look in the compile window when pressing the zkey and see what ground is returning as. If it's returning as "0" then something is overriding the hspd and/or vspd variables but if it's not then you know to focus on the ground variable.
 

demonipo

Member
uh I'm dumb, i didn't notice that i was calling the dash before the jump and expected to dash in the air before i jumped. Sorry.
@CameronScottCreations your code actually works. I had in the wrong spot. Thanks everyone for the help and wasting your time on me. :D:D
 

demonipo

Member
At this point it makes sense to use some debug messages. Try this:
Code:
if (zkey) {
    show_debug_message("ground = "+string(ground));
    if ground == 0{
        vspd = 0;
        hspd = 20*image_xscale;
    }
}
And then look in the compile window when pressing the zkey and see what ground is returning as. If it's returning as "0" then something is overriding the hspd and/or vspd variables but if it's not then you know to focus on the ground variable.
I was seeing the variable changing with my objects draw GUI and it was changing correctly, it was just my dumbness on coding.
 

TheouAegis

Member
if (zkey){
Just a heads up: Even though the impact is small, using the macro throughout all your code is still slower than having a control object store the keyboard state in global variables for all the other instances to access if those key checks are going to be done multiple times per step.

if keyboard_check(ord("Z")) i++
is of course faster than
var zkey = keyboard_check(ord("Z"));
if zkey i++;

The two are about on par if the key state is only checked twice in a step. However, the more times the keyboard state is checked beyond that, the faster the variable check is compared to the macro check. With 10 variable checks vs. 10 macro checks, the variable checks are 110% faster. Now, with proper coding, you shouldn't really ever be calling the macro more than 2 times in a step, since the player object is usually the only one needing to read the inputs and that really should only be needed once or twice per step, if at all. But if you find that you're not an efficient coder and find yourself calling keyboard checks for the same key more than twice throughout the step, or if you for some reason need the same input checked across multiple objects, then you shouldn't use the macro.

Also, I'm not sure how well macros work with customizable controls.
 

demonipo

Member
Just a heads up: Even though the impact is small, using the macro throughout all your code is still slower than having a control object store the keyboard state in global variables for all the other instances to access if those key checks are going to be done multiple times per step.

if keyboard_check(ord("Z")) i++
is of course faster than
var zkey = keyboard_check(ord("Z"));
if zkey i++;

The two are about on par if the key state is only checked twice in a step. However, the more times the keyboard state is checked beyond that, the faster the variable check is compared to the macro check. With 10 variable checks vs. 10 macro checks, the variable checks are 110% faster. Now, with proper coding, you shouldn't really ever be calling the macro more than 2 times in a step, since the player object is usually the only one needing to read the inputs and that really should only be needed once or twice per step, if at all. But if you find that you're not an efficient coder and find yourself calling keyboard checks for the same key more than twice throughout the step, or if you for some reason need the same input checked across multiple objects, then you shouldn't use the macro.

Also, I'm not sure how well macros work with customizable controls.
Im using macro Z only once, since assigning multiple commands to one key can make games feel pretty weird and make moves come out just because of luck. However, knowing that macros are slower to read may help me in the future with other projects, so thank you for the warning.

(P.S.: Why is it so painful to write on mobile here? A one minute answer just took me five to write. WTF? )
 

TheouAegis

Member
Autocorrect typoing?

It's not the macro that's slower, it's what it is calling. Macros are good in and of themselves.
 
Top