Apologies if this is addressed somewhere else, didn't see anything like this but perhaps didn't search correctly.
I'm building a very basic version of Snake to teach myself GML and GMS2, running into issues with movement.
In the create event for my snake, I inserted
snakeSpeed= 30;
q = array_create(0);
Right = keyboard_check_pressed(ord("D"));
Left = keyboard_check_pressed(ord("A"));
Up = keyboard_check_pressed(ord("W"));
Down = keyboard_check_pressed(ord("S"));
and then in the step I use alarms that actually move the snake
if Right
{
alarm[0] = snakeSpeed;
}
if Down
{
alarm[1] = snakeSpeed;
}
...
in this form, nothing happens upon WASD input. I've got an array in the create even that builds the snake upon a collision event with the food that works just fine, which tells me that the array data and snake speed data are able to be accessed from the create event, just not the Right, Left, Up, or Down functions.
However, including them in the step event like so
Right = keyboard_check_pressed(ord("D"));
Left = keyboard_check_pressed(ord("A"));
Up = keyboard_check_pressed(ord("W"));
Down = keyboard_check_pressed(ord("S"));
if Right
{
alarm[0]=snakeSpeed;
}
if Down
{
alarm[1]=snakeSpeed;
}
...
and the snake moves just fine with WASD inputs. Any idea why this is?
keyboard_check_pressed()
will only work if you are -currently- pressing the mentioned button.So such a condition check will only work in the Step Event.
You can still create bools in the Create Event, but if you expect their values to change during the game, then you'll have to put the conditions that alter the variables in the Step Event. I'm assuming the same also applies for Checking Collisions.
Personally, it would fit better if you define the key instead of the
keyboard_check_pressed()
as a variable. So in the create event, you'll put this:And in the Step Event, you can put this: