Hi Tristan
It took me many hours to figure out how to load and save data using json but I cracked it thanks to some good advice from Rob Miracle.
What I did first was to create a text file in notepad on my computer (players.txt) with all the basic data to be written to later, basically just a list of generic scores and names, and saved that text file into the same folder where all my lua modules and images are.
The text file looks like this…
10000
9000
8000
… --and so on
Mr Jones
Mrs Jones
Mr Smith
… --and so on
10
9
8
… --and so on
Then read that text file and put the contents into a table ( which I called playerData{} ) like so…
local \_path = system.pathForFile( "players.txt", system.ResourceDirectory ) local \_file = io.open( \_path, "r" ) local counter = 1 for line in \_file:lines() do if counter \< 11 then playerData.HighScores[counter] = tonumber( line ) end if counter == 11 then playerData.storedPlayers[1] = tostring( line ) end if counter \> 11 and counter \< 16 then playerData.currentPlayers[counter-11] = tostring( line ) end if counter \> 15 and counter \< 26 then playerData.HighScoreLevels[counter-15] = tonumber( line ) end if counter \> 25 then playerData.HighScoreNames[counter-25] = tostring( line ) end counter = counter + 1 end playerData.storedPlayersCount = 1 playerData.NumberOfHighScores = 10 io.close( \_file ) \_file = nil \_path = nil counter = nil line = nil
As you can see at the bottom of my code I also added more data to my playerData table.
Then I saved my table to a json file in the Documents Directory (Sandbox)
loadsave.saveTable(playerData, "players.json", system.DocumentsDirectory )
I have not ever needed to decode the contents by the way ( myTable=json.decode(contents) )
I just load them into a table using Rob Miracle’s “loadsave” module.
local loadsave = require("loadsave") gameData = {} gameData = loadsave.loadTable("gamedata.json", system.DocumentsDirectory )
The thing to note here is that your sandbox (Documents directory) should be empty when you build for device.
I delete the created files in the sandbox folder before I build because they will be created when your app is on the actual device.
Even though you can’t see them they will be there to read from and write to just like in the simulator.
I hope this helps.