Game content data models (stable ids, schemas, serialization)
data-modelserializationjsonidslevel-editorarchitecturemigrations
Lessons about how a game should identify, store, and evolve its levels and entities, drawn from a full audit of Run (Flutter/Flame) in 2026-10, where most recurring bugs traced back to these structural choices rather than to gameplay code.
Give every piece of content a stable identity
- Do not address content by list index. Run's built-in levels were identified by their
position in a list. One
scores.level_indexcolumn ended up carrying four meanings: 0-99 built-in, +100 legacy, negative = world level id, -1 +community_level_id= community level. Results: world-level-1 scores collided with community scores (both -1), legacy ghosts overwrote new levels' ghost slots, PB keys likepb_$iwere ambiguous, and adding level 100 would collide with the legacy offset. - Target shape: a typed reference
LevelRef{kind, id}plus a revision number (level_rev) so scores and ghosts know which version of a level they belong to. - The same applies to checkpoints and other cross-referenced entities: match by id, not by position. (Justin corrected position-based checkpoint matching to id-based.)
- Reference assets (skins, themes) by id, not by display name: Run looked up skins by name in five stores with three different lookup orders.
One pipeline, not three
Run had 23 legacy levels as Dart classes (4.6k lines), 15 JSON built-ins, and server-side "worlds". Each path had its own bugs. Prefer one repository (bundled asset pack -> disk cache -> network manifest), with built-ins published like any other content.
The "forgotten field" bug class
When an entity's field is declared in ~9 places (declaration, constructor, toJson, fromJson,
copy, runtime builder, runtime constructor, properties panel, canvas), adding a field means
editing 9 files, and missing one silently drops data. Run's editor audit found: copy()
dropping lock fields, "is this draft empty?" checks ignoring newer entity lists (which then
auto-deleted real work), serialization that skipped fields when a toggle was off (so turning
the toggle back on lost the settings). Fixes:
- One entity interface (sealed class) and a dimension.all enumeration instead of N parallel
typed lists enumerated by hand in ~20 places.
- A type registry/schema that drives JSON, the properties panel, spawning, and respawn policy.
- Code generation for copy/equality/JSON.
- Serialize every field whenever it differs from the default, regardless of toggles.
- An exhaustive round-trip test (build every entity with non-default values, serialize,
parse, compare) plus a golden parse of all built-in levels.
Tolerant, versioned decoding
- Put a
versionin saved data and run amigrate()pipeline on load. - Tolerate unknown enum values and pass unknown keys through, so older clients do not destroy newer data.
- Never let one corrupt entry blank the app: decode each stored item in its own try/catch and
back up the corrupt blob (Run:
user_levels_corrupt_backup), especially for data loaded beforerunApp. - Key-value prefs (SharedPreferences / localStorage) are a poor home for multi-MB blobs (all drafts in one key, pixel-art skins, ghosts); browsers throw QuotaExceededError. Catch it and fall back, or move to a real store.
Dynamic content ("worlds")
Run's plan to scale past dozens of levels: themed worlds stored in the database (name, description, difficulty 1-5, theme, icon, sort order, lock state, ordered levels) so the app binary stays small and content ships without a release. Requirements: identical UX to built-ins, stale-while-revalidate caching for offline, server withholds JSON for editor-locked levels, thumbnails generated client-side from geometry. Missing pieces found later: no per-level version/updated_at, N+1 fetches, no offline cache.
See also
- Client/server sync pitfalls (deletes, timestamps, merge policies)
- Building an in-game level editor (Flutter)
- Ghost replays for racing and speedrun games
Linked from
- Building an in-game level editor (Flutter)
- Client/server sync pitfalls (deletes, timestamps, merge policies)
- Flutter and Dart gotchas (collections, widgets, theming)
- Ghost replays for racing and speedrun games
- Nightly MySQL/MariaDB backups with mysqldump and cron
- Run (runrunrunrun.run), Justin's speedrun platformer
- Testing Flutter/Flame games and running them in GitLab CI
- Theme and skin systems for 2D games