How do I save and load the sates of many objects using local storage?

0 favourites
  • 6 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Hello! Lately I've been getting familiar with using local storage to save a whole "Dictionary" object and use it for a save system. But the one thing I can't figure out how to do is this: How do I save the states of many objects? If a layout has fifty boxes and the player randomly destroys thirty of those boxes, how do I save all of the destroyed boxes states and "remember" the boxes that were destroyed when the player loads their save?

    Any thoughts or help would be greatly appreciated. Thanks!

  • You can use a Dictionary object, which you will save in LocalStorage as Dictionary.AsJSON.

    Next, save the state of your boxes in this dictionary. The key name for the dictionary is "Box."&Box.IID.

    The key value for the dictionary is Box.AsJSON.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the reply!

    I'll try out what you said and see how it goes.

  • Okay I did what you said but I'm getting some weird results...

    I set the dictionary's key value to the box once it's destroyed:

    Player on collision with Box, Box Destroy and Dictionary add key "Box." & Box.IID with value Box.AsJSON.

    I then save the dictionary to local storage when I call my save function:

    Local storage set item "Box_Data" to Dictionary.AsJSON

    After I load the Dictionary from local storage I then "load" the Dictionary keys:

    System on start of layout, Box set from JSON: Dictionary.Get("Box." & Self.IID))

    This is the problem: If I destroy the boxes and save, exit the game and load, the destroyed boxes are not the boxes the player destroyed but rather random ones.

    I've been going through my code but still have no clue to what I'm doing wrong.

    Thank you again for any help!

  • If you only need to know which instances were destroyed, then you obviously don't need to save their full state as JSON. If you place these objects manually on the layout, they will have fixed UIDs, so you can simply store the list of UIDs which were destroyed. Use an array:

    Player on collision with sprite: Array Push back Sprite.UID, destroy Sprite

    .

    However, if you need to know which instances were destroyed and also save all properties of the remaining instances, you can't use IIDs.

    From the manual:

    Since you are destroying instances, you need to use UID or a different (custom) identifier. Say, add an instance variable "Code" to the sprite, set a unique code for each instance.

    When leaving the layout:

    For each Box : Dictionary add key "Box."&Box.Code with value Box.AsJSON
    

    When returning to the layout:

    For each Box 
     Dictionary "Box."&Box.Code exists : Box set from JSON Dictionary.Get("Box."& Box.code)) 
     Else : Box destroy
    
  • Thank you for the explanation Dop2000!! This helps a lot!

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