# Room_goto_name, Room_goto_random And More

2 replies to this topic

### #1 torigara

torigara

GMC Member

• GMC Member
• 6483 posts

Posted 29 May 2009 - 12:49 PM

These request often come up in Q&A forums, and people usually comes with a solution of executing a string (despite of its drawbacks) or a roundabout method that counts an arbitrary range of numbers to figure out which are valid room indices. Here, I'd like to spotlight a set of in-built functions that gives you all of existing rooms without guesswork.

room_goto_name(name)
This takes you into the room with the specified name (given as a string.) The return value tells whether the room exists or not.
```var r;
for (r = room_first; r >= 0; r = room_next(r)) { // This is not room_goto_next()!
if (room_get_name(r) == argument0) {
room_goto(r);
return true;
}
}
Typical usage:
```name = get_string("What room do you want to go to?");
if (!room_goto_name(name)) {
show_message("No such room");
}```

room_goto_random()
This takes you into a random room.
```var r, rooms, count;
count = 0;
for (r = room_first; r >= 0; r = room_next(r)) {
rooms[count] = r;
count += 1;
}
room_goto(rooms[floor(random(count))]);```

room_skip(n)
When n is a positive number, this function moves you n rooms forward (in the order of the room list.) So, room_skip(1) is identical to room_goto_next(), and room_skip(2) brings you to the next room of it. When n is negative, it goes to previous rooms. For example, use room_skip(-3) to go to the previous room but two. The return value indicates whether there was a room at the specified location or not.
```var r;
r = room;
if (argument0 > 0) {
repeat (argument0) r = room_next(r);
}
else if (argument0 < 0) {
repeat (-argument0) r = room_previous(r);
}
if (room_exists(r)) {
room_goto(r);
return true;
}
else {
return false;
}```
Note: these functions won't work for dynamically added rooms. However, in such a case you may have your own list of room indices.

Edited by torigara, 01 June 2009 - 03:13 AM.

• 1

### #2 lballaty

lballaty

GMC Member

• New Member
• 83 posts

Posted 09 December 2011 - 06:18 AM

Really super, thanks for this. Helped me understand tremendously about room manipulation.
• 0

### #3 BPO_Jeff

BPO_Jeff

GMC Member

• GMC Member
• 7 posts
• Version:GM:Studio

Posted 27 July 2012 - 10:28 AM

Just wanted to say an epic thankyou for this, you are my superhero lol I was trying to find a non-"execute_string" way to set up a ton of buttons for a level select screen and having a devil of a time wrapping my head around how to go about it. This makes total sense, thanks a lot!

- Jeff
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users