How do I wait until data is saved before switching Layout

  • 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?

  • 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:

    (line 6 is left over and doing nothing )

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

    http://www.blackhornettechnologies.com/ ... edGet.capx

  • 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: [quote:3g6ot3ck]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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • 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

  • Thanks for the suggestion 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?

  • Local Storage: On Item Set (or On All Sets Complete) - you could use these triggers to log when all saves have been completed.

  • 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.

  • It's by design that "On end of layout" does not trigger when you close the page - terminating the app is treated as different to switching layouts (which is what triggers on end/start of layout).

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