this isn’t working. it stopped working and I think it was something to do with the system.DocumentsDirectory
Check to see if the file has actualy been opened. If it doesn’t exist you will get a result of nil.
highScoreFile = io.open(…
if not highScoreFile then
print(“High score file not found!”)
end
I suggest adding a file extension to your files like .txt or .cfg or .dat or something that makes sense.
Is there anything in that file?
Instead of trying to do tonumber on it directly, set it to a variable and see if you get an error. If you don’t, see if you can print the variable and if it has content or not.
My guess is that the file read is not returning anything.
If you try to open a file with read mode (io.open(…, “r”)), it will only work if the file already exists. I’d recommend “r+” or “w” mode if you want to make sure the file exists.
Two handy functions:
[lua]
local getFileContents = function(filename, base)
local base = base or system.DocumentsDirectory
local path = system.pathForFile(filename, base)
local contents
local file = io_open(path, “r”)
if file then
contents = file:read("*a")
io_close(file)
file = nil
end
return contents
end
local writeFile = function(filename, contents, base)
local base = base or system.DocumentsDirectory
local path = system.pathForFile(filename, base)
local file = io_open(path, “w”)
if file then
file:write(contents)
io_close(file)
file = nil
end
end
[/lua]
Using readFile() and writeFile(), we can create a make-if-not-existent-otherwise-retain-original-contents function like so:
[lua]
local assertFileExists = function(filename, defaultContents, base)
writeFile(filename, getFileContents(filename, base) or (defaultContents ~= nil and defaultContents) or “”, base)
end
[/lua]
This function writes into the file the contents that it already has. If the contents are nil (viz. the file doesn’t exist), it checks for if the user provided default file contents. If that’s nil, it uses an empty string.
I’d put those functions into a separate file operations library, just for cleanliness.
So the way you’d use it in your game/app is like so:
[lua]
– main.lua (we just need to initialize the file)
local filelib = require(“filelib”)
filelib.assertFileExists(“highscore”, 0) – Default the contents to 0
[/lua]
Then, in your actual game file:
[lua]
local highscore = tonumber(filelib.readFile(“highscore”))
[/lua]
We can do that because we know “highscore” will exist.
Hope this helps
- C
Check to see if the file has actualy been opened. If it doesn’t exist you will get a result of nil.
highScoreFile = io.open(…
if not highScoreFile then
print(“High score file not found!”)
end
I suggest adding a file extension to your files like .txt or .cfg or .dat or something that makes sense.
Is there anything in that file?
Instead of trying to do tonumber on it directly, set it to a variable and see if you get an error. If you don’t, see if you can print the variable and if it has content or not.
My guess is that the file read is not returning anything.
If you try to open a file with read mode (io.open(…, “r”)), it will only work if the file already exists. I’d recommend “r+” or “w” mode if you want to make sure the file exists.
Two handy functions:
[lua]
local getFileContents = function(filename, base)
local base = base or system.DocumentsDirectory
local path = system.pathForFile(filename, base)
local contents
local file = io_open(path, “r”)
if file then
contents = file:read("*a")
io_close(file)
file = nil
end
return contents
end
local writeFile = function(filename, contents, base)
local base = base or system.DocumentsDirectory
local path = system.pathForFile(filename, base)
local file = io_open(path, “w”)
if file then
file:write(contents)
io_close(file)
file = nil
end
end
[/lua]
Using readFile() and writeFile(), we can create a make-if-not-existent-otherwise-retain-original-contents function like so:
[lua]
local assertFileExists = function(filename, defaultContents, base)
writeFile(filename, getFileContents(filename, base) or (defaultContents ~= nil and defaultContents) or “”, base)
end
[/lua]
This function writes into the file the contents that it already has. If the contents are nil (viz. the file doesn’t exist), it checks for if the user provided default file contents. If that’s nil, it uses an empty string.
I’d put those functions into a separate file operations library, just for cleanliness.
So the way you’d use it in your game/app is like so:
[lua]
– main.lua (we just need to initialize the file)
local filelib = require(“filelib”)
filelib.assertFileExists(“highscore”, 0) – Default the contents to 0
[/lua]
Then, in your actual game file:
[lua]
local highscore = tonumber(filelib.readFile(“highscore”))
[/lua]
We can do that because we know “highscore” will exist.
Hope this helps
- C