I am using Rob’s tutorial for Displaying and Saving Score. It is awesome and I have it working for my first level without widget buttons.
Now I would like to modify the code to save the scores for multiple levels. I thought about changing the variable name of score to score1, score2 etc… but I wasn’t sure how to set that up in the save, load and set functions. I’ve tried various things to no avail.
Do I need to create a seperate M.save() , M.load() and set() function for each of the game levels or can the existing code be modified to save the scores for multiple game levels using the same three functions?
The code in question:
25.function M.set( value )
-
M.score = value
-
M.scoreText.text = string.format( M.format, M.score )
28.end
39.function M.save()
-
local path = system.pathForFile( M.filename, system.DocumentsDirectory )
-
local file = io.open(path, “w”)
-
if ( file ) then
-
local contents = tostring( M.score )
-
file:write( contents )
-
io.close( file )
-
return true
-
else
-
print( "Error: could not read ", M.filename, “.” )
-
return false
-
end
51.end
53.function M.load()
-
local path = system.pathForFile( M.filename, system.DocumentsDirectory )
-
local contents = “”
-
local file = io.open( path, “r” )
-
if ( file ) then
-
– read all contents of file into a string
-
local contents = file:read( “*a” )
-
local score = tonumber(contents);
-
io.close( file )
-
return score
-
else
-
print( "Error: could not read scores from ", M.filename, “.” )
-
end
-
return nil
67.end
Thanks.
Lori