Highscore reset after update

Hi, I am storing my highscore in a .txt file, it works well but when I update the app the highscore returns to the value that was stored in the .txt file inside my computer when I created the .apk

There is a way to fix it? I am using this for storing the highscore:

READING:

local path = system.pathForFile("highscore.txt") local file = io.open(path, "r") local content = file:read("\*a") highscore = tonumber(content) io.close(file) file = nil

WRITING:

local path = system.pathForFile("highscore.txt") local file = io.open(path, "w") file:write(highscore) io.close(file) file = nil

I would guess your highscore.txt file is in your app’s resources, and therefore you overwrite it when you install a new version.

Instead, don’t include the highscore.txt file in the resources at all.  as for its path use system’s Documents directory, and at app startup first do a check whether the file exists…

if it does, call the load function, and read the score from it.

if the file doesn’t exist, call the save function and create it in the process…

so your first line of both read and write functions should be more like this:

local path = system.pathForFile( "highscore.txt", system.DocumentsDirectory )

the rest should be fine.

Your app will then retain the data even in upgrades… only when you uninstall the app (or clear app data through your device’s settings) you will lose the file.

Additionally, i would suggest using json files for storage of data, if you ever need to add anything or change the structure, it is much easier… all you have to do is put your data into an array, and before saving it use json.encode function before writing to file, and json.decode when reading back from the file.

system.pathForFile() defaults to system.ResourceDirectory which is read-only. As @joedivinci said, you need to be saving and reading from system.DocumentsDirectory.

Rob

Thank you both! It worked!

I wrote this:

READING:

local path = system.pathForFile("highscores.txt", system.DocumentsDirectory) local file = io.open(path, "r") if ( file ) then local content = file:read("\*a") highscore = tonumber(content) io.close(file) file = nil else local path = system.pathForFile("highscores.txt", system.DocumentsDirectory) file = io.open(path, "w") file:write(0) io.close(file) highscore = 0 file = nil end

WRITING:

local path = system.pathForFile("highscores.txt", system.DocumentsDirectory) local file = io.open(path, "w") file:write(highscore) io.close(file) file = nil

I would guess your highscore.txt file is in your app’s resources, and therefore you overwrite it when you install a new version.

Instead, don’t include the highscore.txt file in the resources at all.  as for its path use system’s Documents directory, and at app startup first do a check whether the file exists…

if it does, call the load function, and read the score from it.

if the file doesn’t exist, call the save function and create it in the process…

so your first line of both read and write functions should be more like this:

local path = system.pathForFile( "highscore.txt", system.DocumentsDirectory )

the rest should be fine.

Your app will then retain the data even in upgrades… only when you uninstall the app (or clear app data through your device’s settings) you will lose the file.

Additionally, i would suggest using json files for storage of data, if you ever need to add anything or change the structure, it is much easier… all you have to do is put your data into an array, and before saving it use json.encode function before writing to file, and json.decode when reading back from the file.

system.pathForFile() defaults to system.ResourceDirectory which is read-only. As @joedivinci said, you need to be saving and reading from system.DocumentsDirectory.

Rob

Thank you both! It worked!

I wrote this:

READING:

local path = system.pathForFile("highscores.txt", system.DocumentsDirectory) local file = io.open(path, "r") if ( file ) then local content = file:read("\*a") highscore = tonumber(content) io.close(file) file = nil else local path = system.pathForFile("highscores.txt", system.DocumentsDirectory) file = io.open(path, "w") file:write(0) io.close(file) highscore = 0 file = nil end

WRITING:

local path = system.pathForFile("highscores.txt", system.DocumentsDirectory) local file = io.open(path, "w") file:write(highscore) io.close(file) file = nil