File does not writing

I’m working on a game, and I want to save the highscore in a text file, so I made a function to set the highscore in that textfile:

[lua]function setHighScore( val )
local file = io.open( filePath, “w” )
file:write( tostring(val) )
io.close( file )
file = nil
end[/lua]

When I call it, I get this error: “attempt to index local file (a nil value)…”

and that’s not all. When I call it from the scene:create, it works just fine, but when I call it from another function, it throws that error. What can I do?

What is filePath? If it works in some places and not others, I’d guess it’s a variable-scope issue. Where is filePath defined? is it a global or is it local to a file or function?

filePath is a local variable that is declared in the top of the code, outside of all the methods. This is the line that’s declaring it:

[lua]local filePath = system.pathForFile( “hs.txt”, system.DocumentsDirectory )[/lua]

But is filePath visible to your setHighScore() function?  It might be scoped to be local to a block that setHighScore cant’ see.

No, it’s at the TOP of the code, above all functions.

I had figured something out. This function works only if I do not call my getHighScore() function, described below. If I had called it anytime, the setHighScore() function won’t work.

[lua]–Returns the current highscore
function getHighScore()
    local readFile = io.open(filePath, “r”)
    if readFile then
        return tonumber( readFile:read("*a") )
    else
        return 0
    end
    io.close( readFile )
end
[/lua]

also, this issue is happening only on the simulator (not on my Android device).

It’s not working because you are exiting the function early (line 5 and 7). Line 9 is never run to close the file - so the file is still open when you go to write the file later.

I’d rewrite your getHighScore function like so:

function getHighScore() local score = 0 local readFile = io.open(filePath, "r") if readFile then score = tonumber( readFile:read("\*a") ) end io.close( readFile ) return score end

Yeah, I kind of figured it out a while ago… thanks anyway!

glad to hear!

What is filePath? If it works in some places and not others, I’d guess it’s a variable-scope issue. Where is filePath defined? is it a global or is it local to a file or function?

filePath is a local variable that is declared in the top of the code, outside of all the methods. This is the line that’s declaring it:

[lua]local filePath = system.pathForFile( “hs.txt”, system.DocumentsDirectory )[/lua]

But is filePath visible to your setHighScore() function?  It might be scoped to be local to a block that setHighScore cant’ see.

No, it’s at the TOP of the code, above all functions.

I had figured something out. This function works only if I do not call my getHighScore() function, described below. If I had called it anytime, the setHighScore() function won’t work.

[lua]–Returns the current highscore
function getHighScore()
    local readFile = io.open(filePath, “r”)
    if readFile then
        return tonumber( readFile:read("*a") )
    else
        return 0
    end
    io.close( readFile )
end
[/lua]

also, this issue is happening only on the simulator (not on my Android device).

It’s not working because you are exiting the function early (line 5 and 7). Line 9 is never run to close the file - so the file is still open when you go to write the file later.

I’d rewrite your getHighScore function like so:

function getHighScore() local score = 0 local readFile = io.open(filePath, "r") if readFile then score = tonumber( readFile:read("\*a") ) end io.close( readFile ) return score end

Yeah, I kind of figured it out a while ago… thanks anyway!

glad to hear!