← All Workshops

MudEngine Part 7: Save & Load

Persist the World

In Part 6 the game world (room names, descriptions, exits) was hardcoded in static arrays in room_data.rs. Every time the server restarts, all state resets — rooms and connected players are gone.

In this part we make the game data-driven again, following the pattern from Part 3 — but with the game state split across two files with very different roles:

  • Game.toml — the static world: the 9 rooms, their names, descriptions, and exits. This defines the game, so it is committed to version control and never written by the server.
  • Players.toml — the live state: every connected player and their position. The server writes it back as players join, move, or disconnect, so it is gitignored — machine-generated session data that must not pollute your history.

These two files exist because the two kinds of data have opposite lives. The world is fixed — you edit it by hand and commit it. The players are ephemeral — they connect, move, and disconnect while the server runs, and their positions change thousands of times. Mixing them (as a single Game.toml would) means every gameplay action creates churn in a file you want to keep stable and reviewable.

This part leans on serde, the toml crate docs, and LazyLock for one-time initialization.

We also refactor into a proper module structure: a new persistence module keeps TOML parsing, persistence, and game-state types in their own file instead of cluttering main.rs.

This gives us two things: the world definition is editable without recompiling, and player positions are written to disk so a server restart can pick up where the previous run left off.

Architecture

Game.toml ────read──▶  rooms        ─┐
                                     ├──▶ GameState { rooms, players }
Players.toml ─read──▶  players      ─┘
                                │
                    ┌───────────┼───────────┐
                    ▼           ▼           ▼
                Player join  Player move  Player leave
                    │           │           │
                    └───────────┼───────────┘
                                ▼
                    snapshot() ──▶ Players.toml
                    (autosave tick, default 60 s)

On the client side, the hardcoded ROOM_NAMES and ROOM_DESCS statics disappear. The server now sends room data as part of the initial State message, and the client renders room names from that data.

"server startup" "server startup" "every AUTOSAVE_INTERVAL" "initial State message" Game.toml static world: rooms load_world() Players.toml live state: players load_players() GameState { rooms, players } snapshot() + write_players() Browser room names + descriptions