How do I combine LocalStorage and loops?

Get help using Construct 2

Post » Sat Oct 22, 2016 2:04 pm

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.

Image

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:

Image

Is there perhaps a different way I could do it? Any help would be hugely appreciated!
B
11
S
2
Posts: 13
Reputation: 601

Post » Sat Oct 22, 2016 2:37 pm

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.
ImageImageImage
B
71
S
21
G
233
Posts: 3,696
Reputation: 130,368

Post » Sun Oct 23, 2016 6:27 pm

Hi @Dingo! I'm interested in the problem, I'll try to think of something to help. :)
Image
B
10
S
3
G
2
Posts: 78
Reputation: 1,729

Post » Mon Oct 24, 2016 12:43 am

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!
B
11
S
2
Posts: 13
Reputation: 601

Post » Mon Oct 24, 2016 6:40 am

Ok! You're welcome, maybe I'll actually help next time! :P See ya!
Image
B
10
S
3
G
2
Posts: 78
Reputation: 1,729


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 28 guests