FlixelSave
View sourceclass
org.flixelgdx.util.save.FlixelSave
Utility class that provides a high-level, cross-platform mechanism for saving and loading persistent game data within the FlixelGDX framework using libGDX's Preferences API.
This utility abstracts away the complexities of serialization and platform-specific details, allowing you to bind to a uniquely identified save slot, manipulate a structured ObjectMap of key-value pairs (which can contain nested structures compatible with libGDX's Json API), and safely flush or clear saved progress.
Key Features:
- Slot-based Save Management: Bind to a specific save "slot" using FlixelSave.bind(String, String), supporting multiple profiles or manual slots.
- Automatic JSON Serialization: All data stored is serialized to JSON transparently via
Json, enabling nested objects, numeric arrays, etc. - Safe: Handles error states and empty preferences files systematically. Check FlixelSave.getStatus() and FlixelSave.isBound() for reliability.
- Merging and Flushing: Easily merge external data with conflict resolution, and flush changes immediately to disk.
- Destroy/Clear: Data can be programmatically wiped for a "reset save" experience.
Typical Usage:
FlixelSave save = new FlixelSave();
save.bind("savefile", null); // or ("savefile", "slot1")
save.data.put("score", 12345);
save.flush();
int highScore = (int)save.data.get("score", 0);
Threading and Platform Notes:
- This class operates on the main thread. Use after libGDX's application context is initialized.
- Data is written using libGDX's
Preferences.flush()to work in harmony with the libGDX ecosystem. - Intended for small to medium-sized game progress data, not binary assets or large files.
See Also: com.badlogic.gdx.Preferences, com.badlogic.gdx.utils.Json, com.badlogic.gdx.utils.ObjectMap, FlixelSaveStatus
Constructors
FlixelSave()
Fields
data
Root data object (JSON-compatible tree via libGDX ObjectMap).
This is where all of your save data is stored. Use this to set any kind of data you want to preserve after the game is closed. Remember to call FlixelSave.flush() to actually save the data to disk!
Methods
bind(String, String)
Binds to local preferences. slot selects a separate file name suffix for multiple slots.
Parameters:
| Name | Description |
|---|---|
name | Primary preferences name (no spaces; safe for file names). |
slot | Optional suffix, e.g. "slot1" -> name_slot1. |
Returns: true if bind succeeded, and data was loaded (or empty new save).
isBound()
getBound()
Returns whether this save is bound to a named preferences file and ready to use.
getName()
getSlot()
getStatus()
load()
Loads the last saved data from disk and refreshes the FlixelSave.data map.
flush()
Writes FlixelSave.data to preferences and flushes.
Returns: true if the data was successfully written to disk.
erase()
Erases the save data and flushes automatically.
Returns: true if the data was successfully erased and flushed.
isEmpty()
getEmpty()
Returns whether this save contains no stored data.
destroy()
mergeData(ObjectMap<String,Object>, boolean, boolean)
Merges another data map into this save (optionally overwriting keys).
Parameters:
| Name | Description |
|---|---|
source | The data map to merge into this save. |
overwrite | Whether to overwrite existing keys. |
flushAfter | Whether to flush the data after merging. |
Returns: true if data changed and flush succeeded when flushAfter is true.