MudEngine Part 7: Save & Load
Run it!
Start the fullstack dev server with dx serve. On the first WebSocket connection the server assembles its state from the two files: the world from Game.toml, and any saved players from Players.toml.
Make sure Game.toml exists in the mud-engine project root before starting โ the server panics if it's missing. Players.toml is optional: it is gitignored, so a freshly-cloned repo won't have it and the server simply starts with an empty player list.
dx serve --openBasic play-through (same as Part 6)
Open two browser tabs side by side with different names โ Alice and Bob. They connect, move around the grid, see each other's positions, and disconnect. Everything works the same as Part 6.
Check Players.toml gets populated
With Alice and Bob connected, open Players.toml:
[[players]]
id = "550e8400-e29b-41d4-a716-446655440000"
name = "Alice"
room = 4
[[players]]
id = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
name = "Bob"
room = 1
After Bob moves to Hilltop (room 1), re-read the file โ Bob's room field has changed. Now open Game.toml โ it is untouched. The server never writes the world file.
Server restart behaviour
- With Alice and Bob in the world, stop the server with Ctrl+C
- Start it again with
dx serve --open - Refresh Alice's tab โ she gets a fresh connection with a new ID
- Open
Players.tomlโ the player entries from the previous session are gone (they were live connections)
What IS persisted
- The world is always loaded from
Game.toml. You can edit room names, descriptions, or exits, then restart the server to see the changes without recompiling โ this file is committed and versioned - Player positions are written to
Players.tomlafter every join, move, and leave โ but this file is gitignored session data, not part of history
Change "Forest Path" to "Enchanted Glade" in Game.toml, restart, and the grid will show the new name.
What is NOT persisted across restarts
Player entries are live session data. When the server stops, all players disconnect and Players.toml is saved with those entries removed (the disconnect handler fires). On restart, Players.toml starts empty โ that's the intended behaviour for a session-backed MUD. Only the world in Game.toml survives because it was never meant to change.
Server panics on startup โ "Failed to read Game.toml"
โ Make sure Game.toml exists in the mud-engine project root (same directory as Cargo.toml). The world file is mandatory. Players.toml is the opposite โ if it doesn't exist, load_players() simply returns an empty player list.
"cannot find type RoomInfo" or "cannot find type TomlWorld"
โ RoomInfo, PlayerInfo, Exit, and TomlWorld are defined in src/game/persistence.rs. Make sure the module is declared in src/game.rs (pub mod persistence;) and that messages.rs re-exports RoomInfo and PlayerInfo with pub use super::persistence::{PlayerInfo, RoomInfo};.
Grid cells show empty names
โ The State message includes room data. Check the message receive loop handles rooms from the State payload โ the room map must be populated from ServerMessage::State { rooms: r, .. }.
"failed to resolve: use of undeclared crate or module toml"
โ Add toml = "1.1" to your Cargo.toml dependencies and re-run dx serve.
Movement still uses old EXITS
โ Remove the old pub const EXITS block from src/game/room_data.rs and replace the movement validation with persistence::can_move(). If room_data.rs becomes empty, delete the file and its pub mod room_data; declaration in src/game.rs.
"error[E0061]: this function takes 2 arguments but 3 arguments were supplied"
โ The ServerMessage::State variant now has a third field rooms. Make sure all pattern matches (both the send and the receive sides) include the new field.
"E0433: failed to resolve: use of undeclared crate or module persistence"
โ Add pub mod persistence; to src/game.rs. The functions are called via super::persistence::* (or crate::game::persistence::*) from other modules.