How do I save and load localstorage from an array?

0 favourites
  • 10 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • 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

    dropbox.com/s/rgxwm0f0buc8qv2/Wolf_runv1.capx

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

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

    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
    
    [/code:1uwjo4qt]
    
    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.
  • 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.

  • 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 sounds something that could benefit a more complex game or database.

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

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

  • Save system with star achievement: https://www.dropbox.com/s/vh3n39sk8z2dh ... .capx?dl=0

  • Try Construct 3

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

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

    hill-climbracing2.com hill climb racing 2

  • 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

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