Trouble with LoadSave table

Hi all,

I’ve been using Rob Miracle’s tutorials on saving game data… I started with the basic:

storyboard.state = {} storyboard.state.life1 = 1 storyboard.state.life2 = 1 storyboard.state.life3 = 1

 Which works well to a degree. Then I tried ‘raising the bar’ a little by using the JSON command to save to system.DocumentsDirectory

My main.lua now looks like this:

local loadsave = require("loadsave") local storyboard = require "storyboard" storyboard.state = {} storyboard.state.life1 = 1 storyboard.state.life2 = 1 storyboard.state.life3 = 1 loadsave.saveTable(storyboard.state, "myTable.json", system.DocumentsDirectory) newTable = loadsave.loadTable("myTable.json", system.DocumentsDirectory) loadsave.printTable(newTable)

I’ve tested on my device and the data isn’t being read if I quit and reopen the application, it just seems to reset to it’s initial values.

Can anyone tell me what I’m doing wrong?

All the best

If that’s your code and you quit and restart the app, you initialize the table and save it right away.  Normally you would first try and load the table and if it returns nil, i.e. it couldn’t find the file, then initialize it and save out the first versions.  After that you only need to save when you change things.

Thanks Rob. I had to read your response a couple of times, before it sank in. So, what you’re sying is; Run a function to see if the table exists, then ‘initialize’ one if it doesn’t (will the normal *.saveTable command do this, or is there another method for this initialization process?)

Then… If any of the table content changes, for example, I have an in-game function, something like:

if character = dead then storyboard.state.life1 = 0

When this change happens, I should always invoke; *.saveTable as part of the same function?

Seems a bit long winded. It would be nice if the table could be saved on application-quit, the basic ‘not save to an external file’ method, handles session-based data and suspended-apps, just fine, as it is.

Thanks again

PS, why do my posts now need to be moderated?

It’s pretty straight forward.  In my main.lua I do:

local settings = loadsave.loadTable( "settings.json" ) if settings == nil then     settings = {}     settings.soundOn = true     settings.musicOn = true     loadsave.saveTable( settings, "settings.json" ) end

Try to load it.  If it does, great I’m all set.  If it doesn’t and the settings table is still nil, then initialize it to my base settings and write the table back out.  Then any time I need to update something, I save the table.

As for the moderated posts, only your first post is moderated.  Once we approve it, you can post without moderation.  This is a spam defense as the forums were being hit with an increasing amount of spam.

Rob

Thanks Rob, that works fine, I just needed it explaining-for-dummies. More interesting however, is the fact I changed my email and now appear to be a newb. Look at the above posts, I started the thread as one profile, changed my account email from settings and now appear to have a completely different one… I prefer being a Jedi to a newb, how can I swap back?

I don’t think I can tie the accounts together.  Don’t worry, you only need a few posts to get to Jedi.

Rob

If that’s your code and you quit and restart the app, you initialize the table and save it right away.  Normally you would first try and load the table and if it returns nil, i.e. it couldn’t find the file, then initialize it and save out the first versions.  After that you only need to save when you change things.

Thanks Rob. I had to read your response a couple of times, before it sank in. So, what you’re sying is; Run a function to see if the table exists, then ‘initialize’ one if it doesn’t (will the normal *.saveTable command do this, or is there another method for this initialization process?)

Then… If any of the table content changes, for example, I have an in-game function, something like:

if character = dead then storyboard.state.life1 = 0

When this change happens, I should always invoke; *.saveTable as part of the same function?

Seems a bit long winded. It would be nice if the table could be saved on application-quit, the basic ‘not save to an external file’ method, handles session-based data and suspended-apps, just fine, as it is.

Thanks again

PS, why do my posts now need to be moderated?

It’s pretty straight forward.  In my main.lua I do:

local settings = loadsave.loadTable( "settings.json" ) if settings == nil then     settings = {}     settings.soundOn = true     settings.musicOn = true     loadsave.saveTable( settings, "settings.json" ) end

Try to load it.  If it does, great I’m all set.  If it doesn’t and the settings table is still nil, then initialize it to my base settings and write the table back out.  Then any time I need to update something, I save the table.

As for the moderated posts, only your first post is moderated.  Once we approve it, you can post without moderation.  This is a spam defense as the forums were being hit with an increasing amount of spam.

Rob

Thanks Rob, that works fine, I just needed it explaining-for-dummies. More interesting however, is the fact I changed my email and now appear to be a newb. Look at the above posts, I started the thread as one profile, changed my account email from settings and now appear to have a completely different one… I prefer being a Jedi to a newb, how can I swap back?

I don’t think I can tie the accounts together.  Don’t worry, you only need a few posts to get to Jedi.

Rob