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