Share Your Code

Load or Save Defaults / Preferences

Posted by jeff472, Posted on January 5, 2012, Last updated January 7, 2012

This is some sample code to show one way in which you application / game can preserve state between sessions.
There are many ways to do this, here is mine.
Use it, abuse it, add to it, whatever you like.
Happy to hear from you if you have extras to contribute, but I dont need to know if you use another method. ;)

(I may move these functions out into a lobal.lua file at some point)

To make use of this system, there is a truly global table called lobal
It contains as many elements as you want
You can initialise them en masse by changing the setDefaults() function
or one by one with the addDefaultItem(itemName, theValue) function

When the app suspends or exits, save the file with saveDefaults()
When the app starts or resumes, use loadDefaults()

Using the variables is simple using the syntactic sugar version

_G.lobal.myVariable = 6 (see what I did there??)

As long as you have the load/save code in place, you just use the _G.lobal variables and forget about it.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
-----------------------------------------------------------------------------------------
--
-- Defaults load /save example
-- Jeff Tullin 2012
----------------------------------------------------------------------------------------- 
 
require ("json")
_G.lobal = {}
_G.prefsFile = "Defaults.json"
 
 
function setDefaults()
 
   --in this function, set up the defaults for all 
   --the global state variables
 
   print ("Initialising")
   _G.lobal["userName"] = "Fred"
   _G.lobal["currentLevel"] = 6
 
end
 
function addDefaultItem(itemName, theValue)
   --this function allows you to add a new state variable
   --if one turns out to be needed later on
   print ("Initialising")
   _G.lobal[itemName] = theValue
end
 
function saveDefaults()
   --call this on application suspend or application exit
   print ("saving defaults")
   local path = system.pathForFile( _G.prefsFile, system.DocumentsDirectory )
   local file = io.open( path, "w" )
   local contents = json.encode(_G.lobal)
   file:write(contents)
   io.close( file )
end
 
 
function loadDefaults()
   --reload saved file
   print ("loading state")
 
   --set defaults first in case the file is missing
   setDefaults()
 
   local path = system.pathForFile( _G.prefsFile, system.DocumentsDirectory )
 
   local file = io.open( path, "r" )
   --will be nil if it does not exist
 
   if file then
        -- read all contents of file into a string
        local contents = file:read( "*a" )
        _G.lobal = json.decode(contents)
        io.close( file )
   else
     print ("file not found")
   end
 
end
 
 
 
--test the system
-- to make use of this system, there is a truly global table called lobal
-- it contains as many elements as you want
-- you can initialise them en masse in the setDefaults() function
-- or one by one with the addDefaultItem(itemName, theValue)  function
 
--when the app suspends or exits, save the file with saveDefaults()
--when the app starts or resumes, use loadDefaults()
 
--using the variables is simple using the syntactic sugar version
 
--   _G.lobal.myVariable = 6   (see what I did there??)
 
 
 
 
setDefaults()
addDefaultItem("highScore", 999)
 
--change a default to some new value
_G.lobal.currentLevel = 9
--save it
saveDefaults()
--reload it
loadDefaults()


Replies

jeff472
User offline. Last seen 1 year 9 weeks ago. Offline
Joined: 2 Dec 2011

>>The glass crystal on a Replica Watches is often not very scratch-resistant<<

Fascinating.
Glad I could be of help.

peach pellen
User offline. Last seen 1 year 17 weeks ago. Offline
Alumni
Joined: 12 Apr 2011

Haha - spam removed. (Even fascinating spam loses appeal after 30 posts ;))

AlexM2
User offline. Last seen 1 year 1 week ago. Offline
Joined: 13 Sep 2012

Fyi there's two of those identical spam posts on other code echange posts.