hi, im trying to learn the corona sdk, I am following a tutorial on tutsplus on creating a poker game with the corona sdk and lua, it uses a third party module to save data by artur sosin
anyhow, I am having trouble integrating the third party library to the app itself, something to do with variable scope but cannot figure out how to fix it
the error i get from the simulator is this
dataSaver.lua:33: attempt to index local ‘file’ ( a nil value ) in function ‘saveValue’
here’s the function, i made a note where line 33 starts, if anyone can give me some guidance I would be grateful
[lua]
function saveValue(key, value)
–temp variable
local app
–default data storage
local filename = “app.data”
–get directory
local base = system.DocumentsDirectory
– create a file path for corona i/o
local path = system.pathForFile( filename, base )
–open file
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
–decode json
app = json.decode(contents)
io.close( file ) – close the file after using it
–if file was empty
if(not app.data) then
app.data = {}
end
–store value in table
app.data[key] = value
–encode table to json
contents = json.encode(app)
–open file
local file = io.open( path, “w” )
–store json string in file ************ Line 33 starts right below **********
file:write( contents )
–close file
io.close( file )
else
–if file doesn’t exist
–create default structure
app = {data = {}}
–store value
app.data[key] = value
–encode in json
local contents = json.encode(app)
–create file
local file = io.open( path, “w” )
–save json string in file
file:write( contents )
–close file
io.close( file )
end
end
[/lua]