Shorter, easier and more effective way of saving data?

Thanks to this great community, I managed to make a save system for my game! However, it is kind of long and painful to write. I need help on how to (if possible) make this a little bit shorter and easier.

So, this is what I got (this example uses ONLY 1 variable to get the idea):

GameState = loadsave.loadTable("GameState.json")

if GameState == nil then
    GameState = {
        TotalPixoosGS = 0,
    }

    loadsave.saveTable(GameState, "GameState.json")
end

TotalPixoos = GameState.TotalPixoosGS

Let me quickly explain what I got here: GameState = loadsave.loadTable("GameState.json") is declaration of a table from .json file. I used .loadTable function which I got from here:

https://code.coronalabs.com/code/easy-saveload-table-data-corona-sdk

if GameState == nil then
        GameState = {
            TotalPixoosGS = 0,
        }

If the file doesn't exist, it simply creates default game settings, in this case sets TotalPixoosGS variable to value of 0.

loadsave.saveTable(GameState, "GameState.json")

Again a function from the link above which simply creates .json file from GameState table, calls it GameState.json.

TotalPixoos = GameState.TotalPixoosGS

Finally, assigns the value of GameState.TotalPixoosGS to our variable TotalPixoos (if GameState.json doesn't exist, it will assign the value of 0). So far, pretty straight-forward, right?

Now, this code was called upon OPENING the application.

The following code is what I use upon EXITING application:

if ( event.type == "applicationExit" ) then
      GameState = {
            TotalPixoosGS = TotalPixoos,
      }

      loadsave.saveTable(GameState, "GameState.json")
end

Basically, what this does is: When the app is about to exit, you simply change the value of TotalPixoosGS to our current value of TotalPixoos (that is the variable which changes over time). Finally, we save the GameState table with the new, changed TotalPixoosGS.

Once the app launches again, the process will repeat.

Now, what is the problem? Well, when you have 1 variable, like in this case, it looks simple. However, when you gotta manage over 50 variables, it can be a pain to write. Is there any way I could shorten this?

Anyway, this is just a small chunk of code, to get the idea:

GameState = loadsave.loadTable("GameState.json")


if GameState == nil then
    GameState = {
        TotalPixoosGS = 0,
        PixoosQuantityGS = 0,
        PixoosPerSecondGS = 0,

        BottlesQuantityGS = 0,
        MaxBottlesFoundGS = 0,
        BottlesPriceGS = 0.005,
        WillFindBottlesChanceGS = 10,
        CanFindBottlesGS = 0,

        MaxClickableGS = 5,
        MaxTrashCanValueGS = 5,
    }

    loadsave.saveTable(GameState, "GameState.json")
end

TotalPixoos = GameState.TotalPixoosGS
PixoosQuantity = GameState.PixoosQuantityGS
PixoosPerSecond = GameState.PixoosPerSecondGS

BottlesQuantity = GameState.BottlesQuantityGS
MaxBottlesFound = GameState.MaxBottlesFoundGS
BottlesPrice = GameState.BottlesPriceGS
WillFindBottlesChance = GameState.WillFindBottlesChanceGS
CanFindBottles = GameState.CanFindBottlesGS

MaxClickable = GameState.MaxClickableGS
MaxTrashCanValue = GameState.MaxTrashCanValueGS

See how much of ugly code there is? And this is upon exit:

if ( event.type == "applicationExit" ) then
      GameState = {
            TotalPixoosGS = TotalPixoos,
            PixoosQuantityGS = PixoosQuantity,
            PixoosPerSecondGS = PixoosPerSecond,

            BottlesQuantityGS = BottlesQuantity,
            MaxBottlesFoundGS = MaxBottlesFound,
            BottlesPriceGS = BottlesPrice,
            WillFindBottlesChanceGS = WillFindBottlesChance,
            CanFindBottlesGS = CanFindBottles,

            MaxClickableGS = MaxClickable,
            MaxTrashCanValueGS = MaxTrashCanValue,

       }

       loadsave.saveTable(GameState, "GameState.json")
end

Anyone with a better way, or idea on how to deal with this?

Answers


The problem here is that you are storing the data in independent global variables that you need to store in the table to save and then fetch from the table to load.

You can avoid needing to do all of that if you just use the table itself as the global variable in your general code.

So instead of writing code that uses TotalPixoosGS or PixoosQuantityGS when checking the values/etc. you write code that uses GameState.TotalPixoosGS and GameState.PixoosQuantityGS instead and then just save and load directly from/to the global GameState table.


Need Your Help

the difference between object with constructor and closure in javascript

javascript closures prototype

I am new to JavaScript, and I am now confused by the two conceptions: object with constructor and prototype, and closure.

can a restful URL consist if "?" symbol?

facebook api rest twitter

I have read in so many places that a restful urls should identify rresources with slash separated names as follows