• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need help switching between rooms

V

Vincent

Guest
Hello, I need some help. I want to go through a hole in room 1 of my game and appear in room 2, then I
programmed another place in room 2 to appear in room 1. Is there a way to appear in room 1 in the spot you went to room 2?
 

jo-thijs

Member
Hi and welcome to the GMC!

Yes, there is.

As the answer on your previous topic explained to you,
you can use global variables to keep track of values when switching rooms.

Global variables are variables that are shared by all instances, objects and rooms.
You use a global variable by putting "global." in front of the name of the variable, like so:
Code:
global.myvar = 5;
global.myvar will now be 5 everywhere, in every room.

To spawn at the same height in a different room, you would store the y coordinate of the player in a global variable
and restore it in the other room.

For a more specific example of how to do this,
I would like to ask how the holes are positioned in the room (at the very edge of the room? always horizontally? always at the same height?)
and what the sizes of the rooms are (always the same height?).
 
V

Vincent

Guest
Hi and welcome to the GMC!

Yes, there is.

As the answer on your previous topic explained to you,
you can use global variables to keep track of values when switching rooms.

Global variables are variables that are shared by all instances, objects and rooms.
You use a global variable by putting "global." in front of the name of the variable, like so:
Code:
global.myvar = 5;
global.myvar will now be 5 everywhere, in every room.

To spawn at the same height in a different room, you would store the y coordinate of the player in a global variable
and restore it in the other room.

For a more specific example of how to do this,
I would like to ask how the holes are positioned in the room (at the very edge of the room? always horizontally? always at the same height?)
and what the sizes of the rooms are (always the same height?).
Dibujo.jpg
 

jo-thijs

Member
That's a bit odd, as the gap in room 2 is 3 blocks wide and in room 1 only 2 blocks wide.
This can lead to collision issues.
 

Nux

GameMaker Staff
GameMaker Dev.
without the use of global variables, you could make your player object persistent and then in the player's "room start" event check if the player is at the bottom of the screen, then you will set their position to the top of the screen, otherwise, set them to the bottom.
example code:
Code:
var centre_y = view_yview + ( view_hview / 2 );
if (centre_y > y) // start at top of screen
{
    y = view_yview + view_hview; // move them to the bottom of the screen
}
else
{
    y = view_yview; // move them to the top
}
image:

The pink room is a separate room from the grey room
 
V

Vincent

Guest
That's a bit odd, as the gap in room 2 is 3 blocks wide and in room 1 only 2 blocks wide.
This can lead to collision issues.
The idea is that if I enter in Room 2 I appear on top, and if I enter in room 1 from room 2 I apear in that gap
 
V

Vincent

Guest
without the use of global variables, you could make your player object persistant and then in a room start event check if the player is at the bottom of the screen, then you will set their position to the top of the screen, otherwise, set them to the bottom.
example code:
Code:
var centre_y = view_yview + ( view_hview / 2 );
if (centre_y > y) // start at top of screen
{
    y = view_yview + view_hview; // move them to the bottom of the screen
}
else
{
    y = view_yview; // move them to the top
}
image:

The pink room is a separate room from the grey room
How does that work? (sorry but I don't know much about gml)
 

jo-thijs

Member
@Nux, that would work if the rooms have the same width and the exact x coordinate should be maintained, but the screenshots suggest this is not the case.
@Vincent, I know, but the width of your gap is inconsistent. Is it 2 or 3 blocks wide?
 

Nux

GameMaker Staff
GameMaker Dev.
How does that work? (sorry but I don't know much about gml)
Basically, persistence allows the object to not be deleted when changing rooms, this also means that it will be in the exact same position every time you change the room, so, instead you want to to theoretically flip the y coordinate from the bottom to the top. if they're at the bottom half of the screen. they're moved to the top, if they're at the top half of the screen, they're moved to the bottom.

also by the looks of your screen shot you don't want this, so i recommend having an object or set of coordinates that you set the player x and player y to once the room is loaded
e.g.
Code:
in room start:
x = targetX;
y = targetY;
 
V

Vincent

Guest
@Nux If I do the gaps aligned and with the same width, will that trick work?
@jo-thijs
 
Last edited by a moderator:

Nux

GameMaker Staff
GameMaker Dev.
yes it should, but i don't recomend it if you're going to have scrolling rooms like limbo or some other puzzle/platformer.
it's perfect for stationary views like in undertale
 
V

Vincent

Guest
@Nux The idea is to make lots of rooms. In each one you can see the whole room, it's not a scrolling game
 

Nux

GameMaker Staff
GameMaker Dev.
since I don't know how your game works I can't give you whole code but the code that switches the y coords I already posted here, and should be placed in the Room Start event
 
V

Vincent

Guest
@Nux
var centre_y = view_yview + ( view_hview / 2 );
if (centre_y > y) // start at top of screen
{
y = view_yview + view_hview; // move them to the bottom of the screen
}
else
{
y = view_yview; // move them to the top
}
Is this the code I have to use?
 

Nux

GameMaker Staff
GameMaker Dev.
yes it is, but if you can't tell then it's probably not the best thing to jump into. try attempting something different and build yourself up instead of blindly copy-pasting :>
 
P

Piece0pie

Guest
So, clearly there are many answers to this question. If it were me I would consider using 2 spawn objects in each room. 1 for the first time entered and the other for re-entry. It has been a while since I have done anything outside of GML and I am at work right now so I don't have access to GameMaker to try it but you should be able to do this without using any GML.

Basically you would need to setup a variable that would track if the player was re-entering each room, then have a conditional in each spawn object that would dictate which one would create the player. You would place these objects where you wanted the player to spawn depending on where they are coming from.

Please consider the following:
In the menu for the game (or somewhere prior to the first level) have an object (which could be invisible) that creates some global variables in the creation event, such as:

global.level1_been_here = false; (you can use the set variable drag 'n drop instead of code)
global.level2_been_here = false;
global.level3_been_here = false;

Then create 2 objects: spawn1 and spawn2 and place them respectively in your first level. (and 2nd, 3rd, etc)

For spawn1 in the create event create 2 nested conditional statements... ( wrote them out here but again you can use drag and drop for this)

if current room is room1 and if global.level1_been_here = false
then create and instance of the player at x,y (of spawn1)
(then you would also add detection for the other rooms as well)

respectively in the spawn2 object

if current room is room1 and if global.level1_been_here = true
then create and instance of the player at x,y (of spawn2)

In the player object in the step event have it detect what room the player is in.
if current room is room1 then global.level1_been_here = true;
(then you would also add detection for the other rooms as well)

In spawn2 you would also want to set all global.level_been_here(s) to false. That way when you re-enter the next room it puts you at spawn 1 again.

and voila your player should now be able to move between levels, entering and exiting them where you choose!
 
Last edited by a moderator:
Top