Persistent Save Data in LÖVE: Sandbox Boundaries and Failure Handling
Design a safe, versioned save system for LÖVE games using love.filesystem, with write validation, trust boundaries, and version-specific API checks.
11 Apr 2026, 21:05 UTC

The problem and the takeaway
Your LÖVE game needs to remember player progress between sessions, but `love.filesystem` does not let you write next to the game executable, and an interrupted save can leave a truncated file that crashes the next load. The useful takeaway is to treat the save directory as the only writable boundary, write one versioned slot on explicit save events, validate every payload on read, and never pass save contents to `loadstring`, `loadfile`, or `dofile`.
Requirements
- Persist player state across game restarts.
- Detect write failures instead of assuming the file exists.
- Reject corrupt or incompatible save files without crashing.
- Keep read-only game assets separate from mutable user data.
Smallest suitable design
Use a single save file named something like `save.json` inside the directory returned by `love.filesystem.getSaveDirectory()`. Write only when the player reaches a checkpoint, opens a menu, or explicitly quits—never every frame. Include a version field in the payload so you can detect old or future save formats.
LÖVE provides no built-in safe table serialization. Choose a format such as JSON or a compact binary layout, and implement `serialize` and `deserialize` as application functions. The example skeleton below assumes those functions exist elsewhere in your code.
local SAVE_FILE = "save.json"
local SAVE_VERSION = 1
local function save_game(state)
local payload = {
version = SAVE_VERSION,
data = state,
}
local ok, message = love.filesystem.write(SAVE_FILE, serialize(payload))
if not ok then
print("Save failed: " .. tostring(message))
return false
end
local info = love.filesystem.getInfo(SAVE_FILE)
if not info or info.size == 0 then
print("Save verification failed: file missing or empty")
return false
end
return true
end
local function load_game()
if not love.filesystem.getInfo(SAVE_FILE) then
return nil
end
local contents = love.filesystem.read(SAVE_FILE)
if not contents then
return nil
end
local ok, decoded = pcall(deserialize, contents)
if not ok or type(decoded) ~= "table" or decoded.version ~= SAVE_VERSION then
return nil
end
return decoded.data
end
Where to run: inside `love.load`, `love.keypressed`, or another LÖVE callback. Required permissions: the OS user account must be able to write to the save directory. Placeholders: replace `SAVE_FILE` and `SAVE_VERSION` with values for your project. Expected checks: `getInfo` returns a table after writing, `size` is greater than zero, and `read` returns the same bytes you wrote. Relevant risks: an interrupted write can leave a truncated file, and `pcall` around `deserialize` catches malformed payloads but does not repair them.
Trust and data boundaries
`love.filesystem.getSaveDirectory()` returns the platform-specific writable user directory. Game code cannot write to its own installation directory or to arbitrary host paths. `love.filesystem.write` and `love.filesystem.read` operate only inside that save directory. This enforces a boundary between read-only assets bundled with the game and mutable user data created at runtime.
Never pass user-controlled save contents to `loadstring`, `loadfile`, or `dofile`. Doing so crosses the data-to-code trust boundary and enables arbitrary execution. Treat the save file as untrusted data until you have validated its structure and version.
Version assumptions and API differences
This guide assumes LÖVE 0.10.x or 11.x. The filesystem info API differs between them:
| LÖVE version | Existence check | Returns |
|---|---|---|
| 0.10.x | `love.filesystem.exists(path)` | Boolean |
| 11.x | `love.filesystem.getInfo(path)` | Table or nil |
If you support both branches, wrap the check in a small compatibility function rather than calling one API directly everywhere.
The save directory also depends on the game identity. Set a distinct identity in `conf.lua` or with `love.filesystem.setIdentity` so saves for different projects do not collide.
function love.conf(t)
t.identity = "mygame_save"
end
Operational checks
- Print `love.filesystem.getSaveDirectory()` in `love.load` on each target operating system.
- Write a known string to a test file, read it back, and compare bytes.
- After a real save, call `love.filesystem.getInfo` on the save path to confirm existence and size.
- Corrupt the save file externally and start the game to confirm your loader returns `nil` or falls back to defaults instead of crashing.
Failure modes
- Disk full or permission limit: `love.filesystem.write` returns `false` and a message. Check the return status before reporting success to the player.
- Interrupted write: The process can be killed between opening the file and finishing the write, leaving a truncated or corrupt save. Because LÖVE does not guarantee an atomic write, keep a previous-generation backup before overwriting and restore it if the new file fails read-time validation.
- Corrupt payload: A user or tool can edit the save file externally. Validate version and shape on every read; discard anything that does not match.
- Missing save directory: Rare, but possible if the identity is misconfigured or the platform denies the directory. Treat a missing directory as a first-run condition, not a fatal error.
Conditions that would change the design
- Cloud sync: you need a merge strategy and network error handling beyond the local filesystem.
- Multiple save slots: a single file is no longer sufficient; you need a slot index and directory listing.
- Encrypted payloads: you must manage key storage separately from the save data.
- Shared identity across games: `love.filesystem.setIdentity` changes where saves live, which can break expectations if players move between builds.
Limitations
LÖVE does not expose file locking, atomic rename, or guaranteed write ordering through `love.filesystem`. The design above reduces corruption exposure but cannot eliminate it on its own. Verify behavior on each target operating system because save directory paths and permission models differ.
Practical verification
Run the game, open the save directory printed at startup, and confirm that a save file appears after an explicit save event. Change one byte in the file with a text editor, restart the game, and confirm the loader falls back to default state. If you change `t.identity` in `conf.lua`, confirm that save files appear under the new identity and do not overwrite the previous project's saves.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.