How to Save and Load Game Progress?

Hi guys,

In a game it’s essentiall to save progress, I looked at de data/file/save and the json tutorial, but it still doesn’t work.

I just want to save a table that holds variables. These variables are changed with functions. Every time a player completes a level, another level is unlocked. I need to save these unlocked levels, so the levels are still unlocked when a player restarts the game.

How can I accomplish this?

If you need more information please tell me.

Thanks in advance.

There are a lot of ways to do this, and some (myself included) have written libraries to help.  My library, GBC Data Cabinet, will allow you to read/store data to storage.  If you would rather roll your own, check out the io library… I believe there are also examples there.

–john

Thanks the GBC Data Cabinet looks good. If I want to save a table, should I put the table name as the value of the Cabinet?

There is also GGData

Everything you need to know is here, including step by step instructions, but basically, you create a cabinet (using nay name you want), then call save to write it to storage.

Hope this helps!  If you need anything, please let me know.

–John

I have a table named “Gamescore” where should I put that in de code to save that table?

Hi,

Check out this bit of code… it’s a bit rough, but should work.  Basically, create a cabinet, set some data, and save it.  All data added to the cabinet will be saved.   This goes anywhere you want to save… create a save high score function, or something like that.

local success success = GBCDataCabinet.createCabinet("Gamescore") if success then success = GBCDataCabinet.set("Gamescore", "MyScore", score) if success then success = GBCDataCabinet.save("Gamescore") end end

–john

Thanks! I’ll try and see if it works.

[quote=“schizoid2k,post:7,topic:338662”]

Hi,
 
Check out this bit of code… it’s a bit rough, but should work.  Basically, create a cabinet, set some data, and save it.  All data added to the cabinet will be saved.   This goes anywhere you want to save… create a save high score function, or something like that.

local success success = GBCDataCabinet.createCabinet("Gamescore") if success then success = GBCDataCabinet.set("Gamescore", "MyScore", score) if success then success = GBCDataCabinet.save("Gamescore") end end

–john [/quote]

Ok thanks I get that, but it doesn’t save. I don’t get an error and I don’t know what I’m doing wrong. When I relaunch the simulator the Gamescore is back to 0 again. Sorry I’m quite new to coding with .lua, do it for about two weeks now.
 
Here’s my code:

local GBCDataCabinet = require("plugin.GBCDataCabinet") G = { Gamescore1=0, Gamescore2=0, Gamescore3=0, Gamescore4=0, Gamescore5=0, Gamescore6=0, Gamescore7=0, Gamescore8=0, Gamescore9=0, } --Gamescore1=0 function addscore() G.Gamescore1=G.Gamescore1+1 --Gamescore1=Gamescore1+1 ----------------------------------------------------------- end ---------------------------------------------------------- function addscore2() G.Gamescore2=G.Gamescore2+1 end --------------------------- function addscore3() G.Gamescore3=G.Gamescore3+1 end function addscore4() G.Gamescore4=G.Gamescore4+1 end function addscore5() G.Gamescore5=G.Gamescore5+1 end Gamescore6=0 function addscore6() G.Gamescore6=G.Gamescore6+1 end function addscore7() G.Gamescore7=G.Gamescore7+1 end function addscore8() G.Gamescore8=G.Gamescore8+1 end function addscore9() G.Gamescore9=G.Gamescore9+1 end local succes succes = GBCDataCabinet.createCabinet("G") if succes then succes = GBCDataCabinet.set("G", "MyScore", score) if succes then succes = GBCDataCabinet.save("G") end end succes = GBCDataCabinet.load("G") local value = GBCDataCabinet.get("G", "MyScore")

I streamlined your code a bit… check out these 3 code bits.

This code below will add 1 to your score table.  It also initializes your score table.

local GBCDataCabinet = require("plugin.GBCDataCabinet") -- table of scores local Scores = {} -- I combined all of you add score functions into a single function -- pass the appropriate position and it will update local function AddScore(ScorePos) Scores[ScorePos] = Scores[ScorePos] + 1 end -- initialize table of scores -- do this JUST ONE TIME, otherwise all scores will be reset for i = 1, 10 do Scores[i] = 0 end

This code below will create a cabinet and save scores to it.  It will also store the table so you can retrieve it later.

local GBCDataCabinet = require("plugin.GBCDataCabinet") local success -- create a cabinet names "Gamescores" success = GBCDataCabinet.createCabinet("Gamescores") if success then -- add one to the first score AddScore(1) -- add 1 to your 3rd score AddScore(3) -- set the scores table to the cabinet success = GBCDataCabinet.set("Gamescores", "Player Scores", Scores) if success then -- save the cabinet success = GBCDataCabinet.save("Gamescores") end end

Finally, this code below will allow you to retrieve the previously saved scores:

local GBCDataCabinet = require("plugin.GBCDataCabinet") success = GBCDataCabinet.load("Gamescores") if success then -- get the scores table local values = GBCDataCabinet.get("Gamescores", "Player Scores") -- print the table for i = 1, 10 do print (values[i]) end end

Note:

The above code is a test.  It should work, but optimally, you should first check to see if there is a previously saved cabinet and extract the data if it exists.  If there is not a previously saved cabinet, then create one.  

If you create a cabinet every time you run your app without checking if the cabinet exists, you risk wiping out all your saved data. This pseudo-code gives you an idea of what to do:

-- does a cabinet "Gamescores" exists? CabExist = GBCDataCab.load("Gamescores") -- if cabinet does not exist, then create it. if CabExist == false then CabExist = GBCDataCab.createCabinet("Gamescores") end -- if the cabinet does exist, or if it was just created, try to extract some data if CabExist then values = GBCDataCab.get("Gamescores", "Player Scores") -- if values is not nil, then you have data. Use it. else -- if values is nil, then you do not have any data. -- Probably because this is the first time running the app. -- initialize your variables here end

Hope this helps

–John

There are a lot of ways to do this, and some (myself included) have written libraries to help.  My library, GBC Data Cabinet, will allow you to read/store data to storage.  If you would rather roll your own, check out the io library… I believe there are also examples there.

–john

Thanks the GBC Data Cabinet looks good. If I want to save a table, should I put the table name as the value of the Cabinet?

There is also GGData

Everything you need to know is here, including step by step instructions, but basically, you create a cabinet (using nay name you want), then call save to write it to storage.

Hope this helps!  If you need anything, please let me know.

–John

I have a table named “Gamescore” where should I put that in de code to save that table?

Hi,

Check out this bit of code… it’s a bit rough, but should work.  Basically, create a cabinet, set some data, and save it.  All data added to the cabinet will be saved.   This goes anywhere you want to save… create a save high score function, or something like that.

local success success = GBCDataCabinet.createCabinet("Gamescore") if success then success = GBCDataCabinet.set("Gamescore", "MyScore", score) if success then success = GBCDataCabinet.save("Gamescore") end end

–john

Thanks! I’ll try and see if it works.

[quote=“schizoid2k,post:16,topic:338662”]

Hi,
 
Check out this bit of code… it’s a bit rough, but should work.  Basically, create a cabinet, set some data, and save it.  All data added to the cabinet will be saved.   This goes anywhere you want to save… create a save high score function, or something like that.

local success success = GBCDataCabinet.createCabinet("Gamescore") if success then success = GBCDataCabinet.set("Gamescore", "MyScore", score) if success then success = GBCDataCabinet.save("Gamescore") end end

–john [/quote]

Ok thanks I get that, but it doesn’t save. I don’t get an error and I don’t know what I’m doing wrong. When I relaunch the simulator the Gamescore is back to 0 again. Sorry I’m quite new to coding with .lua, do it for about two weeks now.
 
Here’s my code:

local GBCDataCabinet = require("plugin.GBCDataCabinet") G = { Gamescore1=0, Gamescore2=0, Gamescore3=0, Gamescore4=0, Gamescore5=0, Gamescore6=0, Gamescore7=0, Gamescore8=0, Gamescore9=0, } --Gamescore1=0 function addscore() G.Gamescore1=G.Gamescore1+1 --Gamescore1=Gamescore1+1 ----------------------------------------------------------- end ---------------------------------------------------------- function addscore2() G.Gamescore2=G.Gamescore2+1 end --------------------------- function addscore3() G.Gamescore3=G.Gamescore3+1 end function addscore4() G.Gamescore4=G.Gamescore4+1 end function addscore5() G.Gamescore5=G.Gamescore5+1 end Gamescore6=0 function addscore6() G.Gamescore6=G.Gamescore6+1 end function addscore7() G.Gamescore7=G.Gamescore7+1 end function addscore8() G.Gamescore8=G.Gamescore8+1 end function addscore9() G.Gamescore9=G.Gamescore9+1 end local succes succes = GBCDataCabinet.createCabinet("G") if succes then succes = GBCDataCabinet.set("G", "MyScore", score) if succes then succes = GBCDataCabinet.save("G") end end succes = GBCDataCabinet.load("G") local value = GBCDataCabinet.get("G", "MyScore")

I streamlined your code a bit… check out these 3 code bits.

This code below will add 1 to your score table.  It also initializes your score table.

local GBCDataCabinet = require("plugin.GBCDataCabinet") -- table of scores local Scores = {} -- I combined all of you add score functions into a single function -- pass the appropriate position and it will update local function AddScore(ScorePos) Scores[ScorePos] = Scores[ScorePos] + 1 end -- initialize table of scores -- do this JUST ONE TIME, otherwise all scores will be reset for i = 1, 10 do Scores[i] = 0 end

This code below will create a cabinet and save scores to it.  It will also store the table so you can retrieve it later.

local GBCDataCabinet = require("plugin.GBCDataCabinet") local success -- create a cabinet names "Gamescores" success = GBCDataCabinet.createCabinet("Gamescores") if success then -- add one to the first score AddScore(1) -- add 1 to your 3rd score AddScore(3) -- set the scores table to the cabinet success = GBCDataCabinet.set("Gamescores", "Player Scores", Scores) if success then -- save the cabinet success = GBCDataCabinet.save("Gamescores") end end

Finally, this code below will allow you to retrieve the previously saved scores:

local GBCDataCabinet = require("plugin.GBCDataCabinet") success = GBCDataCabinet.load("Gamescores") if success then -- get the scores table local values = GBCDataCabinet.get("Gamescores", "Player Scores") -- print the table for i = 1, 10 do print (values[i]) end end

Note:

The above code is a test.  It should work, but optimally, you should first check to see if there is a previously saved cabinet and extract the data if it exists.  If there is not a previously saved cabinet, then create one.  

If you create a cabinet every time you run your app without checking if the cabinet exists, you risk wiping out all your saved data. This pseudo-code gives you an idea of what to do:

-- does a cabinet "Gamescores" exists? CabExist = GBCDataCab.load("Gamescores") -- if cabinet does not exist, then create it. if CabExist == false then CabExist = GBCDataCab.createCabinet("Gamescores") end -- if the cabinet does exist, or if it was just created, try to extract some data if CabExist then values = GBCDataCab.get("Gamescores", "Player Scores") -- if values is not nil, then you have data. Use it. else -- if values is nil, then you do not have any data. -- Probably because this is the first time running the app. -- initialize your variables here end

Hope this helps

–John