(Solved) Connect Multiple Rooms with a Global Variable?

How do I…

I’m trying to make a small open-world game with multiple scenes. I’m having problems with changing the room scenes. Even when I got the scenes to change properly, the player won’t spawn at the position I programmed it to.

What is the expected result

I have three rooms: an office (one exit), a kitchen (two exits), and the dining room (one exit).

When the player leaves the office at the right side of the room, they should spawn at the left side of the kitchen. When the player leaves the kitchen through the left exit, they should spawn back in the office on the right side.

When the player leaves the kitchen through the second exit on the right side of the room, they should spawn in the right side of the dining room. When the player leaves the dining room through the left exit, they should spawn in the right side of the kitchen.

What is the actual result

When the player leaves one room to enter another, they’ll either spawn in the wrong room, respawn in the same room at the wrong position, or spawn in the right room but at the wrong position.

I’ve been at this for almost 5 hours by now and frustrated. I’m gonna try to take a break, so I’ll be slow to reply.

here’s the link of the guide I followed. The guy is programming other things, so there’s not one video that focuses on connecting the rooms. It’s sprinkled in between several videos of the playlist. I’ve been jumping between the videos to find the right steps.

Tutorial Set 3: Optimization through External Layouts and Events in Gdevelop - YouTube

If someone knows an easier or more recent tutorial that could help, I’d truly appreciate it.

Related screenshots




I love that guy’s tutorials.

Ok for the office and the dining room since they only have 1 possible entry point, you don’t need to worry about position. You just drag the character sprite near the door, and when the scene starts they will be at the door like they just walked from the kitchen into the office.

So for the kitchen because you don’t want him spawning at the left door when he just came back in through the dining room, hmm. Well if the kitchen is my starting scene I will give it the value 0. I’m not literally giving it a value in a global variable, just in my mind I know it’s 0. I will place 3 player sprites in this scene. One in the middle of the kitchen like the game just started, with an instance variable RoomNumber = 0. Then one by the Office door with an instance variable RoomNumber=1 and one by the DiningRoom door with RoomNumber 2. Then at Begin of Scene the variable RoomNumber of Player not equal to GlobalVariable LastRoom, delete Player.
Now in my Office I place 1 player sprite by the door. At Begin of Scene I change globalVariable LastRoom to 1.
I do the same in my dining room, place player sprite by the door and At Begin of Scene change globalVariable LastRoom to 2.

Anyway my Christmas belly can’t think of number right now so this is what I’d do but you can do it calling positions too in a similar way. Like in kitchen Begin of Scene if globalVariable lastRoom = 2 then create player sprite at blahblah. Just editing to add, but if you’re going to change the globablVar to the current room number, make sure to do it after you ran all the begin of scene actions that needed that info.

1 Like

Hi Lucky J. Thanks for replying.
I experimented with some of your advice and was able to find a solution!
I added a Previous Room global variable like you suggested, but I found it didn’t work until I added the Change (object’s) Position to (X, Y) action with the X and Y global variables I already had. I’ll keep those to variables in the code for future uses. I still need to program a Save/Checkpoint system in the future.
Thank you so much for your help. I was really hitting a wall, there!
Here’s the functioning result, using three rooms:



Since there’ll be more rooms and exits, I’ll keep adding onto this. For future readers, this is my base programming for a game with multiple rooms, at least one with more than one door and Scene Change function.

My main two variables are DiabloRoomPos (the room the player is in) and LastRoom (the room the player was before).
Every time the player enters a new room, DiabloRoomPos changes to the number I assigned to the room. When the player leaves the room, LastRoom changes to the number of the room the player just left.
LastRoom is important for rooms with more than one entry. I use it to change the player’s position to where they came from.

For example: the Kitchen has a hall that goes to the left (going to the Office) and a hall that goes to the right (going to the Diningroom). I can use the LastRoom Global variable to record which of the two rooms the player came from and assign them positions in the kitchen. If the player entered the kitchen from the office, then they’ll appear on the left side of the kitchen, by the hall that leads to the office. If the player enters the kitchen from the diningroom, then the player will spawn at a different position in the kitchen, beside the hall that leads to the dining room.

(None of the global variables need to be children as far as I know. I just made a couple into Children variables to keep things neat.)

1 Like