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

Combining keyboard events causes bug?

C

CharlieH

Guest
Hi,
I am very new to GameMaker and I started using it 3 days ago and I found a problem.
I am using simple keyboard check as below:
Code:
if (keyboard_check_released(ord("D")||ord("A")||ord("W")||ord("S")))
{
    image_index = 0;
}
I am not sure whether this is a good way of combining multiple conditions into 1.
But it works as expected.
However, when I make keyboard_check_pressed event as below, it gives me totally different results.
Code:
if (keyboard_check_pressed(ord("D")||ord("A")||ord("W")||ord("S")))
{
    delay_counter = 1;
    tap_counter += 1;
}
Variable delay_counter should only be set to 1 when I press D or A or W or S key.
However, when I press any key on my keyboard, delay_counter is set to 1.
I am not sure why because it seems work well with key_check_released event.
So I have no choice but change my code to this:
Code:
if (keyboard_check_pressed(ord("D"))
  ||keyboard_check_pressed(ord("A"))
  ||keyboard_check_pressed(ord("W"))
  ||keyboard_check_pressed(ord("S"))
   )
{
    delay_counter = 1;
    tap_counter += 1;
}
After changing my code, everything works fine. I don't see any difference between those 2 methods.
I know this is entry level codes but I am really confused. Can someone explain this?
Or is this a bug? And thanks in advance.:)
 
You can only check for a single key with the keyboard functions. You're submitting a completely different value into the function by using the or statements, which is why the behaviour is unexpected.
 

TailBit

Member
To explain, it will perform the || operation on all the ord() functions, since they are positive values the || will return true for each of them, so in the end you are just checking:
keyboard_check_pressed(1)
 
C

CharlieH

Guest
You can only check for a single key with the keyboard functions. You're submitting a completely different value into the function by using the or statements, which is why the behaviour is unexpected.
Thanks, now I know it is not a software bug. Seems I want to optimize my game way too much without understanding how these functions work. My bad.
 
C

CharlieH

Guest
To explain, it will perform the || operation on all the ord() functions, since they are positive values the || will return true for each of them, so in the end you are just checking:
keyboard_check_pressed(1)
Oh man, thank you for the explanation. I never thought it works this way. From now on, I will avoid writing keyboard check keys into one function.
 
Top