How can i easily implement a save and load system

Hi
db file should be create when we start the game ?
how to restart it ? I can’t find and delete the file so i should do something like this :
settings:setVar (“levelunlocked”, 0) ?
Thank you
Solved:
the path to file :
/Users/yourusername/Library/Application Support/Corona Simulator/ [import]uid: 13156 topic_id: 5884 reply_id: 25483[/import]

Hello,

@Piotr Turlej: you can add a function into your settings.lua for delete al database data like:
[lua]-- DROP
function settings:resetTable ()
db:exec (“DELETE FROM tb_settings;”)
end[/lua]

Regards.
Francisco. [import]uid: 11749 topic_id: 5884 reply_id: 25668[/import]

Thank you ffm.
I think I add “reset game” button to start whole game once more. Thanks to you :):slight_smile:
[import]uid: 13156 topic_id: 5884 reply_id: 25671[/import]

can help me ?

i have 4 images which will be used as character
then i wanna user click one of the images, then
the image will be used for the rest of the scene?

[import]uid: 33180 topic_id: 5884 reply_id: 26211[/import]

Ricardo, thanks for posting this. In my game, I needed to load a bunch of local variables from a database. I used the settings.lua file above and created this function in my game.lua. I can easily add a variable to the database without a bunch of code.

--Load Game Data  
local loadData = function( var,var2 )  
  
 if type(settings:loadVar(var)) == "nil" then  
 settings:saveVar( var, 0 )  
 var2 = 0  
 else  
 settings:setVar( var, settings:loadVar(var) )  
 var2 = settings:getVar (var)  
 print(var .. " From Settings " .. var2)  
 end  
  
 return var2  
end  
  
--Populate local variables  
 local myVar1 = loadData( "myVar1", myVar1 )  
 local myVar2 = loadData( "myVar2", myVar2 )  
 local myVar3 = loadData( "myVar3", myVar3 )  

[import]uid: 15615 topic_id: 5884 reply_id: 27266[/import]

Boolean support added

Big thanks to Ricardo for posting this code! I’ve added datatype “boolean” support to the loadVar & saveVar functions so bools can be saved in the db (I needed to do this & thought others might):

-- LOAD  
function settings:loadVar( pVarName )  
 for row in db:nrows("SELECT \* FROM tb\_settings WHERE name = '" .. string.upper(pVarName) .. "'") do  
 if row.type == "N" then  
 return tonumber(row.value\_number)  
 elseif row.type == "C" then  
 return tostring(row.value\_string)  
 elseif row.type == "B" then  
 if row.value\_bool == 0 then  
 return false  
 else  
 return true  
 end  
 end  
 end  
 return nil  
end  
   
-- SAVE RECORD  
function settings:saveVar( pVarName, pVarValue )  
 --  
 if type(pVarValue) == "nil" then  
 pVarValue = tabVars[pVarName]  
 else  
 settings:setVar ( pVarName, pVarValue )  
 end  
 --  
 local vVarType  
 --  
 if type(pVarValue) == "number" then  
 vVarType = 'N'  
 elseif type(pVarValue) == "string" then  
 vVarType = 'C'  
 elseif type(pVarValue) == "boolean" then  
 vVarType = 'B'  
 else  
 -- this datatype is not supported for db load/save, just return  
 return true  
 end  
 --  
 for row in db:nrows("SELECT count(\*) as cont FROM tb\_settings WHERE name = '" .. string.upper(pVarName) .. "'") do  
 if row.cont \> 0 then  
 if vVarType == "N" then  
 db:exec ("UPDATE tb\_settings SET type = 'N', value\_number = " .. pVarValue .. " WHERE name = '" .. string.upper(pVarName) .. "';")  
 elseif vVarType == "C" then  
 db:exec ("UPDATE tb\_settings SET type = 'C', value\_string = '" .. pVarValue .. "' WHERE name = '" .. string.upper(pVarName) .. "';")  
 elseif vVarType == "B" then  
 intInsertVal = 0  
 if pVarValue == true then intInsertVal = 1 end  
 db:exec ("UPDATE tb\_settings SET type = 'B', value\_bool = '" .. intInsertVal .. "' WHERE name = '" .. string.upper(pVarName) .. "';")  
 end  
 else  
 if vVarType == "N" then  
 db:exec ("INSERT INTO tb\_settings ( name, type, value\_number) VALUES ('" .. string.upper(pVarName) .. "','N'," .. pVarValue .. ");")  
 elseif vVarType == "C" then  
 db:exec ("INSERT INTO tb\_settings ( name, type, value\_string) VALUES ('" .. string.upper(pVarName) .. "','C','" .. pVarValue .. "');")  
 elseif vVarType == "B" then  
 intInsertVal = 0  
 if pVarValue == true then intInsertVal = 1 end  
 db:exec ("INSERT INTO tb\_settings ( name, type, value\_bool) VALUES ('" .. string.upper(pVarName) .. "','B','" ..intInsertVal .. "');")  
 end  
 end  
 end  
end  
  

And on a related note, if you want to manage your SQLite database (e.g., look at it while running your program), the SQLite Manager firefox plugin seems to work very well. http://code.google.com/p/sqlite-manager/ [import]uid: 39515 topic_id: 5884 reply_id: 27678[/import]

I tried the database and saving to a text file, but ultimately i am using the Beebe class to save and load, its really easy! [import]uid: 19620 topic_id: 5884 reply_id: 28450[/import]