Molten

Screens and the screen stack

A screen is what the game is doing right now: the main menu, playing, paused, game over. A screen decides which level is loaded (if any) and which UI is shown. Screens live in screens/<name>.screen; the screen's name is the file name in PascalCase (main_menu.screen is MainMenu).

// screens/playing.screen (from samples/gridline)
export default screen({ level: "valley", ui: ["ui/hud"] });
Key Meaning
level A level name, or null for no level
ui UI files to show, relative to the game root, with or without .ui
logic A Cast file, relative to the .screen file, whose onEnter, onExit and update run for this screen (see Screen logic in Cast)

game.toml's first_screen is the screen the game starts on.

The stack

Screens form a stack. The top screen is the active one.

Cast call Effect
switchScreen("Playing") Replace the whole stack with Playing. Loads its level (or unloads the current level if it has level: null), every time
pushScreen("Pause") Put Pause on top. The level is not touched
popScreen() Remove the top screen; the one below continues untouched
screenIs("Playing") true if Playing is the top screen

Changes happen at the end of the current tick. switchScreen and pushScreen return false if the screen doesn't exist.

  • Every screen on the stack draws its UI, bottom to top. Only the top screen receives UI input.
  • A pushed screen with a level other than null logs a warning; pushing never changes the level.
  • Each screen on the stack has its own screen arena, freed when that screen leaves the stack. Memory allocated with new(screen) survives while another screen covers it.

Screen logic in Cast

Behaviors run every tick whatever screen is on top. To make gameplay depend on the screen, check it:

if (screenIs("Pause")) {
    if (keyPressed("Escape")) popScreen();
    continue;
}
if (!screenIs("Playing")) continue;

UI buttons switch screens through exported Cast functions (onClick={play} calling switchScreen("Playing")). See the tutorial's step 6.

A screen can declare logic: "./main_menu.cast" for work such as initializing a menu or handling its input. The path is relative to the .screen file. The module can export onEnter(): void (after the screen's level loads), onExit(): void (before it unloads) and update(dt: float): void (once per fixed tick while the screen is on top).

import { keyPressed, switchScreen } from "molten";

export function update(dt: float): void {
    if (keyPressed("Enter")) switchScreen("Playing");
}

See the screen file reference for lifecycle hooks.

Escape

By default Escape quits molten-game. Set escape_quits = false in game.toml to use it yourself (for a pause menu), and offer a Quit button that calls requestQuit().

Starting on a particular level

molten-game --start-level yard starts on the first screen whose level is yard (entering it as switchScreen would). If no screen loads it, the game starts on first_screen with that level loaded. Devtools' Play button uses this.

View this page's source on GitHub