Saving/Loading data JSON

I came across this beautiful tidbit of code:
http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

And I want to use this in the game I’m developing. However, I’m a little unsure on how to implement this.

I’m using Director, so the best place to put the save/load functions would be the Main.lua (I think) and then call these functions upon entering the first scene (main menu).

However, I’m not quite sure how I could make it so it saves the default options first, but loads user options if those exist.

Essentially, I want to to check if there’s a file saved, if not, save the default one, if it does exist, load that file.
Can I check if that file exists? [import]uid: 200198 topic_id: 36198 reply_id: 336198[/import]

What you’d likely do is try to load the file first. I see from the routine if the file isn’t found then it returns nil, so you’d just check against that. If the value isn’t nil, it has loaded fine (IE it already existed).
If it returns nil, you set up your default options, and likely will want to save them immediately (ready to be found the next time you want to load). [import]uid: 46639 topic_id: 36198 reply_id: 143742[/import]

Oh wow, I must be more tired than I thought… hadnt even noticed that if statement
I could just add an else statement, and refer to the saveTable function

function loadSettings(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable else -- save file if it doesnt exist (default settings) gameSettings = {} gameSettings.soundOn = true gameSettings.soundOff = false saveSettings(gameSettings, "gameSettings.json") end return nil end

Well, at least I can hope this will help other developers (note, I changed the names a little, I dont like using “my” in variables) [import]uid: 200198 topic_id: 36198 reply_id: 143746[/import]

Hey there,

you might look at a library i recently released which saves game/app data using JSON. this library is different because you don’t have to think about where, when or how to save your data – it’s all done automatically.

also, there’s a flag you can check for when you’re starting with a new file ; this gives you the opportunity to programmatically put in your defaults.
adding defaults manually within code is good practice because in the future you might upgrade your game and have to migrate the structure of the existing data to a new one, e.g., adding new sections and setting defaults for those. migrating this way will give you a lot more control over the data.

here’s the link in the Corona code share. full documentation is available on my website.

https://developer.coronalabs.com/code/dmc-lib-auto-store

cheers,
dmc [import]uid: 74908 topic_id: 36198 reply_id: 144027[/import]

What you’d likely do is try to load the file first. I see from the routine if the file isn’t found then it returns nil, so you’d just check against that. If the value isn’t nil, it has loaded fine (IE it already existed).
If it returns nil, you set up your default options, and likely will want to save them immediately (ready to be found the next time you want to load). [import]uid: 46639 topic_id: 36198 reply_id: 143742[/import]

Oh wow, I must be more tired than I thought… hadnt even noticed that if statement
I could just add an else statement, and refer to the saveTable function

function loadSettings(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable else -- save file if it doesnt exist (default settings) gameSettings = {} gameSettings.soundOn = true gameSettings.soundOff = false saveSettings(gameSettings, "gameSettings.json") end return nil end

Well, at least I can hope this will help other developers (note, I changed the names a little, I dont like using “my” in variables) [import]uid: 200198 topic_id: 36198 reply_id: 143746[/import]

Hey there,

you might look at a library i recently released which saves game/app data using JSON. this library is different because you don’t have to think about where, when or how to save your data – it’s all done automatically.

also, there’s a flag you can check for when you’re starting with a new file ; this gives you the opportunity to programmatically put in your defaults.
adding defaults manually within code is good practice because in the future you might upgrade your game and have to migrate the structure of the existing data to a new one, e.g., adding new sections and setting defaults for those. migrating this way will give you a lot more control over the data.

here’s the link in the Corona code share. full documentation is available on my website.

https://developer.coronalabs.com/code/dmc-lib-auto-store

cheers,
dmc [import]uid: 74908 topic_id: 36198 reply_id: 144027[/import]

What you’d likely do is try to load the file first. I see from the routine if the file isn’t found then it returns nil, so you’d just check against that. If the value isn’t nil, it has loaded fine (IE it already existed).
If it returns nil, you set up your default options, and likely will want to save them immediately (ready to be found the next time you want to load). [import]uid: 46639 topic_id: 36198 reply_id: 143742[/import]

Oh wow, I must be more tired than I thought… hadnt even noticed that if statement
I could just add an else statement, and refer to the saveTable function

function loadSettings(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable else -- save file if it doesnt exist (default settings) gameSettings = {} gameSettings.soundOn = true gameSettings.soundOff = false saveSettings(gameSettings, "gameSettings.json") end return nil end

Well, at least I can hope this will help other developers (note, I changed the names a little, I dont like using “my” in variables) [import]uid: 200198 topic_id: 36198 reply_id: 143746[/import]

Hey there,

you might look at a library i recently released which saves game/app data using JSON. this library is different because you don’t have to think about where, when or how to save your data – it’s all done automatically.

also, there’s a flag you can check for when you’re starting with a new file ; this gives you the opportunity to programmatically put in your defaults.
adding defaults manually within code is good practice because in the future you might upgrade your game and have to migrate the structure of the existing data to a new one, e.g., adding new sections and setting defaults for those. migrating this way will give you a lot more control over the data.

here’s the link in the Corona code share. full documentation is available on my website.

https://developer.coronalabs.com/code/dmc-lib-auto-store

cheers,
dmc [import]uid: 74908 topic_id: 36198 reply_id: 144027[/import]

What you’d likely do is try to load the file first. I see from the routine if the file isn’t found then it returns nil, so you’d just check against that. If the value isn’t nil, it has loaded fine (IE it already existed).
If it returns nil, you set up your default options, and likely will want to save them immediately (ready to be found the next time you want to load). [import]uid: 46639 topic_id: 36198 reply_id: 143742[/import]

Oh wow, I must be more tired than I thought… hadnt even noticed that if statement
I could just add an else statement, and refer to the saveTable function

function loadSettings(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable else -- save file if it doesnt exist (default settings) gameSettings = {} gameSettings.soundOn = true gameSettings.soundOff = false saveSettings(gameSettings, "gameSettings.json") end return nil end

Well, at least I can hope this will help other developers (note, I changed the names a little, I dont like using “my” in variables) [import]uid: 200198 topic_id: 36198 reply_id: 143746[/import]

Hey there,

you might look at a library i recently released which saves game/app data using JSON. this library is different because you don’t have to think about where, when or how to save your data – it’s all done automatically.

also, there’s a flag you can check for when you’re starting with a new file ; this gives you the opportunity to programmatically put in your defaults.
adding defaults manually within code is good practice because in the future you might upgrade your game and have to migrate the structure of the existing data to a new one, e.g., adding new sections and setting defaults for those. migrating this way will give you a lot more control over the data.

here’s the link in the Corona code share. full documentation is available on my website.

https://developer.coronalabs.com/code/dmc-lib-auto-store

cheers,
dmc [import]uid: 74908 topic_id: 36198 reply_id: 144027[/import]