1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Need help with playerFacingAfter code

Discussion in 'Programming' started by LiamCollie, Apr 19, 2019.

  1. LiamCollie

    LiamCollie Member

    Joined:
    Apr 19, 2019
    Posts:
    4
    Hello! I'm extremely new to GMS2 and I am learning GML2 along with a video on YT from CosmoAstronaut's series Farm RPG.

    And I am at the point of room transitions and with the playerFacingAfter through the game object and what not.

    My issue is, when I go to the transition object I made after doing all the code in game object, player object etc. When I transition to the next room, my player is facing downward, regardless what I ser "playerFacing = dir.up ;"

    But in the code for the playerFacingBefore works just fine. I am not using the same sprites as she is mine are are different in size.

    Right here is my transition object's creation code.
    [​IMG]

    Player code create;


    Code:
    w_spd = 1;
    n_spd = 2;
    r_spd = 3;
    spd = n_spd;
    
    x_frame = 0;
    y_frame = 0;
    
    facing = 0;
    
    
    Player Step;

    Code:
    //---INPUT
    input_left  = keyboard_check(vk_left) ;
    input_right = keyboard_check(vk_right) ;
    input_up = keyboard_check(vk_up) ;
    input_down = keyboard_check(vk_down) ;
    input_walk = keyboard_check(vk_control) ;
    input_run = keyboard_check(vk_shift) ;
    
    //--- ALTER SPEED
    if(input_walk or input_run) {
        spd = abs((input_walk*w_spd) - (input_run*r_spd) ) ;
    } else {
        spd = n_spd;
    }
    
    //---RESET MOVE VARIABLES
    moveX = 0;
    moveY = 0;
    
    //---INTENDED MOVEMENT
    moveY = (input_down - input_up) * spd;
    if(moveY == 0) { moveX = (input_right - input_left) * spd; }
    
    //---COLLISON CHECKS
    
    //Direction Facing
    if(moveX != 0) {
        switch(sign(moveX) ) {
            case 1: facing = dir.right; break;
            case -1: facing = dir.left; break;
        }
    } else if(moveY != 0) {
        switch(sign(moveY) ) {
            case 1: facing = dir.down; break;
            case -1: facing = dir.up; break;
        }
    } else {
        facing = -1;
    }
    
    
    //Objects
    var inst = instance_place(x,y,obj_transition) ;
    if(inst != noone and facing == inst.playerFacingBefore) {
        with(game) {
            if(!doTransition) {
            spawnRoom = inst.targetRoom;
            spawnX = inst.targetX;
            spawnY = inst.targetY;
            spawnPlayerFacing = inst.playerFacingAfter;
            doTransition = true;
            }
        }
    }
    
    //Horizontal
    if(moveX != 0) {
        if(place_meeting(x+moveX, y, obj_collision) ) {
        repeat(abs(moveX) ) {
            if(!place_meeting(x+sign(moveX) , y, obj_collision) ) { x += sign(moveX); }
           else { break; }
        }
        moveX = 0;
       }
     }
    
    //---Vertical
    if(moveY != 0) {
        if(place_meeting(x, y+moveY,  obj_collision) ) {
        repeat(abs(moveY) ) {
            if(!place_meeting(x+sign(moveX) , y, obj_collision) ) { x += sign(moveX); }
            else { break; }
        }
        moveY = 0;
       }
    }
     //---APPLY MOVEMENT
     x += moveX;
     y += moveY;
    
    Player Draw;

    Code:
    var anim_length = 3;
    var anim_speed = 5;
    var frame_size = 22;
    
    switch(facing) {
        case dir.left: y_frame = 6; break;
        case dir.right: y_frame = 1; break;
        case dir.up: y_frame = 4; break;
        case dir.down:  y_frame = 0; break;
        case -1:    x_frame = 0; break;
    }
    
    //--- Draw Sprite
    draw_sprite_part(spr_player,0, floor(x_frame)*frame_size, y_frame*frame_size, frame_size, frame_size,x,y,);
    
    //---Speed Increment
    if(x_frame < anim_length -1) { x_frame += anim_speed/60; }
    else                         { x_frame = 0; }
    
    
    -------------

    And this here is my game object code

    Create:

    Code:
    ///description
    randomize() ;
    room_goto_next()
    
    guiWidth = display_get_gui_width() ;
    guiHeight = display_get_gui_height() ;
    
    blackAlpha = 0;
    
    spawnRoom = -1;
    
    spawnPlayerFacing = -1;
    doTransition = false;
    
    spawnX = 0;
    spawnY = 0;
    
    enum dir {
        right = 0,
        up = 90,
        left = 180,
        down = 270,
    }
    
    
    Draw GUI

    Code:
    if(doTransition) {
        //BlackFade/RT
        if(room != spawnRoom) {
            blackAlpha += 0.1;
            if(blackAlpha >= 1) { room_goto(spawnRoom) }
        } else {
            blackAlpha -= 0.1;
            if(blackAlpha <= 0) { doTransition = false; }
        }
    
    draw_set_alpha(blackAlpha) ;
    draw_rectangle_colour(0,0, guiWidth, guiHeight, c_black, c_black, c_black, c_black, false)
    draw_set_alpha(1) ;
    }
    Room Start;

    Code:
    if(spawnRoom == -1) exit;
    obj_player.x = spawnX;
    obj_player.y = spawnY;
    obj_player.facing = spawnPlayerFacing;
    
    with(obj_player) {
        switch(facing) {
        case dir.left: y_frame = 6; break;
        case dir.right: y_frame = 1; break;
        case dir.up: y_frame = 4; break;
        case dir.down:  y_frame = 0; break;
    }
    }
    ---------------

    This here is my object transition create code
    Code:
    targetRoom = -1;
    targetX = 0;
    targetY = 0;
    
    playerFacingBefore = -1;
    playerFacingAfter = -1;
    
    
    --------------
    This is my room 1 room settings, not sure if this matters or not.
    [​IMG]

    And the forest room;

    [​IMG]



    Maybe I should also add the camera code as well.

    Camera Create;

    Code:
    following = obj_player;
    h_border = 60;
    v_border = 30;
    
    Step;

    Code:
    x = clamp(x, following.x-h_border, following.x+h_border) ;
    y = clamp(y, following.y-v_border, following.y+v_border) ;
    ---------------

    I know absolutely nothing about GML and I just been following along with the Farm RPG tutorial, and maybe my screw up is not using the same sprites exactly? I dont know, I am new at this lol I really love GMS2 and really enjoying it, but I been trying to go back and figure out, why the playerFacingAfter part isnt doing anything.

    Cuz in the end, I want to make something simular to Stardew/Harvest Moon then more RPG based stuff as in like Final Fantasy inspired (Grew up playing that on SNES <3 )

    But yeah, any pointer any help is highly appreciated :D

    Thank you for your time :)
     
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    997
    You could add a breakpoint at the start of the collision between the player and the transition object (F9), and press F6 to run in debug mode, and then step through the code (F11) after the transition happens and see why it points you the wrong way.

    As for the debugger, check out these reference if you've never used it. Some very useful information.

    Debugging - https://developer.amazon.com/blogs/...7228a3107d1/debugging-with-gamemaker-studio-2
    Advanced Debugging - https://developer.amazon.com/blogs/...debugging-and-profiling-in-gamemaker-studio-2
     
    Bentley likes this.
  3. LiamCollie

    LiamCollie Member

    Joined:
    Apr 19, 2019
    Posts:
    4
    Where would I set the breakpoint in the code? In the player object, transition object or game object? Since I am using the game object to really control things from the video tutorial is teaching
     
  4. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    997
    Add the breakpoint at the collision with the transition object, then step through the code from there to see where it changes. You can even add a watch on the players facing to ensure it's correct from that position.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice