Problem saving myData with JSON.

Thank you Rob

I did put same debug prints to verify that I getting into the function as expected

I also remove the saveTable call from the function to the scene:show of the  complete level scene, I also change the default values I have in the levelData file to make sure I see the changes in the settings.json file in the sandBox.

Still when I relaunch the simulator I see only the default values of the table in the levelData.

I also verify that I have only one setting.json file in the relevant game directory

The strange thing is that I can see the setting.json file in the sendBox with the default values.

After I complete the level I can see the new score in the level select menu but I don’t see it in the setting.json file…

The only place I update the table is in the complete level scene

I don’t know what can I do, can you point me to a different method to save the game (using a table for multiple levels)?

Regards,

Guy

Are you doing anything with loading/saving the table in your main.lua?

Rob,

I forgot the main.lua file, I have two lines of code it this file and one of them is:

"loadsave.saveTable( levelData.settings, “settings.json”

I comment out this line and I can save and reload the new table with the new score.

 

Thank you Rob

:slight_smile:

 

 

Now I need to understand why this line cause the problems. :wacko:

 

 

Regards,

Guy

It really helps to copy and paste text. The code above has an error. But if that’s in your main.lua and perhaps just above it somewhere you did:

levelData.settings = {}

followed by:

loadsave.saveTable( levelData.settings, "settings.json" )

then you are blowing away you’re previous settings file. Normally what I do is in main.lua:

levelData.settings = loadsave.loadTable( "settings.json" ) if levelData.settings == nil then  -- did not load the settings file, assume first -- time running app or the user killed the app and restarting cold.     levelData.settings = {}     --     -- Set your default values here     --     loadsave.saveTable( levelData.settings, "settings.json" ) end

Think about it this way. You only ever need to load the table once and that’s when the app starts cold. If it was just in the background and the user switched back to it, the code in main.lua doesn’t run again, the app is still running, it was just backgrounded. Your previous settings were still in memory.  If your user kills the app, then the next time they run it, it will need to resume with the last settings, so you have to read the table.

Now every time you change that table and the change is reasonably important, I would go ahead and call the loadsave.saveTable() function. It’s not that expensive of an operation. I wouldn’t delay until the user backgrounds the app to save. A force quit won’t trigger the suspended event. 

Rob

Hi Rob,

Thank you for your detailed answer

In your code you mention to insert the default values in the main.lua file

levelData.settings = loadsave.loadTable( “settings.json” )
if levelData.settings == nil then  – did not load the settings file, assume first
    –                                time running app or the user killed the app and restarting cold.
    levelData.settings = {}
    –
    – Set your default values here
    –
    loadsave.saveTable( levelData.settings, “settings.json” )
end

I have the default values in a table in the levelData file, do I need to link/copy it to the main file?

Can I hold the table in two files?

Here is an example of my table in levelData file

local M = {}

M.maxLevels = 50

M.settings = {}

M.settings.currentLevel = 1

M.settings.unlockedLevels = 4

M.settings.soundOn = true

M.settings.musicOn = true

M.settings.levels = {}

M.settings.levels[1] = {}

M.settings.levels[1].stars = 1

M.settings.levels[1].score = 1000

M.settings.levels[1].delay = 3000

M.settings.levels[1].balls = {}

M.settings.levels[1].balls.red    = 5

M.settings.levels[1].balls.blue   = 4

M.settings.levels[1].balls.green  = 4

M.settings.levels[1].balls.yellow = 4

Regards,

Guy

Hi Rob,

Its working…….

THANK YOU

levelData.settings = loadsave.loadTable( “settings.json” )

if levelData.settings == nil then  – did not load the settings file, assume first

    –                                time running app or the user killed the app and restarting cold.

    levelData.settings = {}

    –

    – Set your default values here

    –

levelData.settings.currentLevel = 1

levelData.settings.unlockedLevels = 1

levelData.settings.soundOn = true

levelData.settings.musicOn = true

levelData.settings.levels = {}

levelData.settings.levels[1] = {}

levelData.settings.levels[1].stars = 1

levelData.settings.levels[1].score = 1000

levelData.settings.levels[1].delay = 3000

levelData.settings.levels[1].balls = {}

levelData.settings.levels[1].balls.red    = 4

levelData.settings.levels[1].balls.blue   = 4

levelData.settings.levels[1].balls.green  = 4

levelData.settings.levels[1].balls.yellow = 4

    loadsave.saveTable( levelData.settings, “settings.json” )

end

Regards,

Guy

Hi Rob,

 

Its seems that my saving code still not working as expected.

New values for level completed are not saved as expected

 

This is what I have for now.

 

In the main.lua I have some default values in case game runs for the first time (values are only for level1 and general settings )

I have a leveldata.lua file that holds the table of all the levels default settings (including level1).

After player complete the level with higher score I’m saving the table.

In the sandbox I see the new level1 score but I don’t see all the levels settings I have in leveldata.lua table.

 

I can delete the leveldata.lua file and hold the table in my main file but before I do that i want to know if my current code setup can work.

 

 

Regards,

Guy