Tutorial: Meadow Quest (2D RPG)

15. A main menu

Replace "press Space" with a real menu: a New Game button and a language picker, driven by the editor's built-in game-menu system.

The main menu: the logo, New Game and a language picker over the meadow

The title card said "press Space". Now the game opens on a menu you can click: New Game starts playing, and a dropdown switches the whole game between English and German.

How it works

The engine has a game-menu system, GameMenuModel. Buttons talk to it with a UiCommand node, and it shows or hides UI elements by name:

Element nameShown when
MainPagethe menu is open (at the start)
HudPagethe game is running (after New Game)

The game.new command switches from the menu to the game; the language picker uses setting.commit:ui.lang and settings.apply. You only need to name your panels right; the system is added to the scene by itself as soon as a UI script uses UiCommand.

How to build it

  1. A logo without "press Space". Paint mq-logo.png (96×48, the title card with A TINY RPG underneath), or import it from this part's assets. Select Title and click its image slot to pick mq-logo.png.
  2. TitleScreen no longer starts the game. Delete its ActionPressed Jump Branch and the nodes in it, and give the remaining Branch an else that parks the logo at (200, 240):
tgscript
//!tgscript v1
//! script name=TitleScreen
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
    //! node 2 GetGlobal pos=(260,120) global=started
    const n2_value = ctx.getGlobal("started");
    //! node 3 ConstNumber pos=(260,240)
    const n3_value = 0.50;
    //! node 4 Less pos=(460,120)
    const n4_out = (n2_value < n3_value);
    //! node 20 GetGlobal pos=(260,340) global=over
    const n20_value = ctx.getGlobal("over");
    //! node 21 Less pos=(460,340)
    const n21_out = (n20_value < n3_value);
    //! node 22 And pos=(460,220)
    const n22_out = (n4_out && n21_out);
    //! node 5 Branch pos=(660,80)
    if (n22_out) {
        //! node 6 FindEntity pos=(660,260) entity=Camera
        const n6_value = ctx.findEntity("Camera");
        //! node 7 GetEntityPosition2D pos=(860,260)
        const n7_value = ctx.getEntityPosition2D(n6_value);
        //! node 8 SetPosition2D pos=(1060,80)
        ctx.setPosition2D(n7_value);
    } else {
        //! node 9 ConstVec2 pos=(860,400)
        const n9_value = ctx.vec2(200.00, 240.00);
        //! node 10 SetPosition2D pos=(1060,240)
        ctx.setPosition2D(n9_value);
    }
}
//! endregion
  1. The menu page. Select MeadowHud, then UI > Panel. Rename it MainPage, pick the stretch anchor preset, and give it a dark tint with A about 0.35, so the meadow shows through.
  2. The button. With MainPage selected, UI > Button. Rename it MenuNewGame; center anchor preset, Pos Y 250, Width 440, Height 76. Set its label's text to @menu.newgame, a key the engine already translates ("New Game", "Neues Spiel").
  3. The language picker. With MainPage selected, UI > Dropdown. Rename it OptLanguage; center, Pos Y 345, Width 440, Height 64. Its list needs exactly two items, English first, then Deutsch, with the texts @opt.lang.en and @opt.lang.de: the menu system maps the first item to en and the second to de.
  4. The scripts. Create these two and put each in its element's Script slot in the Inspector:

MenuPlay, on MenuNewGame: when clicked, start the game and switch the menu to the game page.

tgscript
//!tgscript v1
//! script name=MenuPlay
//! region graph
//! node 1 OnUiEvent pos=(60,80)
export function onUiEvent(ctx) {
    //! node 2 UiClicked pos=(60,200)
    const n2_value = ctx.uiClicked();
    //! node 3 Branch pos=(260,80)
    if (n2_value) {
        //! node 4 ConstNumber pos=(260,240)
        const n4_value = 1.00;
        //! node 5 SetGlobal pos=(460,80) global=started
        ctx.setGlobal("started", n4_value);
        //! node 6 UiCommand pos=(660,80) command="game.new"
        ctx.uiCommand("game.new");
    }
}
//! endregion

MenuNewGame in the Inspector: the MenuPlay script and its size and position

MenuLanguage, on OptLanguage: when a language is picked, commit and apply it.

tgscript
//!tgscript v1
//! script name=MenuLanguage
//! region graph
//! node 1 OnUiEvent pos=(60,80)
export function onUiEvent(ctx) {
    //! node 2 UiValueCommitted pos=(60,200)
    const n2_value = ctx.uiValueCommitted();
    //! node 3 Branch pos=(260,80)
    if (n2_value) {
        //! node 4 UiCommand pos=(460,80) command="setting.commit:ui.lang"
        ctx.uiCommand("setting.commit:ui.lang");
        //! node 5 UiCommand pos=(660,80) command="settings.apply"
        ctx.uiCommand("settings.apply");
    }
}
//! endregion

Play: the menu opens over the meadow. Pick Deutsch and every key-based text switches at once; click Neues Spiel and the menu gives way to the game. When you lose, the restart brings you back to the menu.

The same menu after picking Deutsch

Good practices

  • Names are the contract. The menu system finds MainPage, HudPage, MenuNewGame and OptLanguage by name. Rename one and it silently stops being managed.
  • Everything shown during play goes inside HudPage. The quest line from Part 13 already lives there, so it stays hidden on the menu without a single extra node.
  • The menu changes pages; your scripts change the game. game.new doesn't load a scene or set your variables. MenuPlay sets started itself, and every gameplay script already waits for it.
  • Use keys for menu text. @menu.newgame comes translated; a literal "New Game" would stay English in every language. Part 16 adds keys of your own.
  • Make the menu readable over the game. A dark, see-through page keeps the world visible and the buttons legible.

This part's assets

MenuPlay.tgscript · MenuLanguage.tgscript · TitleScreen.tgscript · MeadowHud.uidoc · mq-logo.png (the image with its .mt).

Next: languages →