How do I combine LocalStorage and loops?

0 favourites
  • 5 posts
From the Asset Store
For Adventure, Action, Heroes, Sci-fi and Action-RPG games.
  • Hello yet again! Managed to find myself another conundrum that I was hoping somebody could shed some light on.

    This is a problem that's part of a somewhat convoluted system, so I'll try to focus only on the issue. Basically- I'm creating a save/load system for my game. It's strategy like, and set on a map which uses a few different arrays to store game info. I'm using the "AsJSON" expression to save the data into LocalStorage. Saving the game just writes these arrays to relevant keys, and loading pulls these keys back and loads the data back to the arrays.

    This works great. However, I'm struggling to figure out how to handle loading the game's "Faction Units" arrays. This is an array that I use to store the information for each faction's units. On game start, I loop through the Faction Data array which stores the faction information and create a Faction Unit array for each faction, using the index as the connecting ID.

    To be clear- there is only one Faction Data array, which has an entry for each faction, but a Faction Units array for each faction listed in the the former.

    Creating and saving these arrays as keys is fine, as I can use a For Each X loop to create a key for each array instance, and use CurX to pick the relevant Faction Unit array and store the ID in the key name.

    The problem lies in then loading these. I think the root of my issue is that loading is a two step process- you have to "get" the key, and on the separate "on found" trigger you can load the data to an array. But, I can't use the CurX expression in the "on found" trigger because of course it's separate from the loop. So I'm not sure how I'd go about reloading these arrays... this shows what I'm trying to do, obviously it does not work however:

    Is there perhaps a different way I could do it? Any help would be hugely appreciated!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'd recommend using a single dictionary to store the array. Using a similar mechanism, create a dictionary entry for each array, using the same key, and the array's AsJSON as the data. Then you only have one object to save and load.

  • Hi Dingo! I'm interested in the problem, I'll try to think of something to help.

  • Actually blackhornet's solution works perfectly! I didn't even think of being able to save the dictionary object into a single key, now I just have to reload the dictionary and run a loop on it to retrieve the specific keys.

    Cheers for the help guys, hugely appreciated!

  • Ok! You're welcome, maybe I'll actually help next time! See ya!

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