Skip to content

Instantly share code, notes, and snippets.

@veeneck
Last active July 1, 2016 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veeneck/1d988af6e20e67b131c9cf775f546f16 to your computer and use it in GitHub Desktop.
Save veeneck/1d988af6e20e67b131c9cf775f546f16 to your computer and use it in GitHub Desktop.
Example usage of saving GameData
/// User clicks on slot one, so we want to create a new save file for that slot
if GameData.sharedInstance.loadGame(saveFile: "One") {
/// An existing save file was there, progress as you need to
}
else {
/// A new save file was created, so start your game from a fresh state
}
/// User buys 100 gold
GameData.sharedInstance.bank += 100
GameData.sharedInstance.save()
/// User advances to level 2, so you want to update level data and save the file
/// Assumes you have handle on Level object already
level.completed = true
/// Then, I just replace the entire instance of the storage with the new level object.
/// More important when working with structs
GameData.sharedInstance.levelData?.levels[level.id] = level
/// Call save to write the changes out
GameData.sharedInstance.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment