How do I Save ONLY global variables values

  • HI all.

    i have problem, when game start i set on loader layout "on start layout -> load game from slot "Save" " and it hits me in the middle of game where was saved and play music from current load state, not from beginning.

    But what i actually want to save only global variables, and on loading screen just to load global variables values and then when load complete to go on main menu layout. i dont want load anything else but global variable values.

    is this posible? i really need this.

  • Instead of using the system's in-built save function, you could use LocalStorage and a Dictionary. Local Storage can hold values for you between play sessions, and the Dictionary will allow you to store multiple values in one.

    First you'll have to add both LocalStorage and a Dictionary to your game. When you want to save the game, first clear the dictionary using [ Dictionary > Clear ]. For each of the variables you want to store, use [ Dictionary > Add Key ] to add a key whose name is the name of the variable, and whose value is the contents of that variable. Then, use [ LocalStorage > Set item ] to have the event create a new LocalStorage value. Make the contents of that value Dictionary.AsJSON. This will store the whole data structure of the Dictionary under that value. Name the value "savefile" or whatever. That's your whole save action complete:

    [ Dictionary > Clear ] [ Dictionary > Add key } ( variablename , variablevalue ) [ LocalStorage > Set item } ( savename , dictionary.asjson ) <<< Repeat for each variable Fortunately loading is a lot simpler. When you want to load a save file, first you must check it exists. Use

    [ LocalStorage > Check item exists ] and enter the name you gave your save - "savefile" in my example. You must then use the trigger [ LocalStorage > On item exists ] , and the action [ LocalStorage > Get item ] - again, giving the name of the save. Finally, [LocalStorage > On item get ] use the action [ Dictionary > Load ] giving the value LocalStorage.LastData to send the Dictionary structure stored in the LocalStorage value back over to the Dictionary. Finally, set each variable to the stored value using [ System > Set variable ] to Dictionary.Get( "variablename" ).

    • - - >[ LocalStorage > Check item exists } ( savefile )
    [ LocalStorage > On item exists } ( savefile ) | - > [ LocalStorage > Get item } ( savefile ) [ LocalStorage > On item get } ( savefile ) | - > [ Dictionary > Load } ( LocalStorage.LastData ) | - >

    [ System > Set variable ] ( Dictionary.Get( variablename ) <<< Repeat for each variable

    Good luck! This should be a fairly robust system. You can also download the Dictionary.AsJSON to make the save files portable, and you can save multiple save files by simply changing the name of the LocalStorage value for each new file, whilst still retaining the integrity of the file itself. You can also debug save files using this method a lot easier, so if your users have a problem you can easily establish a system to allow them to send a save file for investigation, and it should be easily loaded at the other end.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads

    This is only a solution for storing a single variable - each LocaStorage set/get requires so many actions. Each time OP adds another variable he wants to save it will take minimum four actions to resolve, some of which appear to be missing from that tutorial by the way (the LocalStorage.ItemValue follows on from an 'On item exists' check - in which the ItemValue is not passed!)

  • some of which appear to be missing from that tutorial by the way (the LocalStorage.ItemValue follows on from an 'On item exists' check - in which the ItemValue is not passed!)

    You might want to read that manual entry again - You can use LocalStorage.ItemValue in On item exists.


    The value for a key that has been read or written in any Local Storage trigger, such as On item get or On item exists. This can be either a string or a number. This returns 0 if used outside of a Local Storage trigger.

  • Apologies, read this and took it as it was written:

    "When the read or write is complete, a trigger fires (On item get or On item set) which indicates either the value is available to read (with the ItemValue expression) or that the value was successfully written."

    Nonetheless - LocalStorage alone doesn't provide a solution to the question.

  • afte 2 days trying i finally managet make this works

    thanx to all



    "This is also separate to the browser cache"

    so, if i make build for android and ios, this save will still work?

    If somebody clears cookies, cache or anything in phone or browser, will he lose save data?

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