Issue with traveling to random rooms

Hi, GameMaker Studio 2 noob here! After looking searching some threads to try to learn how to make a script that will send the player to a random room from a list of four rooms, I ended up with this:
GML:
room_goto(choose(asset_get_index("Room2"), asset_get_index("Room3"), asset_get_index("Room4"), asset_get_index("Room5")));
show_debug_message(room);
The issue is I keep getting sent to Room2. I've made sure Randomise(); is used on game start. I've also tried removing Room2 from the list of rooms as a whole, but the game didn't even launch. This is literally my second day using GameMaker Studio 2, could someone please help?
 

chamaeleon

Member
You don't need to use asset_get_index()
GML:
room_goto(choose(Room2, Room3, Room4, Room5));
is perfectly fine. Every resource name for all types, rooms, objects, sprites, etc., are identifiers with the asset index value of the corresponding asset.

Personally, I'd either put a show_debug_message() by before randomize(), and one before room_goto() and make sure the randomize() call is called before the room_goto(), or use a breakpoint on both lines, and see where the debugger stops first.
 
Just tested with what you recommended, they are being called in the correct order. However it is still only going to Room2.
 

chamaeleon

Member
Just tested with what you recommended, they are being called in the correct order. However it is still only going to Room2.
The room represent the current room in which code is executing. The room switch won't happen until the end of the currently executing event. I'm not sure why you see Room2 instead of Room1 though, if this code is running in an instance placed in Room1, unless persistence is involved. If you're 100% sure all is as it should be, perhaps hit the broom button to clear out any generated files which may be confusing the project compilation.

In a test project I get this behavior
GML:
room_goto(Room2);
show_debug_message("Room = " + room_get_name(room));
Code:
Room = Room1
 
Hold up, there's some weird stuff going on. Firstly, I forgot to mention that this script is being run on the end step event. I don't know if that affects anything but I mention it just in case (note: I've made it run on create instead before, and got the same results). Another thing I should mention is that I'm checking which room is which via which number is in it. I made some empty objects that are numbers and then placed them in the corresponding room.

Now for the weird part, I tested with that debug message here are my results:

First attempt:
Room = Room1
Room = Room5
Room = Room5

Second attempt:
Room = Room1

Third attempt:
Room = Room1
Room = Room4

Fourth attempt:
Room = Room1


I put the debug message line in the end step event with the rest of my code.

Edit just got this one:
Room = Room1
Room = Room4
Room = Room4
Room = Room4
Room = Room4
Room = Room4
Room = Room3
Room = Room5
Room = Room5
Room = Room5
Room = Room3
Room = Room3
Room = Room4
Room = Room5
Room = Room3
Room = Room5
Room = Room3
Room = Room5
Room = Room4
Room = Room5
Room = Room3
Room = Room3
Room = Room4
Room = Room5
Room = Room5
Room = Room4
Room = Room4
Room = Room4
 
Last edited:
Even weirder is the fact that the object shouldn't even exist in the rooms it lists (excluding 1). I'm assuming it has to do with this: " Note that the room will not change until the end of the event where the function was called, so any code after this has been called will still run. " -from the manual
 

chamaeleon

Member
Given a persistent object (didn't feel like having multiple objects involved)
Create
GML:
randomize();
End step
GML:
if (room == Room1) {
    room_goto(choose(Room2, Room3));
}
Room start
GML:
show_debug_message("Current room = " + room_get_name(room));
Results in
Code:
Current room = Room3
and
Code:
Current room = Room2
randomly.

If you have code not shown that interferes, persistent objects, same object placed in multiple rooms, etc., I couldn't speculate on what happens. All I know is that the principle works just fine. If you have more code later on in the event that is also room_goto() related, that code might execute (just like how show_debug_message() is executing rather than the event stopping at the call of room_goto()...)
 
Just replacing
GML:
room_goto(choose(Room2, Room3, Room4, Room5));
with
Code:
if (room == Room1) {
    room_goto(choose(Room2, Room3));
}
seems to have worked. Thanks for all the help!
 
Sorry, one last thing, getting an error: Variable Object1.Room1(100004, -2147483648) not set before reading it.
Should I make Room1 a string or some other type of variable

Edit: Forgot I changed the name of Room1, whoops.
 
Last edited:
Top