A really big problem with the Local Storage

Get help using Construct 2

Post » Fri Feb 05, 2016 10:37 am

I have used Local Storage in others projects to manage levels and badges, but something wrong is happening, maybe because I'm storing an array for the first time. Sometimes, when I return to the levels menu, I lost some registers (5 or more) that I have achieved. It happened during the preview (NW.js) and several times when I'm testing on my iPhone. I cannot post my capx, but I captured some screenshots about the event sheets involved in this issue. I hope that someone can help me to fix it. Thanks in advance.

Image

Image

Image

Image
B
73
S
13
G
2
Posts: 990
Reputation: 6,996

Post » Fri Feb 05, 2016 2:01 pm

If you call "Exists", you don't need to call "Get" in "On exists", it's already there at that point, so just "Load" is fine.
Doing a set in a For loop is a bad idea. You only need to save at the end of the loop. Having said that, your For loop doesn't change the array, so there is no reason to save at all. In "on touch", you call Set twice.

LocalStorage is an expensive operation. You want to call Set as little as possible, and only when necessary. If you've queued up dozens of saves and switch layouts, it's possible, that not all of the data is saved, as it's still trying to catch up in the background.
ImageImageImage
B
71
S
21
G
211
Posts: 3,613
Reputation: 119,145

Post » Fri Feb 05, 2016 2:25 pm

Hi @blackhornet

Thanks for all the explanations. I'll remove the unnecessary "sets" but I didn't understand what I do to set the array values correctly. Maybe in the last part of the ES, next to "Array > Set value at currentLevel..."?
B
73
S
13
G
2
Posts: 990
Reputation: 6,996

Post » Fri Feb 05, 2016 2:51 pm

Yes, that is where the data is changing, so that is where you could save, just like "Level_key", it is no different. An optimization could be to use Array(0) as your level-key and only deal with saving the array, instead of breaking into two items. Since I assume your levels start at 1, you have an empty slot sitting there anyway.
ImageImageImage
B
71
S
21
G
211
Posts: 3,613
Reputation: 119,145

Post » Fri Feb 05, 2016 3:18 pm

Thanks.

I've a doubt about the optimisation. If I change as you said, it can possibly improve the game performance or at least doesn't compromise it? I ask because I didn't know that localstorage was an expensive operation and I've 12 more localstorage items (for each in-app product, for ads control, sound control, etc). So, I believe that I'll have a lot of work to make this changes but If will improve my game, I'll do it. I only need some orientation of how to include all this items in the same array (using its height?) and I'll appreciate an example (a capx is not necessary).
B
73
S
13
G
2
Posts: 990
Reputation: 6,996

Post » Fri Feb 05, 2016 3:35 pm

In that case, use a dictionary for the other items. That way you have one array and one dictionary. Much better than coordinating 12+ variables.
ImageImageImage
B
71
S
21
G
211
Posts: 3,613
Reputation: 119,145

Post » Fri Feb 05, 2016 3:45 pm

Thank you.
B
73
S
13
G
2
Posts: 990
Reputation: 6,996

Post » Fri Feb 05, 2016 5:16 pm

@blackhornet

After the changes (without taking into account the dictionary implementation) the next level of the menu remains blocked even the previous mission is completed. As noticed, it happens when the array is loaded (I disabled the array loading to test it). Any idea about where's the problem?
B
73
S
13
G
2
Posts: 990
Reputation: 6,996

Post » Fri Feb 05, 2016 6:36 pm

I included an action after the increment of the level variable that solved this issue. However I'm not very confident that was the right solution. Is it right?

Image
B
73
S
13
G
2
Posts: 990
Reputation: 6,996

Post » Fri Feb 05, 2016 7:20 pm

It depends on what your level-unlocked code looks like, but if it needs to be at least 1, then this should be fine.
ImageImageImage
B
71
S
21
G
211
Posts: 3,613
Reputation: 119,145


Return to How do I....?

Who is online

Users browsing this forum: Maako, tarek2, VarunGulyani and 1 guest