How the game remembers
What it means to 'save' in a web page, and why the game is careful about what it writes down.
Close the game, come back later, and your run is still there. How does a web page remember things after you leave? And why does this game go out of its way to be careful about what it writes down? Two good questions with instructive answers.
Concept: "persistence" / saving. By default, a web page forgets everything the moment you close the tab. To remember, it has to deliberately write information somewhere lasting. Browsers give pages a little storage box for exactly this, called localStorage — a small notebook the page can jot notes in and read back later.
This game keeps four separate notes in that box:
| Note | Remembers |
|---|---|
| Your saved run | the game in progress |
| Achievements | which badges you've earned |
| Local leaderboard | your scores, when there's no shared server |
| Settings | sound on/off, side panels open/closed |
Saving carefully: don't write down everything
You might think saving is just "write down the whole game." But some of what the game holds in memory is temporary junk — half-finished screen animations, and pieces that can't be safely written down at all. So before saving, the game strips those out:
2214 function serializeSave(s) { 2215 var o = {}; 2216 Object.keys(s).forEach(function (k) { if (SAVE_DROP.indexOf(k) === -1) o[k] = s[k]; }); 2217 o.view = "lot"; 2218 return o; 2219 } 2220 function saveGame(s) {
Concept: "serializing." To save something, a program has to turn it into plain text it can write down and read back — that's serializing. The catch: some things (like live animations or functions) can't be turned into plain text. So the game carefully saves only the parts that can, and rebuilds the rest when you return.
The game is also picky about when it saves: only when you're back at the parking lot between shows — never in the middle of a drive or an event. That guarantees every save is taken at a clean, safe moment.
Loading: rebuild what wasn't saved
When you return and load, the game puts back everything it saved, then recalculates the things it deliberately didn't save — including your fuel and food limits (the "derived values" gotcha from the economy chapter). It also double-checks the saved data looks valid before trusting it, so a corrupted save is ignored rather than crashing the game.
Concept: never trust stored data blindly. Saved files can be old, incomplete, or tampered with. Careful programs check that loaded data makes sense before using it. This one confirms your save has a character and a valid position before loading it.
- Persistence means deliberately writing data to lasting storage (the browser's localStorage); pages forget everything otherwise.
- Serializing turns state into saveable text — but not everything can be saved, so the game strips temporary bits and rebuilds them on load.
- Good programs save at clean moments and validate data before trusting it.