Local Storage not saving at the right time.

0 favourites
  • 8 posts
From the Asset Store
Basic Plugins for Firebase Authentication, Realtime-Database, Firestore, Cloud Storage
  • I have a clunky save system in my game that only works at a certain time.

    This is what I want to happen:

    -Each time an enemy gets destroyed, Add 1 to Baddie Count.

    -When the hero dies, save the storage item as the current BaddieCount.

    -On loading a new layout(the Main Menu), set BaddieCount to the saved item.

    -Continue this loop for each play-through, so that BaddieCount will keep adding, even after the game is closed.

    Once BaddieCount reaches 500, a new character will be unlocked.

    What is happening:

    Everything works as expected, but the LocalStorage doesn't save until I complete a second playthrough.

    For example when playing through de-bugmode:

    Round 1, I kill 20 enemies, BaddieCount sets to 20. I close game. Baddie count is back to 0.

    Open again. Round 1, I kill 20 enemies, BaddieCount sets to 20. Round 2, I kill 10 more enemies, BaddieCount sets to 30. Now If I close the game and open, the BaddieCount will be set to 20, not 30.

    It's as if my data is saving one play-through too late. Any ideas on how I can fix this? I have included an image of the relevant code.

    Thanks

  • Any action with the clock icon is not completed instantly.

    You might be switching layouts before the storing is completed.

    You need to wait for the "item set" trigger to happen before allowing switching/restarting layout.

    Also the line baddies=>500 will trigger constantly when it reaches 500, add another valuation if unlockedYce = 0

  • Okay,I can fix the second part with a condition.

    Does the rest of the code look okay though? If things did work instantly, would you expect this code to do what I want it to do? For whatever reason, I have a lot of trouble making sense of Local Storage events.

    I'm trying to figure out the timing and where the critical point is. I have so much trouble making sense of this though.

  • Yeah, the rest looks pretty much fine.

    There is one extreme condition that might happen, where you manage to kill a baddie before the get complete and restored to the saved value, then it wouldn't count that one. But if you can't kill anyone in the first second or two (depends on the speed of the device reading the local storage) of the gameplay it's not an issue.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That won't be an issue. On any of the six levels you can't kill a Baddie before 5 seconds.

    I think I have found the exact issue, but don't know how to resolve it. It is as you suspected.

    The line "Heroes on Destroyed" -> Local Storage -> set "baddiecount" to BaddieCount doesn't save until the following play through. Something is stopping the newest data from saving. I have tested many times and it always saves the score from the previous playthrough, not the one I just finished.

    For reference, this is what happens to end the layout. The first event "destroy heroes" is what triggers save event, but it's a separate event entirely. This Life event is below the saving event in the list of code. Not sure if that matters.

    Also, I have a few of these saves happening, one for all monsters, one for a specific monster and one for a Boss monster (defeat boss 5 times to unlock). Could they be interfering with each other?

  • First of all, if this is not a subevent of a taking damage function/event you should put a "trigger once" on it, otherwise it will run a whole bunch of times.

    To make sure everything is saved you need to move the wait 5 seconds and everything below that to when the "item set" is triggered (though in general 5 second should be more than enough to save everything, which is a bit strange).

    You can loop your saving, meaning once the first is set run the second and when that is set run the third, then do the rest.

    You can put them all in one event with a "wait for previous action to complete" after each set item action.

    But I would recommend to just push all your save data into a dictionary and just save that once with the dictionary.asJson expression.

    You can read the values directly from it too without using globals and you can clear it - same as resetting globals.

  • I finally got it to work! This was the last bug keeping my "beta" version from sending to my friends to test. Thank you for all the help and suggestions Headbang Games.

    I went through all my code and assured that each "set item" would only happen once, either through a trigger, or by setting a second condition. Then I added an All Sets Complete sub-event to my Life =0 event. Picture below.

    I'm a 7th grade teacher and just do this as a hobby to teach my students some coding. Maybe dictionaries will be the next thing for me to explore.

  • Sure, no problem.

    I wasn't aware of the "all sets complete" trigger, it's pretty new.

    Though keep in mind that this will only work if all your sets are fired at the exact same time, most likely from the same event, if there is any delay between them, they might not get batched together, then the all set will trigger multiple times.

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