Here’s a link to something that might be useful.
http://developer.anscamobile.com/forum/2011/02/02/how-can-i-easily-implement-save-and-load-system-0
Make sure you check out the link above before you start reading
further.
All Done with SQLite
This is how I use it.
[lua]–// Step 1.
– just as Ricardo suggested, I created a settings.lua with the SQLite3
– in my main.lua, I put the following at the top
local director = require (“director”)
local settings = require(“settings”)
– _G.newGame = false //this is a global variable i used in my game to detect a new game so I can reset all my local score/bullets/life etc.
– Create a main group
local mainGroup = display.newGroup()
– Main function
local function main()
– Add the group from director class
mainGroup:insert(director.directorView)
director:changeScene( “loadmainmenu2” )
return true
end
–// Step 2:
– In my level1.lua, I save the highScore/life/gameScore/ and etc
– when the game first started so I can carry though variables to next level…
– this is how I do it.
– initial value for my variables
local bestScore = 0
local gameScore = 0
– gameCount is life…LOL… i started with gameCount and just kind of ran with it.
local gameCount = 5
local levelBullet = 25
local nextLevel = “level2” – this is module level2.lua
local gameInit = function()
–==================== begin savings/loading process =============================
– HIGH SCORE OR SAVE IT
if type(settings:loadVar(“highscores”)) == “nil” then
settings:saveVar( “highscores”, bestScore )
else
local loadedBestScore = settings:loadVar(“highscores”)
bestScore = tonumber(loadedBestScore)
end
– GAME SCORE OR SAVE IT
settings:saveVar( “gamescore”, gameScore )
– CANNONBALL LEFT OR SAVE IT
settings:saveVar( “bullet”, levelBullet )
– LIFE LEFT OR SAVE IT
settings:saveVar( “life”, gameCount )
– add whatever codes you want in here if any.
end
–// Step 3. …still in level1… when the player beats the
– level then update the above Variables into your Database.
– Notes: This may not work for you game, but in my game, I only
– update the Variables if the player “click” the next Level Button.
– Example: if the player restart the level, the old
– life/gameScore/bestScore/ etc will be loaded… Not sure if this make sense.
– in my enterFrame function
– i created this to update my highscore, but not save it until
– the player hit the next level button
local loopGame = function ()
if gameScore > bestScore then
local newScore = gameScore
setBest( newScore )
end
end
–// step 4:
– update the Variables when player hit next level button
local function onlevelTouch (event)
– this is my globale variable may not be applicable to your game
_G.newGame = false
– save the high Score
settings:saveVar( “highscores”, bestScore )
– save the level completed
settings:saveVar( “level”, nextLevel)
– GAME SCORE OR SAVE IT
settings:saveVar( “gamescore”, gameScore )
– CANNONBALL LEFT OR SAVE IT
settings:saveVar( “bullet”, levelBullet )
– LIFE LEFT OR SAVE IT
settings:saveVar( “life”, gameCount )
– end of savings process
– add whatever you want it to happen when player hit next button here
end
–// step 5: level2.lua module…
local bestScore = tonumber(settings:loadVar(“highscores”))
local gameScore = tonumber(settings:loadVar(“gamescore”))
local levelBullet = tonumber( settings:loadVar(“bullet”))
local gameCount = tonumber(settings:loadVar(“life”))
local nextLevel = “level3”
–finally: when the player beats level2 then just save it using the same method i mentioned above.
[import]uid: 12455 topic_id: 7917 reply_id: 28218[/import]