Little issue with saves...

So I’m working on saves for my app right now, the problem is, when I test it, the simulator crashes… It stops responding and crashes.

My code is as follows:

[lua]

local saveData = “saveScore”

local path = system.pathForFile( “ScoreSave.txt”, system.DocumentsDirectory )

    local file = io.open( path, “w” )

    file:write( saveData )

    io.close( file )

    file = nil

–opening the file

local path = system.pathForFile( “ScoreSave.txt”, system.DocumentsDirectory )

    local file = io.open( path, “r” )

    local saveData = file:read( “*a” )

    

    io.close( file )

    file = nil

local function onSystemEvent( event )

    if ( event.type == “applicationSuspend” ) then

        saveScore()

        print(“saveScore”)

    end

end

Runtime:addEventListener( “system”, onSystemEvent )

[/lua]

Is that all of your code?

I don’t see a function named saveScore being defined anywhere.  What the code above does is open a file for write, writes the string “saveScore” to it, closes it, immediately re-opens the file and reads the data back in and overwrites the variable saveData but the end effect is that you read back in and overwrite the variable with what it already had.

The reason you are getting an error is there is no function named saveScore for the event listener to add.   It’s really helpful to learn how to read your console log messages with helping solve these problems.  Please make sure your read:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

OK, so I fixed that. I basically just encased all the file writing code into a quick’n dirty function and now it seems to be working. Or, at least, it’s printing “saveScore” as it should be. Is there any way to test if it actually saved or not?

Error checking.  If it could not open the file for write, you will get a nil file pointer.  The write function may return some value you can test… and doing what you’re doing.

However after 30+ years of doing this, if I can get writable file handle and I didn’t crash in the middle, and my close ran, I’m good to go.  Our file systems are that stable these days.

Plus in the simulator if you go to the Sandbox you can see the file itself.  The sandbox contains the directories where you save stuff to. 

Is that all of your code?

I don’t see a function named saveScore being defined anywhere.  What the code above does is open a file for write, writes the string “saveScore” to it, closes it, immediately re-opens the file and reads the data back in and overwrites the variable saveData but the end effect is that you read back in and overwrite the variable with what it already had.

The reason you are getting an error is there is no function named saveScore for the event listener to add.   It’s really helpful to learn how to read your console log messages with helping solve these problems.  Please make sure your read:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

OK, so I fixed that. I basically just encased all the file writing code into a quick’n dirty function and now it seems to be working. Or, at least, it’s printing “saveScore” as it should be. Is there any way to test if it actually saved or not?

Error checking.  If it could not open the file for write, you will get a nil file pointer.  The write function may return some value you can test… and doing what you’re doing.

However after 30+ years of doing this, if I can get writable file handle and I didn’t crash in the middle, and my close ran, I’m good to go.  Our file systems are that stable these days.

Plus in the simulator if you go to the Sandbox you can see the file itself.  The sandbox contains the directories where you save stuff to.