problem testing app in Android using JSON files

Hello. This is my first post and I’m a hobby programmer so aplogies if this is a daft question or I’m not following post protocol. I’m using JSON to load game data, and have set it up using documentation. It works fine on the simulator. Testing on an Android device appears not to have built using the .json files in the sandbox. I’ve read that Android devices has problems seeing system.DocumentsDirectory. However, I’m still stuck on how to resolve it. Small brain. :grinning: Any help would be greatly appreciated

I may be misunderstanding your question, but the .json files in the sandbox won’t be included in the ,apk file of your build. You’ll need to have the app write them to the documents directory as part of a first run sequence.

Thank you for the reply colinmorgan. Sorry. How do I make the app write them to the documents directory as part of a first run sequence? Is this code I need to add to the game just for the test-build, or physically moving .json files from the sandbox into the folder of the game, or something else? Many thanks in advance.

Hello Guy5,

Welcome to the Solar2D community!

There are a number of ways to achieve what you want.

One quick way is to put your “json” file in “system.ResourceDirectory”, in other words, your working source code folder. In your execution of your code, (1) check if the same “json” file exists in “system.DocumentsDirectory”. (2) If yes, okay, good to go. (3) If not, copy “json” file from “ResourceDirectory” to “DocumentsDirectory”.

You can see one example here, https://docs.coronalabs.com/guide/data/readWriteFiles/index.html#copying-files-to-subfolders

I really like Rob Miracle’s loadsave module- it really takes the grunt work out of dealing with .json files.

So, to detect if it’s first run, I just load try to load the .json file into a table, check if the table exists and, if it doesn’t, create it and save it. (I find looking at the table in the Lua format easier than in the .json format, which is why I don’t copy a .json file as in luantiang’s example. It’s all down to how you like to work.)

local json = require( "json" )
local loadsave = require( "loadsave" )  -- Require the Rob's 'loadsave' module
local gameData = loadsave.loadTable( "gameData.json" )
if (gameData == nil ) then -- First run
  gameData = {}
	gameData.highScore = 0
	gameData.levelsCompleted = 0
	-- Etc.
  loadsave.saveTable(gameData, "gameData.json")
end

You can also use my Loadsave plugin:

Whenever you are working with persistent data, you essentially want to check if a save file already exists, which you can do by trying to load it. If a save file does exists, you load it and continue on. If a save file doesn’t exist, then you need to create it.

My plugin handles saving strings or Lua tables to files, as well as save file backups and anti-tampering. I recently added an option to also save human-readable files that can be used during development.

Thank you for the suggestions luantiang. I’ve give these a try. I’ll get my head around it in the end.

Thank you again colinmorgan for the suggestions. I’ll give it a try!

Thank you for the suggestion XeduR. I thought I was going to be stumped and had a moment of doom feeling. I’ve spent ages working in the simulator (far too long to admit) and never got around to actually trying to get it on a device. All these suggestions should sort me out.

Hello. Just as an update, I’ve got this to work. Very exciting. Used luantiang’s methods/links to copy the files rather than create them on first run, as I forgot to clarify that they have all the data to make the world (tiles, objects etc…) rather than being blank and used for data once the game has started (high scores etc…). But those other methods look useful for the future. Thank you everyone.

1 Like