How do I wait until data is saved before switching Layout

Get help using Construct 2

Post » Tue May 24, 2016 9:38 am

Hi. I added the ability to save achievement completion in my new app and it works fine most times. But sometimes the data is not saved before a load action is executed resulting in wrong display or lost data.

I tried some things like wait for signal but I could not make it work yet.
First I tried the save/load actions but when I go to home layout after load complete I had an endless loop (needs loader layout I think) and I had problems with persistent object positions. So I changed to local storage and dictionary.

The setup.
User clicks Sprite. Frame is switched and saved to dictionary. Key is the uid, value the frame number.
On End of Layout (does this work on closing the app?) the dictionary is saved to local storage as json.
On start of layout local storage get item is executed. On get item complete json is loaded into the dictionary.
Then right below a for each key loop sets Sprite with key (uid) and switches frame to the value.

The problem is when you load the same layout or switch too fast the data was not saved properly. Any ideas how to fix this and wait until data is stored?
B
54
S
16
G
6
Posts: 719
Reputation: 9,207

Post » Tue May 24, 2016 10:42 am

Maybe if I put switching layouts in a function?
Like this:
On End of Layout > Save to local storage, Call function
in Function: On Data saved > switch layout?

But what happens when closing the app? Will it execute the function code first?

I will try a few things but any suggestions are very welcome. :)

The current code looks like this:
Image
(line 6 is left over and doing nothing ;))
B
54
S
16
G
6
Posts: 719
Reputation: 9,207

Post » Tue May 24, 2016 1:39 pm

You can check if a Set is processing and delay your get until it is done.

http://www.blackhornettechnologies.com/ ... edGet.capx
ImageImageImage
B
71
S
21
G
231
Posts: 3,688
Reputation: 129,352

Post » Fri May 27, 2016 5:44 am

Thank you. I will try to add it. :)

But end of layout does not execute when you close the preview/app?
I tried to wait for signal in "On End of Layout" but it does not seem to work.

The help says:
On end of layout
Triggered when the layout is ending. This can happen when the project goes to a different layout or when the project closes.
But even with Browser > Close instead of just closing the tab/browser it does not seem to trigger the actions.
B
54
S
16
G
6
Posts: 719
Reputation: 9,207

Post » Fri May 27, 2016 1:22 pm

Or you can work with "Signals".

Something like that should work.

"Function.Save()"
--> Set LS key "Save"
--> Wait for signal "onSaveComplete"
--> Go to Layout "your layout"


On LocalStorage Item "Save" set
--> Signal "onSaveComplete"

So the function.save() holds till the the onSaveComplete signal was given. Then it goes to the layout.

Regards
Andy
Image Image iTunes: Match3 | Google Play: Match3
B
98
S
33
G
30
Posts: 1,118
Reputation: 27,793

Post » Fri May 27, 2016 2:09 pm

I have a different save method that works on 2 different games I made check this out. You can run the games yourself to check if it works. Any questions just ask

for highscore go here

https://www.scirra.com/tutorials/5343/h ... -game-capx

for saving game states , money systems go here

http://www65.zippyshare.com/v/1byvwy1x/file.html
Check out my awesome Android Game releases https://play.google.com/store/apps/deve ... ames&hl=en
B
38
S
18
G
24
Posts: 681
Reputation: 16,613

Post » Mon May 30, 2016 10:55 am

Thanks for the suggestion @AndreasR. I already thought about something like this with functions but this wont work when closing the app/game.
I think I just add a "Save" Button for now until I can find a sultion for that and add signal when switching layouts.
Saving on every "tap object" may be a bit too much?

@PixelPower Thanks for the links but the one above is broken. Can you repost it please?
I will take a look at them. :)

Is the manual not accurate @Ashley? What is meant with "when the project closes"? I tried to show a Browser > Alert popup window "on End of layout" and it works on reload or layout switch but if you Browser>Close or just close the browser tab the actions are not executed and no popup appears. Maybe the manual should be more detailed about this or is it a bug?
B
54
S
16
G
6
Posts: 719
Reputation: 9,207

Post » Mon May 30, 2016 5:00 pm

Check out my awesome Android Game releases https://play.google.com/store/apps/deve ... ames&hl=en
B
38
S
18
G
24
Posts: 681
Reputation: 16,613

Post » Mon May 30, 2016 7:16 pm

Local Storage: On Item Set (or On All Sets Complete) - you could use these triggers to log when all saves have been completed.
A big fan of JavaScript.
B
74
S
20
G
69
Posts: 2,207
Reputation: 43,836

Post » Tue Jun 07, 2016 12:02 pm

Thank you very much for the suggestions and examples!
I used a button to save the data for now cause "On End of Layout" does not work when the app closes like described in the manual.

I hope @Ashley can confirm if it is a bug or bad wording or if I just missed the point with "End of Layout" not triggering when closing the app.
B
54
S
16
G
6
Posts: 719
Reputation: 9,207

Next

Return to How do I....?

Who is online

Users browsing this forum: lukewasthefish and 15 guests