Hey again everyone,
I’m having a weird problem with file i/o. One of two things will happen, depending on what I do with my code,
here are the functions in question:
function settings:accessData() local path = system.pathForFile( "appData.json",system.DocumentsDirectory ) local file = io.open( path,"r" ) if(file) then contents = file:read("\*a") gameSettings = json.decode( contents ) io.close( file ) return gameSettings else -- leaving this else here if we decide to add any debugging messages. print( "Something Failed" ) end --io.close( file ) end
function settings:checkLives() gameSettings = settings:accessData() --print( "Checking the JSON file " .. gameSettings.twentyFourHourTimer ) local OSTime = os.time( ) local nextDayTime = (gameSettings.twentyFourHourTimer) + 120 --86400 = 24 hours replace when done testing timeLeft = print( "time difference is " .. (OSTime - nextDayTime) ) if(OSTime \< nextDayTime ) then print( "24 hours has not passed" ) lives = gameSettings.currentLives return lives else --print( "24 hours have passed" ) lives = gameSettings.maxLives gameSettings.twentyFourHourTimer = os.time( ) local encode = json.encode( gameSettings ) --print( os.time( ) ) File:save(encode, "appData") end end
With that code, i will get this error
Runtime error h:\sticky-fingers-adventure\trunk\code base\settings.lua:86: attempt to perform arithmetic on field 'twentyFourHourTimer' (a nil value) stack traceback: h:\sticky-fingers-adventure\trunk\code base\settings.lua:86: in function 'checkLives' h:\sticky-fingers-adventure\trunk\code base\menu-scene.lua:63: in function \<h:\sticky-fingers-adventure\trunk\code base\menu-scene.lua:61\> ?: in function 'dispatchEvent' ?: in function 'gotoScene' h:\sticky-fingers-adventure\trunk\code base\main.lua:43: in main chunk
if I eliminate the io.close() from settings:accessData(), than I don’t get that error, but I’m not able to save when the timer has expired, because the engine still has the file open… Any idea what I can do?