Storing App Settings

I was looking through this post and I’m just trying to get a few things straight.

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

In order to use this, I can set up a Corona project that just has this code:

AppSettings = {}

AppSettings.Benchmark = “Brent”

AppSettings.Units = “Metric”

AppSettings.Code = “BZ”

saveTable(AppSettings, “AppSettings.json”)

in order to create my JSON file, since I can’t just create one in the same directory as my main.lua?

I tried just writing a JSON file and adding  it to my project directory, but reading further through the blog comments I don’t think that will work.

Also, I am getting some errors when I try and run this project.

File: …ers/seandzafovic/Desktop/Volume App 2/data_entry.lua

Line: 50

Attempt to index global ‘AppSettings’ (a nil value)

stack traceback:

    [C]: ?

    …ers/seandzafovic/Desktop/Volume App 2/data_entry.lua:50: in function <…ers/seandzafovic/Desktop/Volume App 2/data_entry.lua:46>

    ?: in function ‘dispatchEvent’

    ?: in function ‘gotoScene’

    /Users/seandzafovic/Desktop/Volume App 2/main.lua:54: in function ‘gotoScene1’

    /Users/seandzafovic/Desktop/Volume App 2/main.lua:69: in main chunk

I have AppSettings={} as a global variable and then these lines of code:

    --load default settings

    AppSettings = loadTable (“AppSettings.json”)

    OilType=AppSettings.Benchmark

    Units=AppSettings.Units

    OilCode=AppSettings.Code

at the top of my scene:createScene function.

I am assuming that I’m getting the error because I currently have the JSON file in the same place as main.lua. If not maybe you can point me down the right road before I waste too much more time.

Sorry that I cannot help you with your code specifically, but just wanted to pass along that the guys at Glitch Games shared some code that handles app settings (and other stored data) in a nice package.

Might be worth checking out as lots of other users here have used it, so seems solid.  It’s really easy to understand and allows for iCloud syncing as well.

https://github.com/GlitchGames/GGData

I did see that. I will check it out as an alternative.

I can’t seem to wrap my head around the initial data for the data storage. If I include the code for creating and writing to a box in my app, doesn’t that defeat the purpose of having an external data store, since the app code will just overwrite it?

I guess I’m just not sure how to create the data I want to use in the first place.

I would need to see more about how your program is structured.  What is line 50 of data_entry.lua.  If you’re doing this in the sim, you can look at your project sandbox files and see what’s there and make sure it’s saving the file like you expect it is. 

Where are you initializing the base settings?

Are you checking to see if loadtable() is returning nil (can’t find the file usually)?  You can put some prints in the functions to make sure what steps are being reached.

Rob

The problem is I’m not sure how to initialize the base settings. If I create a new JSON file in my app, that will just reset my settings between instances of the app, won’t it?

Am I able to make a new project, create the JSON table and then have it available for my data app? I originally had the JSON in the same folder as main.lua, but according to the blog comments, that won’t work.

Line 50 is OilType=AppSettings.Benchmark.  I am assuming that is because the JSON file doesn’t exist, or I had it in the wrong location.

Printing loadtable shows that it is nil.

Did some investigating on Stackoverflow. Finally came up with a method that seems to work. Basically check to see if the table variable is nil. If nil, then create the tableand load it into JSON, otherwise load from JSON.

The only glitch with this method is that I can’t seem to save just one member of the table, otherwise it erases the JSON document and ave *just* that variable. In order to change one setting I have to save all of them and just alter the one setting I am currently working with.

Basically what I do is something like this:

local mySettings = loadTable(“settings.json”)

if mySettings == nil then – There are no settings:

     mySettings = {}

     mySettings.soundOn = true

     mySettings.musicOn = true

     mySettings.highScore = 0

     saveTable( mySettings, “settings.json” )

end

That tries to load the settings and save it to a Lua table.  If it’s nil, then there are no settings, so set up the base and save it for future use.

Then after that, when ever you make a change to the mySettings table, save it.  There isn’t a reason to read it in again, since it’s still in memory (assuming you use some technique to make the table available where you need it).

Rob

Sorry that I cannot help you with your code specifically, but just wanted to pass along that the guys at Glitch Games shared some code that handles app settings (and other stored data) in a nice package.

Might be worth checking out as lots of other users here have used it, so seems solid.  It’s really easy to understand and allows for iCloud syncing as well.

https://github.com/GlitchGames/GGData

I did see that. I will check it out as an alternative.

I can’t seem to wrap my head around the initial data for the data storage. If I include the code for creating and writing to a box in my app, doesn’t that defeat the purpose of having an external data store, since the app code will just overwrite it?

I guess I’m just not sure how to create the data I want to use in the first place.

I would need to see more about how your program is structured.  What is line 50 of data_entry.lua.  If you’re doing this in the sim, you can look at your project sandbox files and see what’s there and make sure it’s saving the file like you expect it is. 

Where are you initializing the base settings?

Are you checking to see if loadtable() is returning nil (can’t find the file usually)?  You can put some prints in the functions to make sure what steps are being reached.

Rob

The problem is I’m not sure how to initialize the base settings. If I create a new JSON file in my app, that will just reset my settings between instances of the app, won’t it?

Am I able to make a new project, create the JSON table and then have it available for my data app? I originally had the JSON in the same folder as main.lua, but according to the blog comments, that won’t work.

Line 50 is OilType=AppSettings.Benchmark.  I am assuming that is because the JSON file doesn’t exist, or I had it in the wrong location.

Printing loadtable shows that it is nil.

Did some investigating on Stackoverflow. Finally came up with a method that seems to work. Basically check to see if the table variable is nil. If nil, then create the tableand load it into JSON, otherwise load from JSON.

The only glitch with this method is that I can’t seem to save just one member of the table, otherwise it erases the JSON document and ave *just* that variable. In order to change one setting I have to save all of them and just alter the one setting I am currently working with.

Basically what I do is something like this:

local mySettings = loadTable(“settings.json”)

if mySettings == nil then – There are no settings:

     mySettings = {}

     mySettings.soundOn = true

     mySettings.musicOn = true

     mySettings.highScore = 0

     saveTable( mySettings, “settings.json” )

end

That tries to load the settings and save it to a Lua table.  If it’s nil, then there are no settings, so set up the base and save it for future use.

Then after that, when ever you make a change to the mySettings table, save it.  There isn’t a reason to read it in again, since it’s still in memory (assuming you use some technique to make the table available where you need it).

Rob