Reading/Writing to File, Saving High Score Problem

Hello, I am trying to add a scoring module to my game. I’m also trying to save the highscore. 

The current score module works just fine and updates every time to the current score but the highscore only gets updated once per run. next time I run it, it does not updates it and statys the same here is my code:

 function scoreCode() --saving current score to the file local path local file path = system.pathForFile ( "score.txt", system.DocumentsDirectory) file = io.open (path, "w") file:write(score) io.close ( file ) local  path = system.pathForFile ( "score.txt", system.DocumentsDirectory)  local tempCurrentScore file = io.open ( path,  "r" ) tempCurrentScore= file:read ("\*a") io.close ( file ) --Checking to see if new HighScore and saving new HighScore if system.pathForFile ( "highScore.txt", system.DocumentsDirectory) then  local  path = system.pathForFile ( "highScore.txt", system.DocumentsDirectory)  local tempHighScore file = io.open ( path,  "r" ) tempHighScore= file:read ("\*a") io.close ( file ) if tempCurrentScore \> tempHighScore then local  path = system.pathForFile ( "highScore.txt", system.DocumentsDirectory) file = io.open ( path,"w")  file:write(tempCurrentScore) io.close ( file ) print("YES IT CAME HERE CHECKINGGGGGGGGGGGGGG") else end else --HighScore dosent exists creating the file and updating highscore   print("YES IT CAME HERE ELSEEEEEEEEEEEE") local path local file path = system.pathForFile ( "highScore.txt", system.DocumentsDirectory) file = io.open (path, "w") file:write(tempCurrentScore) io.close ( file ) print() end  end   scoreCode() composer.gotoScene( "gameOver" ) 

so basically what it does is simple write the current score to score.txt every time and then prints out in the gameOver scene that works fine. After saving the current score and closing the file it opens and reads it again and saves in tempCurrentScore and then checks to see if highscore.txt exists if it does it reads the highscore.txt and saves it in tempHighscore and then checks if tempCurrentScore>tempHighScore if it is it writes the tempCurrentScore to the file and closes it.

it works fine the 1st time but the complier does not comes to this piece of code the 2nd time. HELP!

You need to add code to test the results of all file I/O operations. Whenever you open a file, read a file or write a file, you should check the return results to see if an error has occurred. Each file API returns two items when an error occurs. The first is nil if the operation failed and the second is a string indicating what the error was.

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local errorString local file, errorString = io.open( path, "r" ) if  not file then     print( "File Open error: " .. errorString) else     local savedData, errorString = file:read( "\*a" )     if not savedData then         print( "File Read error: " .. errorString )     end     io.close( file ) end file = nil

@Tom

the problem is not with writing/reading file I guess. I think I am using the composer wrong. I have this scoring Code inside of  scene:create is that correct? and from scene:create I am going to the next scene gameOver when health=0 is that correct? and from gamOver I have a play again button which takes you back to gamePlay and I have that code in scene:show under elseif ( phase == “did” )  is that correct?

@Tom,  

It saves highscore the first time to the file but if you replay the game and make a new highscore then it gives error while writing to the file:

attempt to index local file a nill value

here is the code I am using to write to the file:

 local  path = system.pathForFile ("highScore.txt", system.DocumentsDirectory)  file = io.open (path,"w")   print("highscore.txt opened for writing")  print( tempCurrentScore) file:write(score) io.close ( file )

If i change score with any other number or any variable it gives the same error.

It’s most likely because it can’t find the file. If you don’t want to check for errors and print out the strings, you will be lost trying to find the problem. (Printing out the errorString like I mentioned before will tell you why you are getting the error.)

Composer’s scene:create is only called when the scene is created. If you never destroy the scene, it will only be called the first time. 

You need to add print statements so you understand the flow of your code and when things are getting called or not called. This is basic debugging techniques and how you understand and learn,

Thanks Tom, I found a way around it by simply not just checking to see if the file exists and manually creating the files.

Can you please confirm if the sandbox files are included when you build the apk?

Sandbox files (Documents, Temporary, Cache) are NOT included in the app builds. Only the resource files are included and if you are using Android, you have limited access to them. If you want a file in the sandbox folders, you must create or copy them yourself with file I/O code.

Thanks, problem solved

You need to add code to test the results of all file I/O operations. Whenever you open a file, read a file or write a file, you should check the return results to see if an error has occurred. Each file API returns two items when an error occurs. The first is nil if the operation failed and the second is a string indicating what the error was.

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local errorString local file, errorString = io.open( path, "r" ) if  not file then     print( "File Open error: " .. errorString) else     local savedData, errorString = file:read( "\*a" )     if not savedData then         print( "File Read error: " .. errorString )     end     io.close( file ) end file = nil

@Tom

the problem is not with writing/reading file I guess. I think I am using the composer wrong. I have this scoring Code inside of  scene:create is that correct? and from scene:create I am going to the next scene gameOver when health=0 is that correct? and from gamOver I have a play again button which takes you back to gamePlay and I have that code in scene:show under elseif ( phase == “did” )  is that correct?

@Tom,  

It saves highscore the first time to the file but if you replay the game and make a new highscore then it gives error while writing to the file:

attempt to index local file a nill value

here is the code I am using to write to the file:

 local  path = system.pathForFile ("highScore.txt", system.DocumentsDirectory)  file = io.open (path,"w")   print("highscore.txt opened for writing")  print( tempCurrentScore) file:write(score) io.close ( file )

If i change score with any other number or any variable it gives the same error.

It’s most likely because it can’t find the file. If you don’t want to check for errors and print out the strings, you will be lost trying to find the problem. (Printing out the errorString like I mentioned before will tell you why you are getting the error.)

Composer’s scene:create is only called when the scene is created. If you never destroy the scene, it will only be called the first time. 

You need to add print statements so you understand the flow of your code and when things are getting called or not called. This is basic debugging techniques and how you understand and learn,

Thanks Tom, I found a way around it by simply not just checking to see if the file exists and manually creating the files.

Can you please confirm if the sandbox files are included when you build the apk?

Sandbox files (Documents, Temporary, Cache) are NOT included in the app builds. Only the resource files are included and if you are using Android, you have limited access to them. If you want a file in the sandbox folders, you must create or copy them yourself with file I/O code.

Thanks, problem solved