GameMaker [SOLVED]Controls for 8 direction

N

NURT

Guest
Please help me make the 8 direction control, I have arrow control, but I would like to make the control on W, S, A, D Here is the code:

Create:
spd = 3.0;

image_speed = .4;

Step:
//move
hInput = keyboard_check(vk_right) - keyboard_check(vk_left);
vInput = keyboard_check(vk_down) - keyboard_check(vk_up);

if(hInput !=0 or vInput !=0){
dir = point_direction(0,0,hInput,vInput);
moveX =lengthdir_x(spd, dir);
moveY =lengthdir_y(spd, dir);

x += moveX;
y += moveY;

//sprite

switch(dir){
case 0: sprite_index = spr_r_strip4; break;
case 45: sprite_index = spr_ur_strip4; break;
case 90: sprite_index = spr_u_strip4; break;
case 135: sprite_index = spr_ul_strip4; break;
case 180: sprite_index = spr_l_strip4; break;
case 225: sprite_index = spr_dl_strip4; break;
case 270: sprite_index = spr_d_strip4; break;
case 315: sprite_index = spr_dr_strip4; break;
}

} else {

image_index = 0;

}
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Just change the arrow controls for ord("W") etc...

EG:
GML:
hInput = keyboard_check(ord("D")) - keyboard_check(ord("A"));
 
Z

zendraw

Guest
you can have both
(vk_right | ord("D"))-(vk_left | ord("A"));
 
you can have both
(vk_right | ord("D"))-(vk_left | ord("A"));
Not a good suggestion with blatantly wrong code.
- You have to do ||, not |. The latter is bitwise OR. While it might work in this situation, it will not work in all situations and CANNOT be treated as interchangable with boolean OR, ||.
- There are no functions. You need to be using some kind of input checking function like keyboard_check.
- You have to have separate the functions. keyboard_check(vk_up || ord ("W")); is incorrect and will not work as expected.
 
Z

zendraw

Guest
Not a good suggestion with blatantly wrong code.
- You have to do ||, not |. The latter is bitwise OR. While it might work in this situation, it will not work in all situations and CANNOT be treated as interchangable with boolean OR, ||.
- There are no functions. You need to be using some kind of input checking function like keyboard_check.
- You have to have separate the functions. keyboard_check(vk_up || ord ("W")); is incorrect and will not work as expected.
are you serious right now? what i wrote is an IDEA, not copy paste code and anyone can see and understand that. and those are not function brackets, but separation brackets, obviosly.

-wether its bitwise or not it doesnt matter, what matters is if its positive or negative, anything above 0 is positive and incase you want to further drag this out for personal satisfaction, all the numbers returned are integers.
-no joke, really? i must have been writing poems on game maker til now.
-i didnt know that, what an enlightment you have been.

Sorry it doesn't work
you wrote hinput=hinput=code. you cant set hinput to itself without previously setting it, and you want to set it to the CODE you want, not to itself.
hinput=code.
 
N

NURT

Guest
Sorry, I don't quite understand.

What a fool I am! Thank you I understand!

Thank you all for helping and explaining to a fool like me!
 

Nidoking

Member
-wether its bitwise or not it doesnt matter, what matters is if its positive or negative, anything above 0 is positive
I don't know how numbers are actually stored in GML, but if it's two's complement or something similar, or even if it's just a negative bit, then -1 is false and 1 is true, but -1 | 1 is false, while -1 || 1 is correctly true.
 
Z

zendraw

Guest
(vk_right | ord(D)) - (vk_left | ord(A)) simply means this:
(this) - (this)
so if we press vk_right or D but not vk_left or A it will look like this
(1) - (0)
=>1
if we dont press vk_right nor D but we press vk_left or A:
(0) - (1)
=>-1

the | "OR" simply lets it take into account 2 variables instead of one.

heres an example
var horizontal=(vk_right | D | F | K | C)-(vk_left | A | X | B | M);

so if you press either one of the keys on the left but none from the keys on right it will return 1. if you dont press any of the keys on the left but press a key on the right it will return -1.

im not sure if it will take into account all the keys on the left so if you press 2 keys on the left but nonbe on the right, im not sure if it will return 2 or 1. this is one of nacho`s points that is acceptable for discussion, to me it doesnt make sense it to take all keys into account. someone who have tested it can say.

Edit: 0.5 and above is positive, anything below 0.5 is negative. that is the rule.
 

chamaeleon

Member
(vk_right | ord(D)) - (vk_left | ord(A)) simply means this:
(this) - (this)
so if we press vk_right or D but not vk_left or A it will look like this
(1) - (0)
=>1
if we dont press vk_right nor D but we press vk_left or A:
(0) - (1)
=>-1

the | "OR" simply lets it take into account 2 variables instead of one.

heres an example
var horizontal=(vk_right | D | F | K | C)-(vk_left | A | X | B | M);

so if you press either one of the keys on the left but none from the keys on right it will return 1. if you dont press any of the keys on the left but press a key on the right it will return -1.

im not sure if it will take into account all the keys on the left so if you press 2 keys on the left but nonbe on the right, im not sure if it will return 2 or 1. this is one of nacho`s points that is acceptable for discussion, to me it doesnt make sense it to take all keys into account. someone who have tested it can say.

Edit: 0.5 and above is positive, anything below 0.5 is negative. that is the rule.
@zendraw Is your code just shorthand because you didn't feel like writing keyboard_check_whatever() for each keyboard test because the expressions would be too long? If it is not, you're just creating some fixed value that has nothing to do with actual keypresses. If it is indeed shorthand that is not meant to be taken literally, you should clarify that.
 
Z

zendraw

Guest
@zendraw Is your code just shorthand because you didn't feel like writing keyboard_check_whatever() for each keyboard test because the expressions would be too long? If it is not, you're just creating some fixed value that has nothing to do with actual keypresses. If it is indeed shorthand that is not meant to be taken literally, you should clarify that.
yes, it is for quick reading and typing. even understanding. typing if (A or B or C) makes more sense to someone who has no coding thinking rather then typing whole functions. you may think about some notstandart variables A B C, but ordinary people like the guy who made the topic wuld think keyboard keys. even so more becouse the topic is such.
 
heres an example
var horizontal=(vk_right | D | F | K | C)-(vk_left | A | X | B | M);
"Here's an example" implies that's verbatim how the code is written. You weren't clear on this in your original post, either. If both @chamaeleon and I got confused by what you were trying to do, what is a newbie going to think? If you can spend an entire post throwing passive-aggresive jabs at me, you can spend the time to write out or copy-paste "keyboard_check" where it belongs.

Of note is that bitwise OR is not a boolean operand and will not trigger the short-circuit evaluation. f(1) | f(2) | f(3) | f(4) | f(5) will evaluate all 5 of those functions no matter what. f(1) || f(2) || f(3) || f(4) || f(5) will only evaluate f(1) and stop once it sees that it's true. Bitwise OR has a lot of functionality that would be confusing to a newbie whereas boolean OR makes perfect sense. Just because you can drive a nail with a sledgehammer doesn't mean you should recommend using one over a regular hammer.
 

BiTrunade

Member
I have arrow control, but I would like to make the control on W, S, A, D Here is the code:
To check whether a keyboard key has been pressed you would use the keyboard_check(key) function. The keyboard_check() function requires one parameter which is the key that you want to check. The key parameter is defined by its UTF8 code, which is a numerical value.

GameMaker Language (GML) provides you with constants, vk_* constants, that hold the UTF8 code for each key. You can use those vk_* constants, such as vk_left and vk_shift, directly with keyboard_check() function. Therefore when using keyboard_check_pressed(vk_left) it would check whether the left key has been pressed.

Examples:

GML:
if(keyboard_check(vk_space)) {
    // jump
}

if(keyboard_check(vk_left)) {
    // move left
}

if(keyboard_check(vk_backspace)) {
    game_end();
}
You can find all the vk_* constants in the Keyboard Input section of the Manual

Using draw_text() to print the values of the four vk_* arrow keys would print the following values:

1603655970625.png

As you can see those are the UTF8 code values of those vk_*.

Alphabetical letters, from A-Z, are strings and NOT UTF8 codes, therefore keyboard_check() would not recognize them. For example:

GML:
if(keyboard_check("W")) {
    // Would do nothing, because "W" is a string, and not a UTF8 code
}
To get the UTF8 code for a capital alphabetical letter, you would need to use the ord() function which returns the UTF8 code for the letter that you want to use, which in return, allows the keyboard_check() function to recognize that key and check whether it was held down, pressed, or released.

Therefore:

GML:
if(keyboard_check(ord("W"))) {
    // Would work, because ord("W") is the UTF8 code for the W key
    // and not simply the string "W"
}
Simply put, keyboard_check functions won't recognize any key that is not its UTF8 code for it. Therefore, using (vk_right | ord(D)) or any of its deviation would not be possible, because, whether they return a boolean or any value, they won't be the UTF8 code for those keys, therefore the keyboard_check won't recognize them.

Examples:

GML:
if(keyboard_check(ord("W") || vk_up)) {
    // Would do nothing
    // because (ord("W") || vk_up) is not a UTF8 code for either keys
    // or anykey for that matter
}
But:

GML:
if(keyboard_check(ord("W")) || keyboard_check(vk_up)) {
    // Would work because we check each key separately whether they were pressed.
    // This would check whether vk_up had been pressed OR whether W key had been pressed.
    // Which is the right and only approach to achieve this.
}
Another thing, keyboard_check() function returns 1 when the key is pressed, and 0 when it is not. Therefore the following code
hInput = keyboard_check(ord("D")) - keyboard_check(ord("A")); would be:
  • Positive, when only D is pressed: 1 - 0 = +1
  • Negative, when only A is pressed: 0 - 1 = -1
  • and Zero, when both D and A are pressed: 1 - 1 = 0
Same applies to other keyboard_check() functions, except keyboard_check_released() checks whether a key had been released.
 
Last edited:
Top