How do I save and load localstorage from an array?

Just started using Construct 2? Post your questions here

Post » Tue Dec 20, 2016 6:01 am

Message: celstrider can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
I need the game to remember how many levels have already been finished and the score for each level.

I think I'm pretty close, but no joy so far in getting it to work.

I'll link the capx below. I'd appreciate any advice.

browser:chrome
windows vista


https://www.dropbox.com/s/rgxwm0f0buc8qv2/Wolf_runv1.capx?dl=0
B
25
S
16
G
26
Posts: 125
Reputation: 16,103

Post » Tue Dec 20, 2016 9:20 pm

You can return Arrays and Dictionaries as JSON.

You would then use two LocalStorage keys.
One for the level you are on/the number you finished and one for the dictionary with the scores as JSON.
(Dictionary.AsJSON)

When you want to get the scores from LocalStorage,simply load the Dictionary from the JSON in the LocalStorage value.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Tue Dec 20, 2016 9:24 pm

why do you use arrays to save levels or asjson? you only need 1 variable

Edited : My bad 2 or more global variables, currentlevel, maxlvlreached,hiscore,score,coins,dogs etc
and more localvariables for all the items you use. so you can give them a number assigned to them and no UID or IID wont work.

Code: Select all
at end of layout > save item "level"&currentlvl 
                                        > save item "hiscore"&currentlvl 
                                        > save item "score"&currentlvl 



//then in main menu//

for repeat maxlevelreached = call item "level"&1+loopindex // (1+loopindex in case you run the loopindex with a 0 based loopindex if you want you can try using for "" from 1 to maxlevelreached then call item "level"&loopindex)


for any itemget

pick by comparison  levelicon

"level"&levelicon.number = localhost.key > set level to localhost.itemvalue



now pressumable you know what i talked about, then you dont need arrays, or any json file or asjson. dont know who told you that.
B
78
S
23
G
69
Posts: 1,353
Reputation: 44,025

Post » Tue Dec 20, 2016 9:44 pm

@gamecorpstudio
I think, your option is easier, but since saving to LocalStorage, saving a dictionary as JSON takes less time since it only saves one Local Storage item instead of multiple.

No offense, just trying to figure out what's more efficient.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Tue Dec 20, 2016 10:13 pm

randomly wrote:@gamecorpstudio
I think, your option is easier, but since saving to LocalStorage, saving a dictionary as JSON takes less time since it only saves one Local Storage item instead of multiple.

No offense, just trying to figure out what's more efficient.



no offense taken, ha ha, i just didn't knew array are saved as 1 file in json ... might be much less time consuming your approach, but maybe a little to complex?

mine just uses direct numbers, for prebuilt like template games, nothing that is randomly generated. which i think his template doesn't need to over complicate to much, since its a small sidescrooler.

im now curious about your array/ json approach :D sounds something that could benefit a more complex game or database.
B
78
S
23
G
69
Posts: 1,353
Reputation: 44,025

Post » Tue Dec 20, 2016 10:49 pm

@gamecorpstudio well, actually, in this case, it uses Dictionary.AsJSON, but you can use Arrays in more complex scenarios too.

It simply works like this:
  • Add all values you need to the Dictionary/Array
  • Now you can share the Dictionary/Array as one single string with Dictionary.AsJSON or Array.AsJSON
  • When somebody receives that string, he can simply load his Dic/Arr with the JSON he just received
  • Like this, you can technically share hundreds of values in one string
This is especially useful for Multiplayer games to send statistics or sth. similar to other peers.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Thu Jul 13, 2017 10:09 pm

I'm a bit new on LocalStorage and Dictionaries, so I'm not sure if these are valid questions or if C2 is set up to prevent this.

While a Dictionary is being turned in to a Json do we need to worry about a race taking place with the system wanting to update a Dictionary Key while it is being turned into a Json? I think there won't be, but want to make sure.


What happens if player closes the game while a string is being written to LocalStorage? Is it allowed to finish?
B
68
S
37
G
13
Posts: 241
Reputation: 12,785

Post » Fri Jul 14, 2017 7:08 am

Save system with star achievement: https://www.dropbox.com/s/vh3n39sk8z2dh ... .capx?dl=0
ImageImage
B
114
S
51
G
24
Posts: 790
Reputation: 23,480

Post » Mon Jul 31, 2017 10:09 am

Nice! I want you to thank for your time of this wonderful read!!! Your blog posts are more interesting and informative. I very like and visit regularly.
http://hill-climbracing2.com hill climb racing 2
B
2
Posts: 1
Reputation: 152

Post » Tue Aug 01, 2017 12:53 pm

Saving a whole Array to local storage is as easy as:
Action: Local Storage - Set Item
Expression;
Key: "Name"
Value: Array.AsJSON

tada! then coming back out after you Get Item "Name" with LocalStorage It goes like this:

Condition: On Item "Name" Get
Action: Array - Load From JSON String LocalStorage.ItemValue
B
11
S
3
Posts: 24
Reputation: 723


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests