Saving your game settings

Any game must keep it’s settings. This is my method of saving settings, it keeps values in SQLite database. I hope it’ll be useful for you, it’s simple itself and simple for use too.

There are two files. The first – default_values.lua – it keeps initial settings values for your game, like a sound/music on/off, game name etc.
The second – settings.lua – manage settings, how to use it see at bottom.

default_values.lua

1
2
3
4
5
6
7
8
module(..., package.seeall)

game = {
    name = "Run Rabbit Run",
    options = {sound=true, music=true},
    money = 0,
    best_score = 0,
}

settings.lua

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
module(..., package.seeall)

local db = {}
game = {}

function settings:init()

    local path = system.pathForFile( "rrr_settings.sqlite", system.DocumentsDirectory )
    file = io.open( path, "r" )
    if( file == nil )then
        pathSource     = system.pathForFile( "db/rrr_settings.sqlite", system.ResourceDirectory )
        fileSource     = io.open( pathSource, "r" )
        contentsSource = fileSource:read( "*a" )
        pathDest = system.pathForFile( "rrr_settings.sqlite", system.DocumentsDirectory )
        fileDest = io.open( pathDest, "w" )
        fileDest:write( contentsSource )
        io.close( fileSource )
        io.close( fileDest )
    else
        io.close( file )
    end

    path = system.pathForFile( "rrr_settings.sqlite", system.DocumentsDirectory )
    db = sqlite3.open(path)
--db:exec('DROP TABLE settings')
    local sql = 'CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY, var UNIQUE, val);'
    db:exec(sql)
    game = get()
    if not game or table.count(game)==0 then
        g = require("default_values")
        game=copy(g.game)
    end
    if not game.player_name then
        local offset = nil
        local name = system.getInfo( "name" )
        if not offset then offset = string.find(name, "’s iPhone") end
        if not offset then offset = string.find(name, "’s iPad") end
        if not offset then offset = string.find(name, "’s iPod") end
        if offset then
            name = string.sub(name, 1, offset-1)
        end
        game.player_name = name
    end
    if not game.player_id then
        game.player_id = crypto.digest( crypto.md4, system.getInfo("deviceID"))
    end
    save()
end

function settings:reset_game()
    g=require("default_values")
    game = copy(g.game)
    save()
end

function settings:close()
       db:close()
end

function settings:get()
    local val = nil
    for result in db:nrows('SELECT val FROM settings WHERE var="game"') do
        val =  Json.Decode(result.val)
        val = val[1]
    end
    return val
end

function settings:save()
    local sql = 'INSERT OR REPLACE INTO settings(var, val) VALUES ("game", \''..Json.Encode({game})..'\')'
    db:exec(sql)
end

Usage:

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
-- include all required libraries
require "sqlite3"
require "Json"
crypto = require("crypto")
settings = require 'settings'
settings:init()

-- setting variables
settings.game.lives = 5
settings.game.options.sound = true
settings.game.any_variable = any_value

-- save all data in settings.game table to SQLite DB
settings:save()

-- access saved data
local is_sound_on = settings.game.options.sound
local player_lives = setting.game.lives
 -- get player's name based on device name (eg: if device name - "John's iPhone", player_name will be "John")
local player_name = settings.game.player_name
-- get player ID, based on deviceID code
local player_id = settings.game.player_id

-- reset all saved data, set default values from default_values.lua file
settings:reset_game()

One thought on “Saving your game settings

  1. Great job guys.
    I’ve read more and more but I still didn’t understand about sqLite for corona. How to use CRUD run on SQLite for coronaSDK?
    Regard

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>