How do I go through each key in storage individually?

0 favourites
  • 6 posts
From the Asset Store
The ultimate voice pack filled with 1,536 files of .......wav and mp3 of individual numbers, letters, and words (that go
  • 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.

  • 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.

  • Thanks a lot, it worked

    I don't really understand how to save and load a Dictionary object

    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.

  • 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.

  • I think I don't understand

    This is what the documentation saying:

    [quote:1hlow1d6]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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

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