Reputation: 819
I have a bunch of variables like "Upgrades bought", "Amount of money" etc. I want the most efficient way to save these variables and load them upon starting the game, as you never even exited the game (every single thing stays the same). So, all the settings and variables stay the same, until you reset the game.
I am asking since I think this is a huge and really important part and I want to begin with the best technique.
What are your suggestions and how can I implement that in my game?
Upvotes: 1
Views: 2344
Reputation: 521
Use any serializer or make your own:
function deepStructure (tabl, struct)
local str = struct.." = {}" .. '\n'
for i, v in pairs (tabl) do
local index = type (i) == "string" and '.'..i or '['..i..']'
if type (v) == "table" then
str = str .. deepStructure (v, struct..index) .. '\n'
elseif (type (v) == "string") then
str = str .. struct .. index .. ' = ' .. '"'..tostring (v)..'"' .. '\n'
else
str = str .. struct .. index .. ' = ' .. tostring (v) .. '\n'
end
end
return string.sub(str, 1, -2)
end
Example:
data = {1,2,3, t = {a="a", b="b", c=true, {7, true, "text"}}}
print (deepStructure (data, "data"))
Result:
data = {}
data[1] = 1
data[2] = 2
data[3] = 3
data.t = {}
data.t[1] = {}
data.t[1][1] = 7
data.t[1][2] = true
data.t[1][3] = "text"
data.t.b = "b"
data.t.a = "a"
data.t.c = true
As you see, they have the same structure until functions or dashes in name.
Upvotes: 0
Reputation: 1683
There are two ways to do fairly easily:
Using a simple text file saved to your DocumentDirectory:
local filePath = system.pathForFile( "data.txt", system.DocumentsDirectory )
local file = io.open( filePath, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
print( "Contents of " .. filePath )
print( contents )
io.close( file )
local t = display.newText( "Contents of ", 5, 80, nil, 16 );
t:setFillColor( 1, 1, 136/255 );
local t = display.newText( filePath, 5, 100, nil, 10 );
t:setFillColor( 1, 1, 136/255 );
local ylast = 130
for line in io.lines(filePath) do
local t = display.newText( line, 15, ylast, nil, 14 );
t:setFillColor( 1, 1, 1 );
ylast = ylast + 20
end
else
print( "Creating file..." )
-- create file b/c it doesn't exist yet
file = io.open( filePath, "w" )
file:write( "Feed me data!\n" )
local numbers = {1,2,3,4,5,6,7,8,9}
file:write( numbers[1], numbers[2], numbers[3], "\n" )
for _,v in ipairs( numbers ) do
file:write( v, " " )
end
file:write( "\nNo more data\n" )
io.close( file )
local t = display.newText( "Created file at:", 5, 80, nil, 16 );
t:setFillColor( 1, 1, 136/255 );
local t = display.newText( filePath, 5, 100, nil, 10 );
t:setFillColor( 1, 1, 136/255 );
local t = display.newText( "Run app again to test file read.", 5, 130, nil, 12 );
t:setFillColor( 1, 1, 136/255 );
-- This removes the file just created
-- os.remove( filePath )
end
2) Using a sqlite file saved to your DocumentDirectory:
--Include sqlite
require "sqlite3"
--Open data.db. If the file doesn't exist it will be created
local path = system.pathForFile("data.db", system.DocumentsDirectory)
db = sqlite3.open( path )
--Handle the applicationExit event to close the db
local function onSystemEvent( event )
if( event.type == "applicationExit" ) then
db:close()
end
end
--Setup the table if it doesn't exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]
print(tablesetup)
db:exec( tablesetup )
--Add rows with a auto index in 'id'. You don't need to specify a set of values because we're populating all of them
local testvalue = {}
testvalue[1] = 'Hello'
testvalue[2] = 'World'
testvalue[3] = 'Lua'
local tablefill =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[2]..[['); ]]
local tablefill2 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[2]..[[',']]..testvalue[1]..[['); ]]
local tablefill3 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[3]..[['); ]]
db:exec( tablefill )
db:exec( tablefill2 )
db:exec( tablefill3 )
--print the sqlite version to the terminal
print( "version " .. sqlite3.version() )
--print all the table contents
for row in db:nrows("SELECT * FROM test") do
local text = row.content.." "..row.content2
local t = display.newText(text, 20, 120 + (20 * row.id), native.systemFont, 16)
t:setFillColor(1,0,1)
end
You can also use a cloud based which is virtually the same thing with more mobility.
Upvotes: 0
Reputation: 26744
You can store them as table fields and then serialize the table using one of the many options for serializers. See also the Serialization chapter in Programming in Lua.
Upvotes: 1