Im using storyboard and i would like to implement a feature that saves and loads the total amount of times the player has died as a stat that accumulates until the player has removed the game from device. The problem im having though is figuring out the logic behind doing this.
I run this in my main
-- load the JSON library. local json = require("json") -- Function to save a table. Since game settings need to be saved from session to session, we will -- use the Documents Directory function saveTable(t, filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) return true else return false end end function loadTable(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end
In my level i use this to create my file and save the amount of deaths that accured during that level
--Runtime Listener. local function saveData () myGameStats = {} myGameStats.totalDeaths = numberDeaths.text saveTable(myGameStats, "mygamestats.json") end
Function that handles incrementing the amount of deaths
--Check for collision with player and spikes. local function spikeDeath(event) if event.phase == "began" then if(event.object1.myName == "spike" and event.object2.myName == "player") then print ("You died from spikes") death = true end elseif event.phase == "ended" then if(event.object1.myName == "spike" and event.object2.myName == "player") then death = false numberDeaths.text = numberDeaths.text + 1 end end end
function scene:createScene( event ) local screenGroup = self.view numberDeaths = display.newText(0, 0, 0, native.systemFont, 32) numberDeaths.text = 0 numberDeaths.x = \_W - 248 numberDeaths.y = \_H - 66 numberDeaths:setFillColor(255,0,0) screenGroup:insert( numberDeaths ) end
My issue is that when i restart my game “myGameStats.totalDeaths” resets to 0 again. Im not sure how to handle this properly. Anyone think they can help with this?