Hi, I’ve always used OF for my score keeping, but the recent error I got when submitting to the app store (while using the new gameNetwork) is making me want to implement a local score/highscore that will save to file, and then read to file when I need to display it at a gameover.lua (or any other screen, such as menu, etc.)
I think I’m making some basic mistakes here, but wanted to find out if anyone could point me in the right direction for this;
menu.lua items (to call up score from score.txt file)
[lua]loadScores = function( )
local scores = {}
local str = “”
local n = 1
local path = system.pathForFile(“score.txt”, system.DocumentsDirectory)
local file = io.open ( path, “r” )
if file == nil then
return 0, 0
end
local contents = file:read( “*a” )
file:close()
for i = 1, string.len( contents ) do
local char = string.char( string.byte( contents, i ) )
if char ~= “|” then
str = str…char
else
scores[n] = tonumber( str )
n = n + 1
str = “”
end
end
return scores[1], scores[2]
end
– DISPLAY SCORE/HIGHSCORE –
score, highScore = loadScores( )
local lastScoreLabel = display.newText("Last Score " … score, 0, 0, native.systemFont, 15 )
lastScoreLabel.x = display.viewableContentWidth / 2
lastScoreLabel.y = 20
lastScoreLabel:setTextColor(255, 139, 0)
localGroup:insert(lastScoreLabel)
local highScoreLabel = display.newText("High Score " … highScore, 0, 0, native.systemFont, 15 )
highScoreLabel.x = display.viewableContentWidth / 2
highScoreLabel.y = lastScoreLabel.y + lastScoreLabel.height
highScoreLabel:setTextColor( 0, 0, 0, 100 )
localGroup:insert(highScoreLabel)[/lua]
game.lua items (to save score/highscore to a score.txt file located on device)
[lua]saveScores = function( s, hs )–{{{
local path = system.pathForFile( “score.txt”, system.DocumentsDirectory )
local file = io.open ( path, “w” )
local contents = tostring( s )…"|"…tostring( hs)…"|"
file:write( contents )
file:close( )
end
loadScores = function(score, highScore)
if score > highScore then
highScore = score
savetoFile(highScore)
end
end
saveScores(score, highScore)[/lua]
gameover.lua (to display score/highscore at the end of the game)
[lua]loadScores = function(score, highScore)–{{{
local scores = {}
local str = “”
local n = 1
local path = system.pathForFile( “score.txt”, system.DocumentsDirectory )
local file = io.open ( path, “r” )
if file == nil then
return 0, 0
end
local contents = file:read( “*a” )
file:close()
for i = 1, string.len( contents ) do
local char = string.char( string.byte( contents, i ) )
if char ~= “|” then
str = str…char
else
scores[n] = tonumber( str )
n = n + 1
str = “”
end
end
return scores[1], scores[2]
end --}}}
score, highScore = loadScores( )
– DISPLAY LAST AND HIGHSCORE ON SCREEN ------------------------------
local lastScoreLabel = display.newText("Final Score " … score, 0, 0, native.systemFont, 15 )
lastScoreLabel.x = display.viewableContentWidth / 2
lastScoreLabel.y = 20
lastScoreLabel:setTextColor(255, 139, 0)
localGroup:insert(lastScoreLabel)
local highScoreLabel = display.newText(“Record Score” … highScore, 0, 0, native.systemFont, 15 )
highScoreLabel.x = display.viewableContentWidth / 2
highScoreLabel.y = lastScoreLabel.y + lastScoreLabel.height
highScoreLabel:setTextColor(255, 139, 0)
localGroup:insert(highScoreLabel)[/lua]
in both instances in menu.lua and gameover.lua if I l don’t uncomment the below
[lua]local highScoreLabel = display.newText(“Record Score” … highScore, 0, 0, native.systemFont, 15 )[/lua]
I get an error “attempt to concatenate global ‘highScore’ (a nil value)”
So I guess my question is; what am I missing from the above to work properly… or do I have it all wrong?
Thanks in advance for the help or direction! [import]uid: 10379 topic_id: 16695 reply_id: 316695[/import]
hopefully I’ll get it faster than debug of the above [import]uid: 10379 topic_id: 16695 reply_id: 62481[/import]
