Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Mehrdad on Sat 09/12/2017 17:10:38

Title: Change room with keep player position[Solved]
Post by: Mehrdad on Sat 09/12/2017 17:10:38
Hi

I want to switch between two rooms with keep character position. My mean is player appear in the new room with the same position in the previous room.
I want this occur automatically for any time player want to switch between the two rooms.for example with the press a key.

Thanks
Title: Re: Change room with keep player position
Post by: Crimson Wizard on Sat 09/12/2017 17:29:05
If you mean same position in room coordinates, that's simply like:
Code (ags) Select

player.ChangeRoom(roomnumber, player.x, player.y);


EDIT: lol, I actually forgot that, as Khris pointed out below, you do not have to pass x,y arguments if you want them to stay same in the new room, so it's really just
Code (ags) Select

player.ChangeRoom(roomnumber); // it will keep same coords
Title: Re: Change room with keep player position
Post by: Mehrdad on Sun 10/12/2017 06:34:35
Yes, I know it. But my means is another things. I want to switch between two rooms and player get the last coordinate automatically and appear in new room in same coordinate. For example with press "F" in keyboard it changes room and stand in same previous room coordinate but in new room.
I hope was clear this time.
Title: Re: Change room with keep player position
Post by: Crimson Wizard on Sun 10/12/2017 08:51:12
Quote from: Mehrdad on Sun 10/12/2017 06:34:35
Yes, I know it. But my means is another things. I want to switch between two rooms and player get the last coordinate automatically and appear in new room in same coordinate. For example with press "F" in keyboard it changes room and stand in same previous room coordinate but in new room.
I hope was clear this time.


Sorry, this is rather confusing. Your first sentence sounds like one thing, but second sentence sound like another thing.
What do you call "previous room" and "new room" exactly?

Could you mean, maybe, that for each room X last character coordinates should be remembered, and when you return to room X these saved coordinates should be applied again?

For that case:
Code (ags) Select

#define MAX_ROOM_NUMBER Put max room number here
struct RoomPosition
{
    int x;
    int y;
    // you can add more here, like character's facing direction
}
RoomPosition LastRoomPos[MAX_ROOM_NUMBER + 1];

function on_event(EventType event, int data)
{
    if (event == eEventLeaveRoom)
    {
        LastRoomPos[player.Room].x = player.x;
        LastRoomPos[player.Room].y = player.y;
    }
    else if (event == eEventEnterRoomBeforeFadein)
    {
        player.x = LastRoomPos[player.Room].x;
        player.y = LastRoomPos[player.Room].y;
    }
}


The code above will apply the last coordinates all the time. If you want them applied only under certain conditions, like key press, then remove last part of the function above (under "if (event == eEventEnterRoomBeforeFadein)") and add this instead:
Code (ags) Select

function ReturnToLastPosInRoom(int room)
{
    player.ChangeRoom(room, LastRoomPos[room].x, LastRoomPos[room].y);
}

Then you can call this function whenever you need to.


If this is still not what you want, then try breaking the task into steps, like -
1) When leaving room X, store player's coordinates.
2) ...
Title: Re: Change room with keep player position
Post by: Khris on Sun 10/12/2017 18:52:17
  player.ChangeRoom(2);
Title: Re: Change room with keep player position
Post by: Mehrdad on Tue 12/12/2017 16:17:08
Thanks CW
Done. works perfect.Thanks a lot.

@Khris
Strange... It works perfect too and don't need to another actions. Thanks so much.