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

GML [SOLVED] Basic platformer hsp move randomly switching values when player changes direction of move

MartinK12

Member
I use below super basic platformer move code in my on_ground state in user_event_0.

Simplified version of my code related to this problem:
Code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));

var move = key_right - key_left;
hsp = move * move_speed; //move_speed = 4

//THIS IS PROBLEMATIC PART START
if (hsp == 0) {
    //set sprite and sound for player idle   
    show_debug_message(hsp);
}           
else {        //else hsp <> 0 so we are running left or right
    //set sprite and sound for player running
    show_debug_message(hsp);
}
//THIS IS PROBLEMATIC PART END

if (hsp != 0) image_xscale = sign(hsp);

//horizontal collision
if (place_meeting(x+hsp,y,obj_wall)) {
    while (!place_meeting(x+sign(hsp),y,obj_wall)) {
        x = x + sign(hsp);
    }
    hsp = 0;   
}
x = x + hsp;
What's the problem?
I've noticed then when player press key_right so he's moving right the hsp value is 4. But when player changes the direction of movement to opposite so he wants to go left - he release the key_right and then presses key_left the hsp will behave in two ways:

behavior 1:
-in current step hsp will be 4
-in the next step hsp will be 0
-and in another next step hsp will be -4
-game steps looks like this, ie.:
step 10 - hsp 4
step 11 - hsp 0
step 12 - hsp -4

behavior 2: this happens occasionally when players fast flips key right to key left
-in current step hsp will be 4
-in the next step hsp will be -4
-so in this behaviour hsp skips the value of 0,
-game steps looks like this, ie.:
step 10 - hsp 4
step 11 - hsp -4
But I want to do some stuff when player changes his direction and I want hsp to be 0, but this behavior skips it entirely. Of course this is also true for opposite direction so moving to left and changing direction to right. When I do this stuff slowly everything works fine.

I assume that hsp behaves in two ways bcs of the speed of key presses? But the code is super basic so shouldn't it behave exactly the same all the time, or how can I achieve 100% proper behavior all the time?
Thank You for all suggestions, links, videos :)
 

Slyddar

Member
What you are describing is normal. In behaviour 1 you're holding a key, and then have pressed the opposite direction and either the presses have overlapped, resulting in a move of 0 as 1-1, or you've pressed it too late and had a move of 0 from 0-0.
In behaviour 2 it occasionally happens because you've pressed it perfectly and skipped the zero case.

It's not the same all the time because you can't press the buttons exactly at the right moment every time. You can even set a variable called "facing" or "dir" to 1 when moving right and -1 when moving left, and only change it when moving in that direction. You can then use that to do various things related to sprites and movement.

Is there some specific case where this normal behaviour is causing you problems?
 

MartinK12

Member
Thank You for your reply and clarification.
As my learning experience I'm building my own platformer engine.

In this case I want to have behavior set in my code so everytime when player changes direction (from left to right or right to left) the code will behave exactly the same no matter the speed of key presses. I thought I could achieve this with hsp variable alone and then do some actions based on that.

Are there some buffers for horizontal movements (ie. like we have for jumps) or it would be unnecessary complication and "facing" variable as you suggest should be enough to create exact behavior all the time when switching direction?
 

Slyddar

Member
I doubt having a hsp of 0 for one step during transitions will cause any issues....it doesn't for thousands of other platformers. Maybe just code it as is and see if it's a problem, I suspect you will find it is not.
 

MartinK12

Member
Sorry for late reply but 2 week holidays duties :(
Thank You @TheSly for your explanations, with them and my tests now I have better understanding of this code and yes hsp is not an issue :)
 
Top