Hi Guys. In order to organize my functions, I created a utlility.lua file and I put the following code:
local M = {} local json = require("json") function M.saveTable(t, filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) return true else return false end end function M.loadTable(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end
And, In my main code, I put:
-- Load Configurations local utility = require("utility") local mySettings = utility.loadTable("settings.json") \<\<-- Error HERE if mySettings == nil then mySettings = {} mySettings.soundOn = true mySettings.musicOn = true mySettings.score = 0 utility.saveTable(mySettings, "settings.json") end local mySettings = utility.loadTable("settings.json")
But I got this error at the first line:
<
attempt to index local ‘utility’(a boolean value)
>
I dont understand this error because since I didn’t previusly declared mySettings, it should be converted to boolean false or to a table regarding the result isn’t it?