A class to help automate and simplify save game functionality. A simple wrapper for the OpenFl's SharedObject, with a couple helpers. It's used automatically by various flixel utilities like the sound tray, as well as some debugging features.

Resources

Making your own

You can use a specific save name and path by calling the following,

FlxG.save.bind("myGameName", "myGameStudioName");

It is recommended that you do so before creating an instance of FlxGame.

Note: It is NOT recommended to make you own instance of FlxSave, one is made for you when a FlxGame is created at FlxG.save. The default name and path is specified by your Project.xml's "file" and "company", respectively. That said, nothing is stopping you from instantiating your own instance.

Default Paths

  • Windows: "C:\Users\<username>\AppData\Roaming\<localPath>\<name>.sol"
  • Mac: "/Users/<username>/Library/Application Support/<localPath>/<name>.sol"
  • Chrome: In the developer tools, go to the Application tab, and under Storage->Local Storage->https://<url>.com with the key:<localPath>:<name>"

5.0.0 Migration

In older version of flixel, a null path on html5 would use the current url. FlxSaves with a null path will now look for that path, if the new default path is not found. If data is found at the legacy, it is loaded, but flush calls will save to the new default path. The old save is not deleted.

Prior to 5.0.0, FlxG.save's default save name was "flixel", now it uses the project name as defined in Project.xml. FlxG.save will automatically look for the old default save if the new one is not found, and any flush call will save to the new path and id.

Previously, on desktop targets, saves were added to subfolders based on the project's name and company, eg: "/<app company>/<app title>/<localPath>/<name>.sol". This prevent separate projects from referencing each others saves (this was OpenFL attempting to mirror Flash's SharedObject behaviour). To allow cross-save referencing, the new location is simply: '/<localPath>/<name>.sol'. If no data is found in this location, FlxSave will look in the old location, any flush call will save to the new location. For example, a save named with the old default name "flixel" may be saved at "<...>/FooBarGames/PorbsAdventure/flixel.sol". The new default save name "PorbsAdventure" would be at "<...>/FooBarGames/PorbsAdventure.sol".

See also:

Constructor

new()

Variables

read onlydata:Dynamic

Allows you to directly access the data container in the local shared object.

read onlyisBound:Bool

Wether the save was successfully bound.

Available since

5.0.0

.

read onlyname:String

The name of the local shared object.

read onlypath:String

The path of the local shared object.

Available since

4.6.0

.

@:value(EMPTY)read onlystatus:FlxSaveStatus = EMPTY

The current status of the save.

Available since

5.0.0

.

Methods

bind(name:String, ?path:String):Bool

Automatically creates or reconnects to locally saved data.

Parameters:

name

The name of the save (should be the same each time to access old data). May not contain spaces or any of the following characters: ~ % & \ ; : " ' , < > ? #

path

The full or partial path to the file that created the shared object. Mainly used to differentiate from other FlxSaves. If you do not specify this parameter, the company name specified in your Project.xml is used.

Returns:

Whether or not you successfully connected to the save data.

@:value({ minFileSize : 0 })close(minFileSize:Int = 0):Bool

A way to safely call flush() and destroy() on your save file. Will correctly handle storage size popups and all that good stuff. If you don't want to save your changes first, just call destroy() instead.

Parameters:

minFileSize

If you need X amount of space for your save, specify it here.

Returns:

The result of result of the flush() call (see below for more details).

destroy():Void

Clean up memory.

erase():Bool

Erases everything stored in the local shared object. Data is immediately erased and the object is saved that way, so use with caution!

Returns:

Returns false if the save object is not bound yet.

@:value({ minFileSize : 0 })flush(minFileSize:Int = 0):Bool

Writes the local shared object to disk immediately. Leaves the object open in memory.

Parameters:

minFileSize

If you need X amount of space for your save, specify it here.

Returns:

Whether or not the data was written immediately. False could be an error OR a storage request popup.

isEmpty():Bool

Scans the data for any properties.

Available since

5.0.0

.

@:value({ minFileSize : 0, overwrite : false })mergeData(sourceData:Dynamic, overwrite:Bool = false, minFileSize:Int = 0):Bool

Copies the given data over to this save and flushes (if changed).

Parameters:

sourceData

The data to merge

overwrite

Whether the data should overwrite, should the 2 saves share data fields. defaults to false.

minFileSize

If you need X amount of space for your save, specify it here.

Returns:

Whether or not you successfully saved the data.

@:value({ minFileSize : 0, eraseSave : true, overwrite : false })mergeDataFrom(name:String, ?path:String, overwrite:Bool = false, eraseSave:Bool = true, minFileSize:Int = 0):Bool

Creates a new FlxSave and copies the data from old to new, flushes the new save (if changed) and then optionally erases the old save.

Parameters:

name

The name of the save.

path

The full or partial path to the file that created the save.

overwrite

Whether the data should overwrite, should the 2 saves share data fields. defaults to false.

eraseSave

Whether to erase the save after successfully migrating the data. defaults to true.

minFileSize

If you need X amount of space for your save, specify it here.

Returns:

Whether or not you successfully found, merged and flushed data.