GMS 2 Persistent player should start on the right side of screen after changing room

Discussion in 'Programming' started by pimeys, Jul 14, 2019.

  1. pimeys

    pimeys Member

    Joined:
    Jul 14, 2019
    Posts:
    8
    I have an invisible trigger object at the end of a room, that is supposed to bring the persistent(!) player to the next room. So when I leave one room to the left side of the screen, I want the player to enter the next room on the right side of the screen.
    So my trigger object, is pretty much trying this (very simplified):

    Code:
    if(place_meeting(x, y, oPlayer)){
        room_goto(target);
        with(oPlayer){
            x = room_width;
        }
    }
    
    However, my rooms can be widely different in size, so the first room could have a width of 10,000, and the new one only has a width of 1,000. It looks like "room_width" is always just the width of the current room, so the player would end up on x = 10,000 in a room that only has a width of 1,000.
    Is there a way to check the width of a different room (here "target"), so I can set the x to the new room's width instead? Or is there a different trick to make this work? Trying something like "target.room_width" does not work unfortunatelly
     
  2. maru_th_undrtkr

    maru_th_undrtkr Member

    Joined:
    Jan 23, 2018
    Posts:
    45
    you can do this, might not be the best solution. maybe an easier solution would be an object. This is faster, but I can't help think its a bit more work than should be needed, especially if you need to make tons of levels or changes.
    Code:
    
    if room_next(room) = room_1 {
    with(player){
    x=0;
    y=0;
    }
    }
    
     
  3. pimeys

    pimeys Member

    Joined:
    Jul 14, 2019
    Posts:
    8
    Are you suggesting to kinda hardcode the width and height of each room into an object? I'm afraid this is not a solution.
     
  4. pimeys

    pimeys Member

    Joined:
    Jul 14, 2019
    Posts:
    8
    I came up with a workaround that works, but might not be optimal.

    What I ended up doing for now is instead of setting x to room_width right away, I make the player invisible and set an alarm on the player for 1 frame, which will then set x to the room_width of the new room and make the player visible again. This technically makes the player invisible for one frame though, so it's a kinda dirty workaround.

    Please let me know, if anyone knows a better approach.
     
  5. HeWhoShallNotBeNamed

    HeWhoShallNotBeNamed Member

    Joined:
    Nov 9, 2018
    Posts:
    65
    You could try setting the player's x position in the Room Start event. I don't know if that solution would be any better than yours, though.
     
  6. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    700
    I don't think you can get the width of a room before entering it, oddly enough I expected to find a function for this in the docs, but looks like you are out of luck.

    I can think of a couple of solutions though:

    1. Set a variable in the player instance to be used in the room start event to place it at the correct side of the room. Something like this:
    Code:
    //create (oPlayer)
    enum PLAYER_SPAWN {
        east,
        south,
        west,
        north
    }
    
    spawn_next = undefined;
    
    Code:
    //trigger object (collision?)
    if(place_meeting(x, y, oPlayer)){
       with(oPlayer){ spawn_next = PLAYER_SPAWN.east; }
       room_goto(target);
    }
    
    Code:
    //room start (oPlayer)
    switch(spawn_next) {
        case PLAYER_SPAWN.east: x = room_width; break;
        /* ... other cases .... */
    }
    
    2. Instead of hardcoding the position as suggested, you could place some spawn points in the rooms and link them to the trigger objects in the other rooms by using custom instance names in the room editor. This allows you to let the player, using the room start event, spawn in the new room at a very specific location depending on how he entered, without having to rely on the room size or on hardcoded coordinates (although the spawn instance is technically hardcoded, but I don't really see that as a problem in this case)
     
    Last edited: Jul 14, 2019
  7. emicarra

    emicarra Member

    Joined:
    Jul 2, 2017
    Posts:
    38
    In case using Room_start event doesn't work, you can set players' alarm[0] to 1 so that it's activated at the next frame after changing rooms, and in alarm[0] do the x = room_width;
     

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