saving and loading settings with local storage

0 favourites
  • 4 posts
From the Asset Store
Creepy Sprite Font suitable font for your Dark/Horror/Halloween games.
  • I am quite new to the topic of saving and loading, so I'd appreciate some help.

    I want to save and load a bunch of settings by using the local storage. For example, I have a global bool variable, that checks if sounds are active.

    When the player closes the settings menu, I want the settings not only be stored in the global variable, but stored in the local storage as well, so that the player does not have to readjust their settings the next time they start the game.

    I found the option to save data to the local storage by using "set item xyz to myValue", and I use "get item xyz" when the game starts.

    The problem now is that I can't set a global bool variable to the content of another variable. I only have the option to toggle, or to set it to true or false. I can't set the bool to whatever is stored in the local storage.

    I saw that there is something called binary data, but from what I read it did not lool like it is for booleans, but it looks like it works as some kind of buffer.

    Can someone give me a hint please on how I can save and load bools with the local storage?

    Tagged:

  • What I do is:

    Every time a variable (including Booleans) is changed, I call a "save" function.

    This function writes ALL the variables I want to save to local storage (not just the one that just changed) to a dictionary ("Add key" action)

    For example, if I have a Boolean named "mute":

    Key = "mute"

    Value = mute

    This stores either a 0 or 1 in the dictionary against a label of "mute" (I use the same names for labels in the dictionary as the variable I'm storing there).

    After I've saved all the variables to the dictionary, I then save the dictionary to local storage.

    So, my dictionary is called "saved", so on the LocalStorage object, I use the Set Item action with:

    Key = "saveData"

    Value = "saved.AsJSON"

    So now there's a single key in local storage named "saveData" with a value of the JSON of my dictionary that contains ALL the values.

    To load them:

    On start of layout - LocalStorage - Check item "saveData" exists

    Then:

    LocalStorage - on item "saveData" exists:

    On the "saved" dictionary object, use "JSON - Load" with:

    JSON = LocalStorage.ItemValue

    Now the whole dictionary has been loaded back from local storage

    Then I can read through the dictionary for all the values loaded back into it and set the corresponding variables. For the "mute" Boolean above, the event would be Compare Values on the "saved" dictionary object where:

    Key = "mute"

    Comparison = "= Equal to"

    Value = 0

    And the action would be to set the mute variable to "False"

    Then add an Else that sets that variable to "True"

    For an integer or string variable, I just set the variable to the value of the corresponding key in the dictionary.

    Hope that make sense!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you both, so if I understand correctly, you don't use the actual booleans for this, but use number variables to store 0 or 1?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)