GameMaker Studio 2 – Struct Based Room Switching

Get current room name via a struct.

This struct based method gets the current room name you are in when in game. You can then pass it to a variable that detects what room you are in with one line of code, which allows easy code execution dependent on which room you are in.

  • Start GameMaker 2.3+ and create a new project.
  • In the “Asset Browser”, select the “Rooms” folder.
  • Select the first room there, there should be only one named “Room1”.
  • Rename the room to “init”. This will be the initialization room where we put controller instance objects.
  • In the “Room Editor” properties tab click “Creation Code” and add the following to it in the code window:
    if ( instance_exists(oGameController) )
    {  
        room_goto(TitleScreen);
    }
  • Create two new Rooms. One called “TitleScreen” and one called “GameScreen”.
  • Change the background colour of the TitleScreen to grey and the background colour of the GameScreen to blue. This way, you will have a visual change when rooms switch.
  • In the “Asset Browser”, find the “Objects” folder and right click and create a new object name “oGameController”.
  • Go back to the “init” room and select the “instances” layer with the “Room Editor” tab/sidebar.
  • Drag the newly created “oGameController” object into the room on the “instances” layer.
  • In the “Asset Browser” double-click the “oGameController” object which will open it in the Workspace.
  • In the “oGameController” settings, find and tick the persistent checkbox. This will make the “oGameController” remain persistent when switching rooms.
  • In the “oGameController” Events tab, click the “Add Event” button and select “Create” as the event to add.
  • Click the “Add Event” button again and add a “Step” event.
  • In the events tab, double-click the “Create” event which will open up the event in the code window.
  • In the “Create” event’s code window, enter the following:
cScreen =
{
    roomName : room,
    currentScreen : function(_cRoom)
                    {  
                        _cRoom = room_get_name(roomName);
                        var asset = asset_get_index(_cRoom);
                        return asset;
                    }
};

In the “Events” tab, double-click the “Step” event which will open up the event in the code window.

In the “Step” event’s code window, enter the following:

cScreen.roomName = room;

if ( cScreen.currentScreen(cScreen.roomName) == TitleScreen )
{  
    if ( mouse_check_button_pressed(mb_left) )
    {
        room_goto(GameScreen);
    }
}
if ( cScreen.currentScreen(cScreen.roomName) == GameScreen )
{
    if ( mouse_check_button_pressed(mb_left) )
    {
        room_goto(TitleScreen);
    }  
}

Now run the app and it will start in the TitleScreen room. Press the left mouse button to go to the next screen, which is the GameScreen. Press it again to go back to the TitleScreen.

I know this is very basic, but it will be a sort of first step in introducing you to Structs in GameMaker Studio 2.3+.

Enjoy!

One Comment:

  1. Pingback:The Blog – Amon.Pro

Leave a Reply

Your email address will not be published. Required fields are marked *