SOLVED How to change the location of a object from the original placement as it enters the room?

S

SharpDim.

Guest
New in programming, sorry for the unsophisticated languages

So in my program there is a room that plays the role as the "main room", which means there are a several objects in that room and they are like entrances to other rooms, when the player object collides with one of those "entrances" it will be transferred to the corresponding room, and then through colliding the "exit" inside each room the player could get back to the main room.

But the problem is I've set a location for the instance of the player object in the main room (when I dragged the object in), and every time it enters the main room, no matter which room it comes out from, it will get back to that location, when it is expected to be near the "entrance" of the room it comes out from.

Just like in a game when the player comes out of a village they should be in front of the entrance of that village instead of somewhere else. How to achieve that through coding?

If there's confusion here's a graphic demonstration of the situation:Mainroom.jpg

Thank you guys very much for answering!
 
Last edited by a moderator:

NightFrost

Member
My typical solution: I don't have player placed in the room editor. I have global variables that define player's starting location in a room, and a script that gets called in room creation code that takes those globals and creates player character at given coordinates. When player collides with a door, besides performing a room switch the door also updates the globals to point at correct position in target room. The globals have default values that define initial start position in the main room, as at game start player has not yet collided with any door.
 
S

SharpDim.

Guest
My typical solution: I don't have player placed in the room editor. I have global variables that define player's starting location in a room, and a script that gets called in room creation code that takes those globals and creates player character at given coordinates. When player collides with a door, besides performing a room switch the door also updates the globals to point at correct position in target room. The globals have default values that define initial start position in the main room, as at game start player has not yet collided with any door.
Do you mind showing the script you are using? Thank you very much!
 

NightFrost

Member
Do you mind showing the script you are using? Thank you very much!
Sure. Somewhere in my game initialization code I have global declarations that define the initial start position:
GML:
global.Player_Start_X = 500;
global.Player_Start_Y = 200;
Then a script that gets called in room creation. Note that the call has to be in every game room you have.
GML:
///@function scr_create_player()
///@desc Spawns player at room start coordinates.

instance_create_layer(global.Player_Start_X, global.Player_Start_Y, "Characters", obj_player);
Each door then updates those globals as they switch room. Your doors are either each a unique object or you have some other way of retrieving correct target room. You store and act on target coordinates in the same manner.
 
Top