SOLVED Check direction face character before move.

Luk

Member
Hi, I am quite new to GM, I would like someone to help me, you see, I have managed to make my character move in a 16x16 grid, which changes the sprites according to the direction in which I move. But in addition to that I would like to implement the following validation:
-When my character is looking in one direction, and a button is in the opposite direction to the one he is looking at, my character stays in the same position but looking in the direction he presses. And if I press the same button again then my character must move because it is the direction in which he is looking.

I will be very grateful if you at least give me a hint of how to encompass that validation. Thank you very much in advance.
 

Nidoking

Member
Are you familiar with the usage of "if" and "else"? Because that's really all you need.

if the direction is the way you want to go,
go
else
turn
 
  • Like
Reactions: Luk

Luk

Member
Are you familiar with the usage of "if" and "else"? Because that's really all you need.

if the direction is the way you want to go,
go
else
turn
Thanks for your response, and yes, I am familiar with conditional structures. I assumed the same as you, but when I put it in the Step event I don't get what I want. In any case do you have any idea what event I should use?

GML:
    //in create event
 
    distance = 0
    spd = 16/60 * 4; 
 
    sprite_index = spr_hero_man_idle_down;
    image_index = 1;
    image_speed = 0;

    action = "idle";
    face = "down";
    face_old = face;
GML:
//Step event

if(speed != 0)
{
    distance -=spd;
 
    if (distance <=0)
    {    
        speed =0;
        image_index = 1;
    }
}
    xSpeed = 0;
    ySpeed = 0;
 
    if (speed ==0)
    {

        if ( keyboard_check(ord("W")))
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_up");
            image_speed = 2;
            ySpeed = -spd;
            distance = 16;
            face ="up";
        }
        else if(keyboard_check(ord("S")))
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_down");
            image_speed = 2;
            ySpeed = spd;
            distance = 16;
            face ="down";
        }
        else if( keyboard_check(ord("D")))
        {
            sprite_index = asset_get_index("spr_hero_man_" + action  + "_right");
            image_speed = 2;
            xSpeed = spd;
            distance = 16;
            face ="right";
        }
        else if(keyboard_check(ord("A")))
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_left");
            image_speed = 2;
            xSpeed = -spd;
            distance = 16;
            face ="left";
        }
        else
        {
            image_index = 1;
            image_speed = 0;

        }
     
        hspeed = xSpeed;
        vspeed = ySpeed;

    }
 
Last edited:
There is an example of what's missing (I just did "down" part, lazyness, sorry!)
You have to check the facing direction too.
GML:
if((keyboard_check(ord("S"))) && (face !="down")) {
//Just change the facing direction since you were NOT already looking down
face = "down";
}
 
  • Wow
Reactions: Luk

Luk

Member
There is an example of what's missing (I just did "down" part, lazyness, sorry!)
You have to check the facing direction too.
GML:
if((keyboard_check(ord("S"))) && (face !="down")) {
//Just change the facing direction since you were NOT already looking down
face = "down";
}
hahaha thanks for taking some time!, arriving at my house I will check your solution. See you then :)
 

Luk

Member
There is an example of what's missing (I just did "down" part, lazyness, sorry!)
You have to check the facing direction too.
GML:
if((keyboard_check(ord("S"))) && (face !="down")) {
//Just change the facing direction since you were NOT already looking down
face = "down";
}
Hi @Slow Fingers , I already tried it, I see that it works partially, what happens is that now when I press a directional button (A, W, S, D) it is as if it gets stuck sometimes. For example, if my character is looking to the right and I press S, my character walks in that direction, and in what I have debugged I have seen that it does not stop going through the Step event and for that reason the face variable always is changing. You will have any suggestions besides uninstalling GM? xd


GML:
//Step event

if(speed != 0)
{
    distance -=spd;
  
    if (distance <=0)
    {     
        speed =0;
        image_index = 1;
    }
}
    xSpeed = 0;
    ySpeed = 0;
  
    if (speed ==0)
    {
    // Set x/y velocity based on direction of keys
        if ( keyboard_check(ord("W"))  and face == "up")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_up");
            image_speed = 2;
            ySpeed = -spd;
            distance = 16;
            face ="up";
        }
        else if(keyboard_check(ord("S"))  and face == "down")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_down");
            image_speed = 2;
            ySpeed = spd;
            distance = 16;
            face ="down";
        }
        else if( keyboard_check(ord("D"))  and face == "right")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action  + "_right");
            image_speed = 2;
            xSpeed = spd;
            distance = 16;
            face ="right";
        }
        else if(keyboard_check(ord("A")) and face == "left")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_left");
            image_speed = 2;
            xSpeed = -spd;
            distance = 16;
            face ="left";
        }
        else if(keyboard_check(ord("W"))  and face != "up")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_up");
            image_speed =0;
            ySpeed = 0;
            distance = 0;
            face ="up";
        }
        else if(keyboard_check(ord("S"))  and face != "down")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_down");
            image_speed =0;
            ySpeed = 0;
            distance = 0;
            face ="down";
        }
        else if(keyboard_check(ord("D"))  and face != "right")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_right");
            image_speed =0;
            xSpeed = 0;
            distance = 0;
            face ="right";
        }
        else if(keyboard_check(ord("A"))  and face != "left")
        {
            sprite_index = asset_get_index("spr_hero_man_" + action + "_left");
            image_speed =0;
            xSpeed = 0;
            distance = 0;
            face ="left";
        }
        else
        {
            image_index = 1;
            image_speed = 0;

        }
      
        hspeed = xSpeed;
        vspeed = ySpeed;
        }
 

Luk

Member
I got it! At the end I added a delay so that there is a little pause when pressing the button. Here I share the code, maybe it will be useful to someone else, now I only have to refactor. Thank you very much to all!

Code:
    distance = 0

    spd = 16/60 * 4;

    image_speed = .4;

 

    sprite_index = spr_hero_man_idle_down;

    image_index = 1;

    image_speed = 0;



    action = "idle";

    face = "down";

    face_old = face;

 

     delay = 0;


Code:
//Step event



/// @description Inserte aquí la descripción

// Puede escribir su código en este editor





if(speed != 0)

{

    distance -=spd;

 

    if (distance <=0)

    {     

        speed =0;

        image_index = 1;

    }

}

    xSpeed = 0;

    ySpeed = 0;

 

    if (speed ==0)

    {

    // Set x/y velocity based on direction of keys

 

        if ( keyboard_check(ord("W"))  and face == "up" and delay > 10)

        {

            sprite_index = asset_get_index("spr_hero_man_" + action + "_up");

            image_speed = 2;

            ySpeed = -spd;

            distance = 16;

            face ="up";

        }

        else if(keyboard_check(ord("S"))  and face == "down" and delay > 10)

        {

            sprite_index = asset_get_index("spr_hero_man_" + action + "_down");

            image_speed = 2;

            ySpeed = spd;

            distance = 16;

            face ="down";

        }

        else if( keyboard_check(ord("D"))  and face == "right"  and delay > 10)

        {

            sprite_index = asset_get_index("spr_hero_man_" + action  + "_right");

            image_speed = 2;

            xSpeed = spd;

            distance = 16;

            face ="right";

        }

        else if(keyboard_check(ord("A")) and face == "left" and delay > 10)

        {

            sprite_index = asset_get_index("spr_hero_man_" + action + "_left");

            image_speed = 2;

            xSpeed = -spd;

            distance = 16;

            face ="left";

        }

        else if(keyboard_check(ord("W"))  and face != "up")

        {

            sprite_index = asset_get_index("spr_hero_man_" + action + "_up");

            image_speed =0;

            ySpeed = 0;

            distance = 0;

            face ="up";

            delay = 0;

        }

        else if(keyboard_check(ord("S"))  and face != "down")

        {

            sprite_index = asset_get_index("spr_hero_man_" + action + "_down");

            image_speed =0;

            ySpeed = 0;

            distance = 0;

            face ="down";

            delay = 0;

        }

        else if(keyboard_check(ord("D"))  and face != "right")

        {

            sprite_index = asset_get_index("spr_hero_man_" + action + "_right");

            image_speed =0;

            xSpeed = 0;

            distance = 0;

            face ="right";

            delay = 0;

        }

        else if(keyboard_check(ord("A"))  and face != "left")

        {

            sprite_index = asset_get_index("spr_hero_man_" + action + "_left");

            image_speed =0;

            xSpeed = 0;

            distance = 0;

            face ="left";

            delay = 0;

        }

        else

        {

            image_index = 1;

            image_speed = 0;



        }

      

            delay++;

            if delay > 10

            {

                hspeed = xSpeed;

                vspeed = ySpeed;

            }
 
I have seen that it does not stop going through the Step event and for that reason the face variable always is changing. You will have any suggestions besides uninstalling GM? xd
Uninstalling won't do anything, since it's the way it's supposed to be.
The step event is running every frame (60 times per second), it is what it's for.
The bulk of the else/if statements, although quite inelegant, seems ok to me.
You realize ALL those else/if are inside a if (speed ==0) conditional, right? What happens if I'm pressing a key and I'm moving? Also, you use both the built-in speed and a custom spd variable, that's probably not right either.
 
  • Like
Reactions: Luk

Luk

Member
Uninstalling won't do anything, since it's the way it's supposed to be.
The step event is running every frame (60 times per second), it is what it's for.
The bulk of the else/if statements, although quite inelegant, seems ok to me.
You realize ALL those else/if are inside a if (speed ==0) conditional, right? What happens if I'm pressing a key and I'm moving? Also, you use both the built-in speed and a custom spd variable, that's probably not right either.
[/CITA]
Sorry, I was joking about uninstalling GM, and again thank you very much for the clarification, I really value your time. Currently the object is moving as I wanted, and yes, I have to refactor code. I'll do that to make it cleaner.

Greetings from Peru!
 
Last edited:
Top