So I am making a game which has a lot of values to save. I used a module by Rob Miracle with makes it easy to save and load tables to do this with two tables. One is named “gamestuff” and the other is named “leveltries”.
In one scene I declare the values of these tables (only if they haven’t been saved before)
gamestuff.level = 1
gamestuff.sound = true
gamestuff.name = “user”
gamestuff.highscore = 0
gamestuff.lastscore = 0
gamestuff.volume = 0.5
gamestuff.lastlevel = 1
gamestuff.won = false
gamestuff.bouncesleft = 0
local leveltries = {
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
}
loadsave.saveTable(leveltries, “leveltries.json”)
loadsave.saveTable(gamestuff, “gamestuff.json”)
This works fine except for when I try to go to a restart level scene and a win scene after the first level.
But it confuses me because in level one I try
print("Fails: "…leveltries[gamestuff.lastlevel].fails)
and it works
But when I try it in the restarter scene, it doesn’t:
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local loadsave = require(“loadsave”)
local gamestuff = {}
local leveltries = {}
local options =
{
effect = “fade”,
time = 400
}
local function loader()
gamestuff = loadsave.loadTable(“gamestuff.json”)
leveltries = loadsave.loadTable(“leveltries.json”)
print(“loaded”)
end
local function save()
loadsave.saveTable(gamestuff, “gamestuff.json”)
loadsave.saveTable(leveltries, “leveltries.json”)
end
function scene:createScene(event)
gamestuff = {}
leveltries = {}
end
function scene:enterScene(event)
loader()
print(“Fails”…leveltries[gamestuff.lastlevel].fails) --says the error is here
local a = leveltries[gamestuff.lastlevel].fails + 1
leveltries[gamestuff.lastlevel].fails = a
timer.performWithDelay( 50, function()
save()
storyboard.gotoScene(“level”…tostring(gamestuff.lastlevel), options)
end, 1 )
end
function scene:exitScene(event)
local group = self.view
storyboard.removeScene(“restarter”)
end
function scene:destroyScene(event)
gamestuff = nil
leveltries = nil
options = nil
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene
Can anyone tell my why the code above doesn’t work?
Could it be that I don’t properly remove the “gamestuff” and “leveltries” in level1?