Save/load my game?

I’m currently making a choose your own adventure game using composer, but I cant figure out how to use json to save. I’ve looked at almost all the tutorials but its not making sense to me, could someone try to explain how to save/load a game while using composer? Thanks for any help I get! 

So basically what I want to do is save the scene I was at, for example menu>play>play2>play3>play4 save then have a load button which will take me to play4. the only problem is I have a lot of branch offs, so I have to have a save button how would I make that work? 

What are “branch offs”? Are those Corona Composer related?

I guess I worded that wrong what I mean is. my game is a choose your own adventure so it like this.

main>menu > play >exploring >exploringscream > exploringstalk

                                                > exploringwait

                               >waiting > waitexploring 

                                             > waitcall

                               > calling >

I have bunch more but I think you can see where I’m going with it. so what I’m trying to do Is have a save button and a load button on the menu when I push save it will save the scene you are in, then you can just push the load button from the main menu. the thing is that I also need it to be able to go back to the saved scene from a cold start. how would I achieve that?

Sorry, I’m not exactly sure what the problem is, so let me ask some more questions to approximate the problem.

Is this saving/loading problem because of using the Corona Composer? Or would it be the same problem if you are not using Corona Composer? If the latter then I can give you some hints on how to make save games. If it is a special case related to Corona Composer… I have no experience with Corona Composer.

Honestly my problem is that I have no idea where to start when it comes to saving/loading a game, and none of the tutorials that I’ve looked at are making any sense.  

Ok, I will try to give some hints. Basically you need to write down in a file the state of your objects/levels. In example you need a variable where you store the current level number in, lets call it levelno. If your player is in level number four your variable levelno should store 4.

Nowadays JSON is a nice way to store such values. A LUA JSON module would create a string like {“levelno”:4}. This is JSON syntax.

Now think about which states you need to save. This could be player position, player hitpoints, player inventory etc. So in the end, when you press Save your save file in JSON Syntax may look like this:

{"levelno":4, "playerName":"Jim", "playerPositionX":150, "playerPositionY":275, "playerHitpoints":23}

When you press the Load button, the JSON module should be able to read the above save file and return a Lua table with these values. I have no exact example, but I think you returned table can be read like this

local levelnumber = JSONResult["levelno"] local player.name = JSONResult["playerName"] local player.x = JSONResult["playerPositionX"] and so on...

Here are the steps you need to do for SAVING:

  1. You need Coronas JSON module

    local json = require “json”

  2. Create a table of all values you want to save to file. Coronas JSON module needs a table to create a JSON string of your data.

    local myTable = {   [“levelno”] = levelno,   [“playerName”] = playerName,   [“playerPositionX”] = playerPositionX,   [“playerPositionY”] = playerPositionY,   etc. }

  3. Convert the myTable to a JSON String

    local jsonSaveData = json.encode( myTable )

  4. Save the encoded string to a file

    local path = system.pathForFile( “mySaveFile.txt”, system.DocumentsDirectory ) local file = io.open( path, “w” ) file:write( jsonSaveData ) io.close( file ) file = nil

I think for LOADING you get the idea now (use json.decode() )

I hope this helps understanding this topic.

Here are some useful links about JSON and reading/writing files:

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

http://docs.coronalabs.com/daily/api/library/json/encode.html

http://docs.coronalabs.com/daily/api/library/json/decode.html

So basically what I want to do is save the scene I was at, for example menu>play>play2>play3>play4 save then have a load button which will take me to play4. the only problem is I have a lot of branch offs, so I have to have a save button how would I make that work? 

What are “branch offs”? Are those Corona Composer related?

I guess I worded that wrong what I mean is. my game is a choose your own adventure so it like this.

main>menu > play >exploring >exploringscream > exploringstalk

                                                > exploringwait

                               >waiting > waitexploring 

                                             > waitcall

                               > calling >

I have bunch more but I think you can see where I’m going with it. so what I’m trying to do Is have a save button and a load button on the menu when I push save it will save the scene you are in, then you can just push the load button from the main menu. the thing is that I also need it to be able to go back to the saved scene from a cold start. how would I achieve that?

Sorry, I’m not exactly sure what the problem is, so let me ask some more questions to approximate the problem.

Is this saving/loading problem because of using the Corona Composer? Or would it be the same problem if you are not using Corona Composer? If the latter then I can give you some hints on how to make save games. If it is a special case related to Corona Composer… I have no experience with Corona Composer.

Honestly my problem is that I have no idea where to start when it comes to saving/loading a game, and none of the tutorials that I’ve looked at are making any sense.  

Ok, I will try to give some hints. Basically you need to write down in a file the state of your objects/levels. In example you need a variable where you store the current level number in, lets call it levelno. If your player is in level number four your variable levelno should store 4.

Nowadays JSON is a nice way to store such values. A LUA JSON module would create a string like {“levelno”:4}. This is JSON syntax.

Now think about which states you need to save. This could be player position, player hitpoints, player inventory etc. So in the end, when you press Save your save file in JSON Syntax may look like this:

{"levelno":4, "playerName":"Jim", "playerPositionX":150, "playerPositionY":275, "playerHitpoints":23}

When you press the Load button, the JSON module should be able to read the above save file and return a Lua table with these values. I have no exact example, but I think you returned table can be read like this

local levelnumber = JSONResult["levelno"] local player.name = JSONResult["playerName"] local player.x = JSONResult["playerPositionX"] and so on...

Here are the steps you need to do for SAVING:

  1. You need Coronas JSON module

    local json = require “json”

  2. Create a table of all values you want to save to file. Coronas JSON module needs a table to create a JSON string of your data.

    local myTable = {   [“levelno”] = levelno,   [“playerName”] = playerName,   [“playerPositionX”] = playerPositionX,   [“playerPositionY”] = playerPositionY,   etc. }

  3. Convert the myTable to a JSON String

    local jsonSaveData = json.encode( myTable )

  4. Save the encoded string to a file

    local path = system.pathForFile( “mySaveFile.txt”, system.DocumentsDirectory ) local file = io.open( path, “w” ) file:write( jsonSaveData ) io.close( file ) file = nil

I think for LOADING you get the idea now (use json.decode() )

I hope this helps understanding this topic.

Here are some useful links about JSON and reading/writing files:

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

http://docs.coronalabs.com/daily/api/library/json/encode.html

http://docs.coronalabs.com/daily/api/library/json/decode.html