Editing permanently a .json file with Corona (Load save .json)

Hi ! I’m currently working on a game using Corona Sdk and creating levels using Tiled thanks to the Ponytiled customs (https://github.com/ponywolf/ponytiled) .

Game has 8 levels and i would like to know which could be the eaasiest way to unlock a passed\reached level button in the main menu of the game and save it permantly?

I’ve tried this one (https://coronalabs.com/blog/2014/08/12/tutorial-building-a-level-selection-scene/) " Building a Level Selection Scene" which is pretty good, but actually not working very well for my game.

But since i’m already using a .json file to load my menu ( similar to title.json in ponytiled), is there any way i could edit a line of that file with a lua function, chnging an object.isVisible from false to true?
This edit should be permanent, ich means that when i quit the game it gets saved.

i’ll bring some lines from the Ponytiled ‘‘title.json’’ hopefully trying to be more clear:

	    **-- Load our UI**
**    	local uiData = json.decodeFile( system.pathForFile( "scene/menu/ui/title.json", system.ResourceDirectory ) )**
**    	ui = tiled.new( uiData, "scene/menu/ui" )**
**    	ui.x, ui.y = display.contentCenterX - ui.designedWidth/2, display.contentCenterY - ui.designedHeight/2**

This lines work pretty good because with just:

**ui:findLayer( " LEVEL 2 BUTTON" ).isVisible = true**

i can now unlock the LEVEL 2 BUTTON , but obviosly the line “visible”:false,
in the title.json doesn’t get edited.

so is there a way to edit the line “visible”:false, in title.json to “visible”:true,?
Maybe using the

    **`local` `file = ` `io.open` `(uiData , ` `"w"` `)`**

    ** **

    **`    ` `if` `file ` `then`**

    **`        ` `file:write( json.encode( "scene/menu/ui/title.json"  ) )`**

    **`        ` `io.close( file )`**

or something similar that i’ve missed?

i know it seems pretty strange, but it could be a really good way to unlock levels.

Thank you!

I have created this module to help me loading and saving the game data: loadSave.lua (2.9 KB)

You will need the getValue and setValue functions to make changes, and invoke save() to make them persist in memory

One important note is that you are loading your ui data from the ResourceDirectory and you can only save data on DocumentsDirectory

So once you save your first changes, you should look for them on the DocumentsDirectory too.

I actually tried something similar but the way i made it with the setVariable and getVariable wasn’t working very well. i had to change scene everytime to save the variable in the .json file ( it was an highscores based unlock level).
Thanks, seems that your module is the easiest way

Yes. I would agree, that saving the whole UI JSON to keep a variable or two is probably overkill. @depilz module should work better. Think about saving all your unlocks as a table that way you can loop through them and if you name your buttons properly it will be easy to exapnd.

unlocks = {}
unlocks["levelOneButton"] = true
unlocks["levelTwoButton"] = true
saveToFile(unlocks) -- replace with your function to savea table

Now you can do something like this

loadFromFile(unlocks) -- replace with your function to load a table
for k,v in pairs(unlocks) do
  ui:findObject(k).isVisible = v
end

You can also find a tutorial for saving and loading data in JSON format in here: https://docs.coronalabs.com/tutorial/data/jsonSaveLoad/index.html.

Ok i saved all as a table as you said @ponywolf and it works pretty well. Thank you!

1 Like