• 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!

GameMaker [SOLVED] Issue with the room_goto() function

T

Toldo171

Guest
Hello everybody,



I am new to GameMaker. I have been programming a management/card game for the past 2 months, and right now I have an issue that I cannot solve by myself. That is why I need your help.

What I am trying to do

It is very simple. I want my hero to switch room when he collides with a door. I have:

  • My hero obj_Hero

  • My doors obj_Door

  • 2 rooms, rm_House and rm_Office
What I did

Inside obj_door, I have a create event and a collision event with obj_hero :

Create event

var targetRoom = 0;

var x_target = 0;

var y_target = 0;

Collision event

room_ goto(targetRoom);

obj_Hero.x = x_target;

obj_Hero.y = y_target;

Then, inside the door instance of each room, I set up the following values:

rm_House door instance

targetRoom = rm_Office;

x_target = 1024;

y_target = 670;

rm_Office door instance

targetRoom = rm_House;

x_target = 800;

y_target = 600;

What it does ingame

The switching room thing works, but my hero is not setup at the right coordinates, instead, he is set up at random coordinates on the top left of the screen. For example, x=-16 y = 254, or x=6 y=211…

Notes

My hero obj_hero is persistent.

I only put an obj_hero instance in the first room, not in the second room.

When the random coordinates are both positives, I can see my hero on the top left on the screen, and I can control it, which means it is not a layer issue.

When I run the game in debugging mode, the new coordinates of my obj_hero are changed to x=1024 y=670 when I hit the door, but only for 1 frame. Then I go step by step, and all of a sudden the coordinates changes to random numbers before the displaying of the other room.

The coordinates are random, but always in the same range of values. For example, x is always between -15 and 10.



Do you have any idea of what is going on?

Thank you very much for your help.
 

Relic

Member
do you have any collision handling with other objects? When you collide with the door and change room, the remainder of the event code will run before changing rooms.

To be clear, I don’t know if that means all collision code for all objects would run or just the code in this collision event, but it might explain why you character is moved to the correct spot, but will be moved again before the room changes.

Try setting target x and target y in the player instance when it collided with the door, then have the player move in the room start event to its target x and target y coordinates
 
T

Toldo171

Guest
Thank you for your answer.

I have other collision handling in my project, but I don't have any other collision happening during that specific frame.

To be clear, the position of my hero is randomized before the new room loads, so when it loads, my hero is already at the top left of the screen.

I don't have access to GMS right now. Tomorrow, I will try to set the position of the hero after the room loads, and I will let you know.
 

TheouAegis

Member
Move the player object down in the resource list and then try again.

You said you can see it change position when debugging. Find out which event it is in when it changes.

Do you have code in the Room Settings?

Do you have a Room Start event in any objects?

You said it is random, so find any code that has to do with random positions.



Post ALL of your player's code if none of those help.
 
T

Toldo171

Guest
Hello TheouAegis,

Thank you for your help.

I think I found where is the issue. When my player object collides with the door, the "moving event" of the player is still running :

Code:
if (x_heroDestination != mouse.none && y_heroDestination != mouse.none && global.action = false)
{
    scr_MoveHero(x_heroDestination, y_heroDestination, grid);
    x_heroDestination = mouse.none;
    y_heroDestination = mouse.none;
}
This piece of code launches a script which creates a path for the player to follow.

I think that when the room changes, the player is still moving, and its position is reset to x=0 y=0 in the new room.

I will try to stop this "moving event" before the room switch, and I will let you know tomorrow.
 
T

Toldo171

Guest
Hello,

I have solved my issue. Instead of triggering the room switch in the collide event, I did it in the step event, when my player object has finished to go through its path.

Basically, it looks like this in the door step event :

Code:
if (mouse_check_button_pressed(mb_left))
{
     scr_MoveToAction();
     wantToSwitchRoom = true;
}

if (obj_Hero.path_position = 1 && wantToSwitchRoom = true)
{
    room_goto(targetRoom);
    obj_Hero.x = x_target;
    obj_Hero.y = y_target;
}

...
Thank you everyone for your help. This thread is solved.
 
Top