How do I Save ONLY global variables values

Get help using Construct 2

Post » Wed Mar 15, 2017 1:16 am

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.
B
6
S
3
Posts: 15
Reputation: 610

Post » Wed Mar 15, 2017 10:05 am

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.
The LEVEL GENERATOR TOOLKIT



24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 237
Reputation: 2,296


Post » Fri Mar 17, 2017 12:09 pm

lamar wrote:https://www.scirra.com/tutorials/5343/hi-score-save-w-local-storage-plus-full-mobile-game-capx


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!)

https://www.scirra.com/manual/188/local-storage
The LEVEL GENERATOR TOOLKIT



24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 237
Reputation: 2,296

Post » Fri Mar 17, 2017 2:11 pm

mrtumbles wrote: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!)

https://www.scirra.com/manual/188/local-storage


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

Manual wrote:ItemValue
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.
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731

Post » Fri Mar 17, 2017 3:46 pm

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.
The LEVEL GENERATOR TOOLKIT



24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 237
Reputation: 2,296

Post » Fri Mar 17, 2017 11:44 pm

afte 2 days trying i finally managet make this works
thanx to all
cheers

Edit: https://www.scirra.com/manual/188/local-storage
"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?
B
6
S
3
Posts: 15
Reputation: 610


Return to How do I....?

Who is online

Users browsing this forum: ramyaswetha and 5 guests