← All Workshops
MudEngine Part 7: Save & Load
Step 17 / 17
Congratulations!
You just added persistent game state to the MUD engine!
What we achieved:
- Data-driven world — the full 9-room grid is defined in
Game.toml, editable without recompiling - Live-state persistence — player positions live in memory and are flushed to
Players.tomlby an autosave task (default: every 60 s) - Clean file split — the static world (
Game.toml, committed) and the live state (Players.toml, gitignored) never mix - Client-server synchronisation — room data is sent from the server to the client in the initial
Statemessage, eliminating the hardcoded statics - Save/load architecture —
persistence::load_world()andpersistence::load_players()at startup,persistence::snapshot()+persistence::write_players()from the autosave task,persistence::can_move()for movement validation from loaded data - Module separation — persistence code lives in its own
game/persistence.rsmodule instead of clutteringmain.rs - Cleanup — the old
ROOM_NAMES,ROOM_DESCS, andEXITSstatics are gone; the game is fully data-driven
Architecture recap
Game.toml ────read──▶ rooms ─┐
├──▶ GameState { rooms, players }
Players.toml ─read──▶ players ─┘
│
┌────────────┼────────────┐
▼ ▼ ▼
Player join Player move Player leave
│ │ │
└────────────┼────────────┘
│ in-memory only
▼
GameState (memory)
│
every AUTOSAVE_INTERVAL
▼
snapshot() + write_players()
│
▼
Players.toml (synced)
What's next?
The dungeon keeps growing! Future parts could explore:
- SQLite database — replace the TOML files with
sqlx+ SQLite for concurrent read/write access - Room state — locked doors, dark rooms that need a torch, writable signs
- Inventory — items the player can pick up, carry between rooms, and drop (saved in
Players.toml, of course) - Chat — add a chat panel where players can send messages to each other
- Monsters & combat — NPCs roaming the world, attack commands, health bars
- Editor UI — an admin panel to edit room data from the browser
The MUD persists! 🏰💾
📚 What we learned
| Concept | How we used it |
|---|---|
serde + toml | Deserialize Game.toml + Players.toml, serialize Players.toml back |
LazyLock<Mutex<T>> | Thread-safe, lazily-initialised globals — the game state and the one-shot autosave task |
persistence module | New src/game/persistence.rs keeps the shared types, GameState, and persistence functions together |
load_world() | Reads Game.toml straight into Vec<RoomInfo> — no conversion, panics on missing file |
load_players() | Reads Players.toml straight into players, tolerates a missing file (empty player list) |
snapshot() + write_players() | snapshot serializes the live state to a TOML string; write_players writes it to Players.toml |
RoomInfo + PlayerInfo | Shared types for TOML, runtime, and wire — re-exported via messages.rs |
| Data-driven design | World definition lives in a data file, not in source code |
| Periodic sync / write-behind | The autosave task flushes in-memory state to Players.toml every AUTOSAVE_INTERVAL, like a filesystem sync |
can_move() | Movement validation using room-ID-based lookup instead of index-based slices |
Step 17 / 17