Skip to main content

FlixelSave

View source

class

org.flixelgdx.util.save.FlixelSave

public class FlixelSave implements FlixelDestroyable

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()

public FlixelSave()

Fields

data

public final ObjectMap<String,Object> 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)

public boolean bind(String name, String slot)

Binds to local preferences. slot selects a separate file name suffix for multiple slots.

Parameters:

NameDescription
namePrimary preferences name (no spaces; safe for file names).
slotOptional suffix, e.g. "slot1" -> name_slot1.

Returns: true if bind succeeded, and data was loaded (or empty new save).


isBound()

public boolean isBound()

getBound()

public boolean getBound()

Returns whether this save is bound to a named preferences file and ready to use.


getName()

public String getName()

getSlot()

public String getSlot()

getStatus()

public FlixelSaveStatus getStatus()

load()

public void load()

Loads the last saved data from disk and refreshes the FlixelSave.data map.


flush()

public boolean flush()

Writes FlixelSave.data to preferences and flushes.

Returns: true if the data was successfully written to disk.


erase()

public boolean erase()

Erases the save data and flushes automatically.

Returns: true if the data was successfully erased and flushed.


isEmpty()

public boolean isEmpty()

getEmpty()

public boolean getEmpty()

Returns whether this save contains no stored data.


destroy()

public void destroy()

mergeData(ObjectMap<String,Object>, boolean, boolean)

public boolean mergeData(ObjectMap<String,Object> source, boolean overwrite, boolean flushAfter)

Merges another data map into this save (optionally overwriting keys).

Parameters:

NameDescription
sourceThe data map to merge into this save.
overwriteWhether to overwrite existing keys.
flushAfterWhether to flush the data after merging.

Returns: true if data changed and flush succeeded when flushAfter is true.


close(int)

public boolean close(int minFileSize)