Cannot save and load new data with loadsave.lua

I want the user to be able to unlock a new level when their score reaches a certain value. I am using loadsave.lua to save the number of unlocked levels the player has. I use this code to determine if they unlocked the next level:

if score >= 30 then

        myData.settings.unlockedLevels = 3

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

 end

For my levels I am using this tutorial:

https://coronalabs.com/blog/2014/08/12/tutorial-building-a-level-selection-scene/

It works fine the first time, but when I reload the simulator it is not saved. 

I load the table in my levelselect.lua:

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

Can anyone help me with this? Thank you.

Sincerely, 

Alex

Do I have to read or write anything from my json file?

That code is pretty bug free. Normally on start up, you try to read the file and if the returned value is nil, then the table didn’t load for some reason (likely cause, the file doesn’t exist). Then you would create the table of data you want to initialize with and then call saveTable() to write out the data. If you successfully read the data, your settings table would be populated and you would be good to go.

Since the settings table stays in memory, you only need to call saveTable() when you make updates that need saved.

You can open the simulator’s sandbox: File -> Show Project Sandbox and then open the “Docs” folder and see if your file is there or not. You can also test the return value from saveTable() and see what it’s returning.

Rob

The settings.json exists and contains the right information, but I am having trouble retrieving that information.

Do I have to convert it into a lua table?

The saveTable() and loadTable() check for the existence and path of the file, right?

I had similar problem.

Use print function to make sure you have right data. Print table with data from file before use and do the same before write them to file. 

Hope this help:)

What is the value of myData.settings after you call loadTable()?

Rob

myData.settings.unlockedLevels = 2, meaning only two levels are unlocked. 

can you do:

local results = loadsave.loadTable(“settings.json”)

print(“Results:”, results, “!”

Rob

Thanks for all you help guys, I just had to set M.settings to settings.json.

Do I have to read or write anything from my json file?

That code is pretty bug free. Normally on start up, you try to read the file and if the returned value is nil, then the table didn’t load for some reason (likely cause, the file doesn’t exist). Then you would create the table of data you want to initialize with and then call saveTable() to write out the data. If you successfully read the data, your settings table would be populated and you would be good to go.

Since the settings table stays in memory, you only need to call saveTable() when you make updates that need saved.

You can open the simulator’s sandbox: File -> Show Project Sandbox and then open the “Docs” folder and see if your file is there or not. You can also test the return value from saveTable() and see what it’s returning.

Rob

The settings.json exists and contains the right information, but I am having trouble retrieving that information.

Do I have to convert it into a lua table?

The saveTable() and loadTable() check for the existence and path of the file, right?

I had similar problem.

Use print function to make sure you have right data. Print table with data from file before use and do the same before write them to file. 

Hope this help:)

What is the value of myData.settings after you call loadTable()?

Rob

myData.settings.unlockedLevels = 2, meaning only two levels are unlocked. 

can you do:

local results = loadsave.loadTable(“settings.json”)

print(“Results:”, results, “!”

Rob