I don’t mind helping. I’m no expert by any stretch, but helping others helps everyone in the long run, so I’m happy to lend a hand.
As for you getting another nil error when using json strings, I’m going to assume it’s still not implemented correctly. Here’s basically what I have for json game data:
--main.lua local json = require("json") local pathOne = system.pathForFile( "SavedGameOneSettings.json", system.DocumentsDirectory ) local oneFile = io.open( pathOne ) local pathGame = system.pathForFile( "GameSettings.json", system.DocumentsDirectory ) local gameFile = io.open( pathGame ) function saveTableOne(t, filename) local path = system.pathForFile( "SavedGameOneSettings.json", system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) print("File Encoded!") return true else return false end end function saveTableGame(t, filename) local path = system.pathForFile( "GameSettings.json", system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) print("File Encoded!") return true else return false end end function loadTableGame(filename) local path = system.pathForFile( "GameSettings.json", system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end function loadTableOne(filename) local path = system.pathForFile( "SavedGameOneSettings.json", system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end --[[Create your GameSettings and SavedGameOneSettings variables here]] if oneFile then print( "File exists" ) oneFile.close(oneFile) else saveTableOne(SavedGameOneSettings, "SavedGameOneSettings.json") print( "SavedGameOneSettings didn't exist, but now IT DOES!" ) end if gameFile then print( "File exists" ) gameFile.close(gameFile) else saveTableGame(GameSettings, "GameSettings.json") print( "GameSettings didn't exist, but now IT DOES!" ) end
free to copy it at will. I think I just adapted what Rob described in his tutorial. Put this in your main.lua and it will tell you pretty clearly in your terminal if the table exists or not. If you wanted to do something like testvalue[1] and adapt it to this snippet, you could do something like this:
--[[Create your GameSettings and SavedGameOneSettings variables here]] SavedGameOneSettings.testvalue= { 0,0,0,0,0}
The above creates tables somewhat similar to what you were doing. The first code snippet will create the second code snippet and will check to see if it’s created. Now, you can have a function that does this:
local function checkDataTest1() if SavedGameOneSettings.testvalue[1] == 0 then print("JSON STRING SAVE AND LOAD SUCCESS!") end end checkDataTest1()
Try that out and let me know how it works; you should be able to convert the important parts to your needs.