Scratch for Java

SaveData()

Saves a game's progress as JSON independently of its installation directory. Data classes should have a public no-argument constructor and public fields. Public fields, including inherited fields, primitives, strings, enums, arrays and nested data classes, form the portable save format. Static and transient fields are excluded. Avoid getters, setters, library objects and object cycles in data intended to move between the desktop and browser implementations.

Saves live under the operating-system user's data folder, independently of the game installation. Keep the game ID unchanged across releases. Call save at checkpoints, rather than every frame. This helper assumes one running game instance; it does not synchronize writes from multiple instances or implement schema migrations or Steam-account separation.

On Windows, files live in LOCALAPPDATA; on macOS in Library/Application Support; on Linux in XDG_DATA_HOME or .local/share. Each game has a separate saves directory. The browser implementation uses IndexedDB, scoped to the hosting origin and browser profile, with a separate record per game and slot. Browser storage can be cleared by the user or evicted by the browser.

SaveData<Progress> saves = new SaveData<>("org.example.game", "slot1", Progress.class);
Progress progress = saves.loadOrDefault(() -> new Progress());
progress.coins += 10;
saves.save(progress);

Examples

View on GitHub

Syntax

Java

new SaveData(gameId, slot, type)
SaveData()

Create Shareable URL

Select Sections