Json file being read in simulator, but not on device.

Okay so I have a “gamedata.json” that I have intialized and put in Documents folder in corona sandbox of my app.

In the simulator everything works fine and data is being loaded and saved to the file. But when I run the app on the actual device, it seems that the app cant find the “gamedata.json” file.

Any help on how I could locate the file or if it doesn’t exist then how could I create when when the app is first launched? Thanks!

are you using loadsave module ??

would you please post your code?

Ah sorry for the late reply.

Yes I am using the loadsave module.

I have a gamedata.json file placed in Documents directory. The file contents: 

{"unlockedlevels":1,"maxlevels":50}

Then in menu.lua scene I do:

local mydata={} mydata = loadsave.loadTable("gamedata.json")

Though this is working fine in the simulator when I run the app on the actual device, it seems that the app cant find the “gamedata.json” file, and hence doesnt show how many levels have been unlocked. Thanks

json file is not created when you launch your app for the first time

it will be created by following code:

mydata = loadsave.loadTable(“gamedata.json”) – if mydata exists, it won’t create mydata

if mydata == nil then – this checks wether mydata exists or not

 mydata = {}

 mydata.unlockedlevels = 1 

 mydata.maxLevels = 50

 loadsave.saveTable(mydata,“gamedata.json”)

end 

this code will load mydata file and if doesn’t exists, it will create it 

Hope that works !

I am afraid that didnt work.

For the code you pasted above, shouldn’t a “gamedata.json” already exist, or else in what file will the table saved?

Here’s my current code for menu.lua . The code you gave above is in scene:show()

local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local myData = require( "mydata" ) local mydata={} local json=require("json") local loadsave = require("loadsave") mydata = loadsave.loadTable("gamedata.json") local function handleCancelButtonEvent( event ) if ( "ended" == event.phase ) then composer.gotoScene( "mainscreen", { effect="crossFade", time=333 } ) end end local function handleLevelSelect( event ) if ( "ended" == event.phase ) then myData.settings.currentLevel = event.target.id composer.removeScene("level1", false ) composer.removeScene("level2", false ) composer.removeScene("level3", false ) composer.removeScene("level4", false ) composer.gotoScene(event.target.id, { effect="crossFade", time=333 } ) end end function scene:create( event ) local sceneGroup = self.view --images and buttons code goes here end function scene:show( event ) local sceneGroup = self.view mydata = loadsave.loadTable("gamedata.json") if mydata == nil then mydata={} mydata.unlockedlevels=1 mydata.maxlevels=50 loadsave.saveTable(mydata, "gamedata.json") end if ( event.phase == "did" ) then end end function scene:hide( event ) local sceneGroup = self.view if ( event.phase == "will" ) then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Any further help would be appreciated.

sorry i didnt mentioned that before, you should paste that code(the code which checks whether gamedata exists or not) in main.lua

NOTE: remember to paste that code before composer.gotoScene() function

remove mydata = {} from line 7

That worked like a charm sir!

Thank you!

Im having a similar issue, but I think I am missing a step that mostwanted included.

I am also using the loadsave module except I’m afraid I don’t recall reading anything about… mostwanted mentioned that 

“Okay so I have a “gamedata.json” that I have intialized and put in Documents folder in corona sandbox of my app.”

 

could someone explain this to me?

 

did you just create a folder called Documents inside your main project folder and then created another file called gamedata.json?

are you using loadsave module ??

would you please post your code?

Ah sorry for the late reply.

Yes I am using the loadsave module.

I have a gamedata.json file placed in Documents directory. The file contents: 

{"unlockedlevels":1,"maxlevels":50}

Then in menu.lua scene I do:

local mydata={} mydata = loadsave.loadTable("gamedata.json")

Though this is working fine in the simulator when I run the app on the actual device, it seems that the app cant find the “gamedata.json” file, and hence doesnt show how many levels have been unlocked. Thanks

json file is not created when you launch your app for the first time

it will be created by following code:

mydata = loadsave.loadTable(“gamedata.json”) – if mydata exists, it won’t create mydata

if mydata == nil then – this checks wether mydata exists or not

 mydata = {}

 mydata.unlockedlevels = 1 

 mydata.maxLevels = 50

 loadsave.saveTable(mydata,“gamedata.json”)

end 

this code will load mydata file and if doesn’t exists, it will create it 

Hope that works !

I am afraid that didnt work.

For the code you pasted above, shouldn’t a “gamedata.json” already exist, or else in what file will the table saved?

Here’s my current code for menu.lua . The code you gave above is in scene:show()

local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local myData = require( "mydata" ) local mydata={} local json=require("json") local loadsave = require("loadsave") mydata = loadsave.loadTable("gamedata.json") local function handleCancelButtonEvent( event ) if ( "ended" == event.phase ) then composer.gotoScene( "mainscreen", { effect="crossFade", time=333 } ) end end local function handleLevelSelect( event ) if ( "ended" == event.phase ) then myData.settings.currentLevel = event.target.id composer.removeScene("level1", false ) composer.removeScene("level2", false ) composer.removeScene("level3", false ) composer.removeScene("level4", false ) composer.gotoScene(event.target.id, { effect="crossFade", time=333 } ) end end function scene:create( event ) local sceneGroup = self.view --images and buttons code goes here end function scene:show( event ) local sceneGroup = self.view mydata = loadsave.loadTable("gamedata.json") if mydata == nil then mydata={} mydata.unlockedlevels=1 mydata.maxlevels=50 loadsave.saveTable(mydata, "gamedata.json") end if ( event.phase == "did" ) then end end function scene:hide( event ) local sceneGroup = self.view if ( event.phase == "will" ) then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Any further help would be appreciated.

sorry i didnt mentioned that before, you should paste that code(the code which checks whether gamedata exists or not) in main.lua

NOTE: remember to paste that code before composer.gotoScene() function

remove mydata = {} from line 7

That worked like a charm sir!

Thank you!

Im having a similar issue, but I think I am missing a step that mostwanted included.

I am also using the loadsave module except I’m afraid I don’t recall reading anything about… mostwanted mentioned that 

“Okay so I have a “gamedata.json” that I have intialized and put in Documents folder in corona sandbox of my app.”

 

could someone explain this to me?

 

did you just create a folder called Documents inside your main project folder and then created another file called gamedata.json?