Highscore returns "nil"

I’m trying to get my high score displayed at the game over screen, but it shows nil every time. On both the game screen and game over screen, it shows "High Score: nil " And this is after a few rounds of gameplay, it doesn’t look like it’s saving. Can someone find what I’m doing wrong? All the scoring scripts…

-- GAME SCREEN --   
local score = 0   
local scoreText  
local highScore   
local highScoreText  
  
function loadHighScore()   
 local highScoreFilename = "highScore.data"  
 local loadedHighScore = loadValue( highScoreFilename )   
 highScore = tonumber(loadedHighScore)   
end   
  
local scoreText = display.newText("0", 0, 0, native.systemFont, 24)   
--scoreText.x/y values  
  
local highScoreText = display.newText("High Score: " .. tostring(highScore, 0, 0, native.systemFont, 24)   
--highScoreText.x/y values  
  
-- How the score is added   
score = score + 25  
scoreText.text = tostring( score )   
  
-- GAME OVER SCREEN --   
scoreText.isVisible = false   
scoreText.text = "Score: " .. tostring( score )   
scoreText.x = 50; scoreText.y = display.contentHeight/2   
scoreText:toFront()   
timer.performWithDelay( 0, function() scoreText.isVisible = true;end, 1)   
  
highScoreText = display.newText("High Score: " .. tostring( highScore ), 50, scoreText.y + 25, native.systemFontBold, 24)   
highScoreText:setTextColor( 255, 0, 0 )   

Save/Load values

[code]
– Save Value
local saveValue = function( strFilename, strValue )
– will save specified value to specified file
local theFile = strFilename
local theValue = strValue

local path = system.pathForFile( theFile, system.DocumentsDirectory )

– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “w+” )
if file then
– write game score to the text file
file:write( theValue )
io.close( file )
end
end

– Load Value
local loadValue = function( strFilename )
– will load specified file, or create new file if it doesn’t exist

local theFile = strFilename

local path = system.pathForFile( theFile, system.DocumentsDirectory )

– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
io.close( file )
return contents
else
– create file b/c it doesn’t exist yet
file = io.open( path, “w” )
file:write( “0” )
io.close( file )
return “0”
end
end
[import]uid: 114389 topic_id: 30713 reply_id: 330713[/import]

I can’t actually see there were you are saving the high score, only loading it - can you post more code, please? [import]uid: 52491 topic_id: 30713 reply_id: 123068[/import]

I also don’t see where you’re saving the score, unless it’s somewhere after line 32 in your first code block output.

Also, why a “.data” file? If you’re reading and writing basic info to a file, use a text file (.txt). It might not matter, but I can vouch that text files work for this purpose; I save and retrieve all kinds of game data to them and never have a problem.

Beyond this, you should test your code piece by piece to determine what might be wrong. Print the value of certain variables like “file” to the Terminal. If you see the expected value, then move to the next piece of the routine. Print that and see the result. This is the best way to determine where one thing is going “nil” and how to fix it.

Brent Sorrentino
[import]uid: 9747 topic_id: 30713 reply_id: 123103[/import]

I guess I don’t know what to do on how to save the high score, does it need it’s separate function? And thanks for the advice with the “.txt” file instead of “.data” … in all of the tutorials and source codes I see .data, so I just figured.

In the previous code, lines 21 and 22 adds score on collision.

I tried this but of course it didn’t work…

[code]
local setScore = function( scoreNum )
local newScore = scoreNum
gameScore = newScore
if gameScore < 0 then gameScore = 0; end
scoreText.text = "Score: " … tostring( gameScore )
scoreText.xScale = 0.5; scoreText.yScale = 0.5
scoreText.x = (scoreText.contentWidth * 0.5) + 15
scoreText.y = 15
end
end

–On collision (Scoring)
local newScore = gameScore + 500
setScore( newScore )
[/code] [import]uid: 114389 topic_id: 30713 reply_id: 123216[/import]

Hi Silky,

Now I’m confused on what’s not working. Does the save function (saveValue) you wrote overwrite the text file with the proper score value? Does the load function retrieve the value from the file? How does this relate to the latest piece of code you posted, “setScore”? I think we should approach this all one step at a time to solve the entire puzzle.

Brent
[import]uid: 9747 topic_id: 30713 reply_id: 123226[/import]

I probably shouldn’t of added that last piece of code, it’s just something I tried to “save” the high score but didn’t work, the first code I posted is the one currently used. When the game over screen appears, it shows "High Score = nil " … I also displayed the “High Score” text on the game screen and it showed the same thing, nil. I can’t get it to save the score. [import]uid: 114389 topic_id: 30713 reply_id: 123230[/import]

OK, what I suggest then is that you “print debug” your code, step by step. For example, after line 7 in your code (in the saveValue function), print(path) to the Terminal and see what it is. If it’s nil, then you know the problem is there… it’s not reading the file for some reason. If that part IS working, move to line 10 and print(file). See if that comes up valid or nil. Repeat this process through both your save and load functions, and it should determine where/why it’s not working. :slight_smile:

Brent
[import]uid: 9747 topic_id: 30713 reply_id: 123270[/import]

I can’t actually see there were you are saving the high score, only loading it - can you post more code, please? [import]uid: 52491 topic_id: 30713 reply_id: 123068[/import]

I also don’t see where you’re saving the score, unless it’s somewhere after line 32 in your first code block output.

Also, why a “.data” file? If you’re reading and writing basic info to a file, use a text file (.txt). It might not matter, but I can vouch that text files work for this purpose; I save and retrieve all kinds of game data to them and never have a problem.

Beyond this, you should test your code piece by piece to determine what might be wrong. Print the value of certain variables like “file” to the Terminal. If you see the expected value, then move to the next piece of the routine. Print that and see the result. This is the best way to determine where one thing is going “nil” and how to fix it.

Brent Sorrentino
[import]uid: 9747 topic_id: 30713 reply_id: 123103[/import]

I guess I don’t know what to do on how to save the high score, does it need it’s separate function? And thanks for the advice with the “.txt” file instead of “.data” … in all of the tutorials and source codes I see .data, so I just figured.

In the previous code, lines 21 and 22 adds score on collision.

I tried this but of course it didn’t work…

[code]
local setScore = function( scoreNum )
local newScore = scoreNum
gameScore = newScore
if gameScore < 0 then gameScore = 0; end
scoreText.text = "Score: " … tostring( gameScore )
scoreText.xScale = 0.5; scoreText.yScale = 0.5
scoreText.x = (scoreText.contentWidth * 0.5) + 15
scoreText.y = 15
end
end

–On collision (Scoring)
local newScore = gameScore + 500
setScore( newScore )
[/code] [import]uid: 114389 topic_id: 30713 reply_id: 123216[/import]

Hi Silky,

Now I’m confused on what’s not working. Does the save function (saveValue) you wrote overwrite the text file with the proper score value? Does the load function retrieve the value from the file? How does this relate to the latest piece of code you posted, “setScore”? I think we should approach this all one step at a time to solve the entire puzzle.

Brent
[import]uid: 9747 topic_id: 30713 reply_id: 123226[/import]

I probably shouldn’t of added that last piece of code, it’s just something I tried to “save” the high score but didn’t work, the first code I posted is the one currently used. When the game over screen appears, it shows "High Score = nil " … I also displayed the “High Score” text on the game screen and it showed the same thing, nil. I can’t get it to save the score. [import]uid: 114389 topic_id: 30713 reply_id: 123230[/import]

OK, what I suggest then is that you “print debug” your code, step by step. For example, after line 7 in your code (in the saveValue function), print(path) to the Terminal and see what it is. If it’s nil, then you know the problem is there… it’s not reading the file for some reason. If that part IS working, move to line 10 and print(file). See if that comes up valid or nil. Repeat this process through both your save and load functions, and it should determine where/why it’s not working. :slight_smile:

Brent
[import]uid: 9747 topic_id: 30713 reply_id: 123270[/import]