How do I go through each key in storage individually?

Just started using Construct 2? Post your questions here

Post » Sun Mar 12, 2017 2:59 pm

Hi all!

What I'm trying to do is, save values using the local storage under key names set by the player, means I don't know exactly what the key names are going to be and on start of the layout I want to load all keys from storage and add each key to a list object.

There is an action to "load all keys" but I can't find a "for each key" or similar to be able to go through each and every key individually and add them to the list object one by one.

Anyone could point me in to the right direction please?

Thanks.
B
4
S
1
Posts: 28
Reputation: 406

Post » Sun Mar 12, 2017 4:00 pm

https://www.scirra.com/manual/188/local-storage
On all key names loaded
Triggered after the Get all key names action completes. In this trigger the KeyCount and KeyAt expressions give the list of all the key names.

I'd recommend using a dictionary for this kind of thing. Then there is only one object to load, and you can iterate over the keys in a similar manor.
ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704

Post » Sun Mar 12, 2017 5:36 pm

Thanks a lot, it worked :)
I don't really understand how to save and load a Dictionary object :roll:
The documentation mentioning something about the asJSON expression and AJAX to save and load the content as JSON.
Should I use the "post to URL" and "request URL" to save/load the dictionary as JSON (using the Dictionary.asJSON expression at "Data") then use the Load action to load the info from JSON file in to the Dictionary?

Thanks.
B
4
S
1
Posts: 28
Reputation: 406

Post » Sun Mar 12, 2017 5:53 pm

No, AsJSON just converts it to a string, so you just save it in LocalStorage as text. Then you load that text back in and Load it.
ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704

Post » Sun Mar 12, 2017 7:04 pm

I think I don't understand :(

This is what the documentation saying:
AsJSON
Return the contents of the Dictionary object in JSON format. This can be later loaded back with the Load action


The load action in the dictionary object require a JSON file but the local storage can not save or load JSON but individual items :|
I'm confused, is there any example or tutorial how to save and load a Dictionary object with all items?
B
4
S
1
Posts: 28
Reputation: 406

Post » Sun Mar 12, 2017 9:25 pm

Finally I managed to get the Dictionary work. Technically, it is require only 3 simple steps:

1. To Save Dictionary in to LocalStorage just Set Item as JSON string using the "Dictionary.AsJSON" expression.
2. To Load Dictionary, first Get Item from LocalStorage using the key previously used to save Dictionary
3. Finally, check if you get the Item then Load Dictionary from JSON as string using the "LocalStorage.ItemValue" expression

That's it. After, I can simply go through each item in the dictionary one by one as I wanted and add them to list, check their number, value, text..etc Awesome

Thanks for all the help.
B
4
S
1
Posts: 28
Reputation: 406


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 3 guests