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

SOLVED Random Room switching

yvodlyn

Member
Hello everyone, I would like to simplify this code. What I want to do is change rooms randomly and the next room is not the same as the current room.
Here my code:

GML:
if (point_distance(o_player.x,o_player.y,o_portail.x,o_portail.y) < 8){
   if (room == rm_player_1){
       room_goto(choose(rm_player_2,rm_player_3,rm_player_4,rm_player_5,rm_player_6));
   }
   if (room == rm_player_2){
       room_goto(choose(rm_player_1,rm_player_3,rm_player_4,rm_player_5,rm_player_6));
   }
   if (room == rm_player_3){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_4,rm_player_5,rm_player_6));
   }
   if (room == rm_player_4){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_3,rm_player_5,rm_player_6));
   }
   if (room == rm_player_5){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_3,rm_player_4,rm_player_6));
   }
   if (room == rm_player_6){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_3,rm_player_4,rm_player_5));
   }
}
 

Evanski

Raccoon Lord
Forum Staff
Moderator
use a switch statement
Code:
switch(room)

{

    case rm_player_1: room_goto(choose(rm_player_2,rm_player_3,rm_player_4,rm_player_5,rm_player_6)); break;


    default: break;

}
seeing as no matter what room the player is in you could just do one line having a room chosen and saved to a variable
then compare the variable to the current room and if its different go to the room otherwise choose a different room
 

jo-thijs

Member
Hello everyone, I would like to simplify this code. What I want to do is change rooms randomly and the next room is not the same as the current room.
Here my code:

GML:
if (point_distance(o_player.x,o_player.y,o_portail.x,o_portail.y) < 8){
   if (room == rm_player_1){
       room_goto(choose(rm_player_2,rm_player_3,rm_player_4,rm_player_5,rm_player_6));
   }
   if (room == rm_player_2){
       room_goto(choose(rm_player_1,rm_player_3,rm_player_4,rm_player_5,rm_player_6));
   }
   if (room == rm_player_3){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_4,rm_player_5,rm_player_6));
   }
   if (room == rm_player_4){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_3,rm_player_5,rm_player_6));
   }
   if (room == rm_player_5){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_3,rm_player_4,rm_player_6));
   }
   if (room == rm_player_6){
       room_goto(choose(rm_player_1,rm_player_2,rm_player_3,rm_player_4,rm_player_5));
   }
}
GML:
if (point_distance(o_player.x,o_player.y,o_portail.x,o_portail.y) < 8){
    var r;
    do{
        r = choose(rm_player_1,rm_player_2,rm_player_3,rm_player_4,rm_player_5,rm_player_6);
    until r != room;
    room_goto(r);
}
 
Top