How to get Gamemaker to remember the last room you were in?

I am trying to get Gamemaker to remember the last room I was in so that every time I enter a room I can choose the starting location based on what direction my character came in from. My first idea was labeling the lastroom = room right before I transitioned from room to room, but the first level didn't except that since I hadn't properly defined lastroom to it. Is there a specific way I have missed, to do this?
 

The-any-Key

Member
So make sure lastroom=noone, when you are in the first room. Create a persistent control object that hold this variable from the start of the game. Tips is to have a init room that then go to the game room. To make sure the control is created first in the init room.
 

Relic

Member
A controller object is just another object, like your player object or walls or enemies, but rather than being assigned sprites and movement it has code that helps control other parts of the game.

In the context of your problem, this control object would hold one key bit of code- in the room end event, set a variable to store the room you are leaving.

last_room=room

If you ever want to know what the last room was, you can refer to this object and its variable. E.g.:

if obj_room_controller.last_room= room_level1 {
//do something
}

Just be also sure to follow The Any Key’s advice about persistent objects- you need this controller object to appear in all rooms and not lose its variable.
 

The-any-Key

Member
A controller object is just a ordinary object. But you plan to run sertain code or have it as a central for important variables. You set it as persistent so it exist in even if the room change.
 
My persistant object says lastroomin = noone; when the game starts, says lastroomin = room; when the room ends, and room0 tells where to move my character if lastroomin == room1. I seriously can't figure out where the issue is.
 

NeZvers

Member
use room_end event to write lastroom = room;
So when you are leaving the room it marks down which room it was or if you have room you don't want to be written you can simply do:
Code:
if(room != notThisRoom)
{
   lastroom = room;
}
 

Relic

Member
The error is telling you that you are trying to read a variable that does not yet exist.

The variable is oldMan.lastroomin.

This occurs in the line of code: if lastroomin=room1

Is your controller object oldMan? Is it persistent? Or is your controller another object and you have mistakenly tried to get the variable value within oldMan?
 
My controller object is named last_room. oldMan is the player character and I may have mistakingly placed the order for him to move inside his creation code. What is the proper way to fix this mistake?
 

NeZvers

Member
Why not make lastroomin a globalvar? So variable persistent and you don't have to worry about instances that doesn't exist.
 

chamaeleon

Member
My controller object is named last_room. oldMan is the player character and I may have mistakingly placed the order for him to move inside his creation code. What is the proper way to fix this mistake?
If your controller object is last_room, and Old_Man is another object that does not have the instance variable lastroom, why are you referring to lastroom as if it was an instance variable of Old_Man in your code instead of properly using last_room.lastroom?
 
Top