settings.lua for storing info

Posted by hoan, Posted on September 7, 2011

3 votes
GitHub URL: 
https://github.com/hoan/corona-settings

settings.lua stores info in a local file. It will persis

Do this somewhere in main():

1
settings.load()

Then you can get values easily:

1
2
3
4
5
6
local soundOn = settings.get('sound_on')
if soundOn then
  print("Sound is on")
else
  print("Sound is off")
end

You can also set values easily:

1
2
settings.set('sound_on', false)
settings.set('music_on', true)

The info will be saved to a file as soon as settings.set is run.
The info will persist across the application restarts.

Thats all!


Replies

autolib
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 19 Jan 2011

Can you show a working example. Every time I put the code to work I get a nil value.

My sample main:

-------------------------------
local testpic = display.newImage ("image.png")
testpic.x = 160
testpic.y = 230
-----------------------------------------------------
-------------------------------
local testpic1 = display.newImage ("image.png")
testpic1.x = 10
testpic1.y = 10
-----------------------------------------------------

local function loadtest (event)
settings.load()

--Then you can get values easily:

local soundOn = settings.get('sound_on')
if soundOn then
print("Sound is on")
else
print("Sound is off")
end
end

local function savetest (event)
--You can also set values easily:
settings.set('sound_on', false)
settings.set('music_on', true)
end

------------------------

testpic:addEventListener ("touch", loadtest)

testpic1:addEventListener ("touch", savetest)

hoan
User offline. Last seen 10 hours 59 min ago. Offline
Joined: 20 Jan 2011

Hey there

Please run

1
settings.load()

in your main function when the app starts up. Then all the info will be loaded into memory.

It currently prints "Sound is off" because of settings.set('sound_on', false) which sets 'sound_on' to false.

autolib
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 19 Jan 2011

When I do that I get the following error still

"attempt to index global 'settings' (a nil value)"

note: I did have in the version that I did not post

hoan
User offline. Last seen 10 hours 59 min ago. Offline
Joined: 20 Jan 2011

Please do this as well

1
require("settings")

autolib
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 19 Jan 2011

Still no go. Was prompted for Json and I added the file.

require("settings")

-------------------------------
local testpic = display.newImage ("image.png")
testpic.x = 160
testpic.y = 230
-----------------------------------------------------
-------------------------------
local testpic1 = display.newImage ("image.png")
testpic1.x = 10
testpic1.y = 10
-----------------------------------------------------

local function loadtest (event)
settings.load()

--Then you can get values easily:

local soundOn = settings.get('sound_on')
if soundOn then
print("Sound is on")
else
print("Sound is off")
end
end

local function savetest (event)
--You can also set values easily:
settings.set('sound_on', false)
settings.set('music_on', true)
end

------------------------

testpic:addEventListener ("touch", loadtest)

testpic1:addEventListener ("touch", savetest)

settings.load()

autolib
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 19 Jan 2011

Any chance you can show a working example. I still can not get the program to work.

connectoid
User offline. Last seen 25 weeks 2 days ago. Offline
Joined: 3 Sep 2011

Here is working example:

in main.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
require("settings")
local json = require "json"
 
--............
 
settings.load()
hiScore = settings.get('hiScore')
 
--............
 
if score > hiScore then
        settings.set('hiScore', score)
end

in settings.json

1
{"hiScore":0}

put json.lua, settings.lua and settings.json into the project folder

jayme_fishman
User offline. Last seen 12 weeks 4 days ago. Offline
Joined: 5 Aug 2011

I am also trying to get this to work. I have been following the examples until this last post: "put json.lua, settings.lua and settings.json into the project folder" I downloaded the settings.lua file and created the settings.json file as shown above but where do you get the json.lua file?

connectoid
User offline. Last seen 25 weeks 2 days ago. Offline
Joined: 3 Sep 2011

..\Ansca\Sample Code\Networking\Facebook\

hoan
User offline. Last seen 10 hours 59 min ago. Offline
Joined: 20 Jan 2011

Hey Everyone

I figured out the bug. It has problems parsing invalid JSON and will error. Now it will handle error and work, even if the json file isn't created:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module(..., package.seeall)
 
function settings.set(key, value)
        _G.__settings[key] = value
        
        settings.save()
end
 
function settings.get(key)
        if _G.__settings then
                return _G.__settings[key]
        else
                return nil
        end
end
 
function settings.save()
        local path = system.pathForFile("settings.json", system.DocumentsDirectory)
        local fh = io.open(path, "w")
        
        fh:write(json.encode(_G.__settings))
        fh:close()
end
 
function settings.load()
        local path = system.pathForFile("settings.json", system.DocumentsDirectory)
        local fh, reason = io.open(path, "r")
 
        if fh then
                -- read all contents of file into a string
                local contents = fh:read("*a")
                
                local succ, data = pcall(function()
                        return json.decode(contents)
                end)
                
                if succ then
                        _G.__settings = data
                else
                        _G.__settings = {}
                end
 
                print("Loaded settings")
        else
                print("Coulnd't load settings: " .. reason)
                
                _G.__settings = {}
        end
end

003naveen
User offline. Last seen 1 day 23 hours ago. Offline
Joined: 9 Apr 2011

I'm getting the error:

attempt to index field __settings ( a nil value) at line 4.

Also, do we have to have the settings.json file and make it manually? It seems pretty dumb to have to manually add each entry to settings.json like in the highscore example that you posted...

ramana.rachakonda
User offline. Last seen 4 weeks 6 days ago. Offline
Joined: 21 Jan 2011

Try this......

module(..., package.seeall)

function settings.set(key, value)

_G.__settings[key] = value

settings.save()
end

function settings.get(key)
if _G.__settings then
return _G.__settings[key]
else
return nil
end
end

function settings.save()
local json = require("json")
local path = system.pathForFile("appchita_settings.json", system.DocumentsDirectory)
local fh = io.open(path, "w")

fh:write(json.encode(_G.__settings))
fh:close()
end

function settings.load()
local json = require("json")
local path = system.pathForFile("appchita_settings.json", system.DocumentsDirectory)
local fh, reason = io.open(path, "r")

if fh then
-- read all contents of file into a string
local contents = fh:read("*a")

local succ, data = pcall(function()
return json.decode(contents)
end)

if succ and data then
_G.__settings = data
else
_G.__settings = {}
end

print("Loaded settings")
else
print("Coulnd't load settings: " .. reason)

-- create file because it doesn't exist yet
_G.__settings = {}
settings.save()
end

return fh
end