I am creating a system to save the state of my game. It works in the Corona simulator but if I build it for an IOS device or the XCode simulator (or possibly Android; I can’t build for Android right now.), the io.open function fails. This happens when reading and writing to the file. The file is in the documents directory in the project sandbox and the system.pathForFile() function returns true but when I use the path with io.open(), it returns false. Again, the code works on the Corona simulator so is there something else that I need to do to make it work on devices?
Here is the code if it helps:
if roomsComplete ~= 0 then --do not save score of zero local path = system.pathForFile("highScoreData.txt", system.DocumentsDirectory) local file = io.open(path, "r") --open in read mode to get previous score local scores = {} for line in file:lines() do --insert all previous scores into table for sorting scores[#scores + 1] = line end scores[#scores + 1] = roomsComplete .. " " .. composer.getVariable("difficulty") --add new score to table along with difficulty file:close() table.sort(scores, function(x, y) --sort new score into correct pos in table return tonumber(x:match("%d+")) \> tonumber(y:match("%d+")) end) file = io.open(path, "w") --open in write mode to rewrite scores for i = 1, #scores do --loop through score file:write(scores[i] .. "\n") --write score end file:close() end composer.gotoScene("lose", "fade", 500) end
Thank you in advance for any help you can give.