Saving/Loadind Data - Easier way?

Hi, I've seen the Techority(Peach Pellen's blog) and I'd like to know if there is any easier way to save and load data to my app. I'd like to save A LOT of stuff, so I guess I'd have to create a lot of files for save all the data, which could be not so cool.

My Ice library might be able to help you - http://developer.anscamobile.com/code/ice

I absolutely recommend Ice library. It's insanely easy to use and amazingly robust system. I struggled to understand json, and I got it working, but honestly, I wish Ice came along before I dealt with save/load implementation. It would've saved days of headache.

Thanks, I took a glimpse at the ice library and it seems so efficient.

I'd like to thank you, like, A LOT.

For future reference there is also Jon Beebe's Games Class - that is very neat :)
http://jonbeebe.tumblr.com/post/2320935925/beebegames-class-for-corona-sdk

I haven't tried Ice but I used to use JSON but didn't fully understand it. Now I've switched to SQLite/Lua Tables and haven't looked back.

If you are willing to take some time to figure out how to get it all set up and working, it works great!

Using JSON is uber easy.

Here is the settings.lua file I'm using with a game I'm currently developing. I've not fully tested it, but:

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
module(..., package.seeall)
 
--
-- settings.lua - Load/Save game settings
--
 
local json = require("json")
 
local mySettings = {}
mySettings.playername = "player"
mySettings.password = ""
mySettings.firstname = ""
mySettings.lastname = ""
mySettings.email = ""
mySettings.sessionID = ""
mySettings.offline = false
mySettings.hasRegistered = false
mySettings.rememberLogin = false
mySettings.hasRated = false
mySettings.soundFx = true
mySettings.music = true
mySettings.scores = {0,0,0,0,0,0,0,0,0,0}
 
local settingsFile = "settings.json"
 
 
local function saveSettings()
    local path = system.pathForFile( settingsFile, system.DocumentsDirectory)
    local file = io.open(path, "w")
    if file then
        local contents = json.encode(mySettings)
        file:write( contents )
        io.close( file )
        return true
    else
        return false
    end
end
 
function loadSettings()
    local path = system.pathForFile( settingsFile, system.DocumentsDirectory)
    local contents = ""
    local file = io.open( path, "r" )
    if file then
        -- read all contents of file into a string
        local contents = file:read( "*a" )
        mySettings = json.decode(contents);
        io.close( file )
        if mySettings.hasRated == nil then
            mySettings.hasRated = false
        end
        if mySettings.soundFx == nil then
            mySettings.soundFx = true
        end
        if mySettings.music == nil then
            mySettings.music = true
        end
        if mySettings.playername == nil then
            mySettings.playername = "player"
        end
        if mySettings.password == nil then
            mySettings.password = ""
        end
        if mySettings.firstname == nil then
            mySettings.firstname = ""
        end
        if mySettings.lastname == nil then
            mySettings.lastname = ""        
        end
        if mySettings.email == nil then
            mySettings.email = ""
        end
        if mySettings.sessionID == nil then
            mySettings.sessionID = ""
        end
        if mySettings.offline == nil then
            mySettings.offline = false
        end
        if mySettings.hasRegsitered == nil then
            mySettings.hasRegistered = false
        end
        if mySettings.rememberLogin == nil then
            mySettings.rememberLogin = false
        end
        else
                saveSettings() -- initialize the settings!
        end
        return mySettings
end

hey rob you can simplify your code more by changing

if mySettings.hasRated == nil then
mySettings.hasRated = false

to

mySettings.hasRated = mySettings.hasRated or false

this will remove alot of the if statements and run better

views:1743 update:2011/10/22 9:46:13
corona forums © 2003-2011