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

Windows How to go to a Random Room in 8.1

A

ApolloStephens

Guest
hey this is my first thread and its just a quick simple question basically I have around 50 rooms and I want have the first room with a button and when I click it I want to go to a random room out of those 50, does anyone know how to code this?

for example I want to click it and randomly be on 23

if anyone can help me that would be great! :D
 

TheouAegis

Member
A couple ways.

Method 1) Add all your rooms to a global array at the start of the game. When you click on the button, pick a random integer (so if you have 50 rooms, use irandom(49) to pick a random number from 0 to 49). Then goto the room indexed in the array at that number.


room_goto(RoomList[irandom(49)]);


Method 2) Put all your actual game rooms in order. Make sure you're done adding rooms to your game, otherwise you'll have to repeat this process each time you add/remove rooms. Export all the resources in your game to a gmres file. Create a new project. Import that gmres file into the new project. Save the new project. Now do the same thing as above with a random number, but this time add it to the first game room.

room_goto(Level_1 + irandom(49));
 

matharoo

manualman
GameMaker Dev.
Other than what @TheouAegis mentioned:
Suppose you have your room names like this:
room1
room2
room3
....
room50

You can easily access them using asset_get_index.

Code:
var roomno = irandom_range(0,50);
var rm_go = asset_get_index("room"+string(roomno));
room_goto(rm_go);
So basically, asset_get_index() converts a string to the asset index. Here, it would take a string (ex, room4) and make it into a usable value so that the actual room can be accessed.

@J.J. In any code where you want the room to change, simple
 

Roderick

Member
room_goto(Level_1 + irandom(49))
Does choose() exist in 8.1? If so, you could use

room_goto(choose(level_1, level_2, level_3));

It would get bulky with a large list, but you wouldn't have to worry about forcing the rooms into a specific order.
 
J

J.J.

Guest
Other than what @TheouAegis mentioned:
Suppose you have your room names like this:
room1
room2
room3
....
room50

You can easily access them using asset_get_index.

Code:
var roomno = irandom_range(0,50);
var rm_go = asset_get_index("room"+string(roomno));
room_goto(rm_go);
So basically, asset_get_index() converts a string to the asset index. Here, it would take a string (ex, room4) and make it into a usable value so that the actual room can be accessed.

@J.J. In any code where you want the room to change, simple
@matharoo This works like a charm and is really easy also. Clever scripting!
 

607

Member
J

J.J.

Guest
@matharoo, I used the code, but everytime I start the game, the same order of rooms apear in the exact same order everytime.

Do you have a solution for this?
 
J

J.J.

Guest
randomize();
var roomno = irandom_range(1,50);
var rm_go = asset_get_index("room"+string(roomno));
room_goto(rm_go);

This is it! Nice :)'

thank guys!
 

matharoo

manualman
GameMaker Dev.
@matharoo, I used the code, but everytime I start the game, the same order of rooms apear in the exact same order everytime.

Do you have a solution for this?
That's because the random values that come up in a GM game are always the same, unless specified by randomize(), which makes debugging easier.
 
Top