LocalStorage won't load. It works when I restart Layout

0 favourites
From the Asset Store
In this template the music plays without looping in your game
  • My code is working, but only when I click Restart Layout.

    So when first Layout is loading then nothing is happend. You need to click restart layout to script load.

  • Run the project in Debug Mode and check the variables. I'm guessing the values are loaded from Local Storage just fine, and the problem is with other events. (You should wait for the values to load, before trying to access them)

    Also, check out this demo showing an easier way to save many values in Local Storage:

    howtoconstructdemos.com/easily-save-multiple-values-in-local-storage

  • Yes it's true there is no global variables on this Event Sheet. But why do they fill when I press Restart Layout. Then everything works properly and the game is loading.

    I just can't include here an event with all Global Variables, because it will be overloaded. Also I can't make special Event only for Load and Save because then I need to copy Globals and they'll got extensions 1,2,3...

  • Yes it's true there is no global variables on this Event Sheet.

    This is not what I said! It doesn't matter in which event sheet your global variables are defined.

    I meant you need to run the game in Debug Mode and check your global variables - Levels, Experience etc. You should be able to see if they contain default values, or if the values loaded from Local Storage.

    .

    I think the values are loaded correctly, but you are checking them too soon. It takes some time to load data from Local Storage.

    If you can't figure out the issue, please post your project file.

  • I thing its better to set your variables after you get the local storage.

    On item exist -> get item

    on item get -> set var to localstorage

  • savvito123 "Item get" and "On item get" steps are not required, this just adds many unnecessary events. From the documentation:

  • > I think the values are loaded correctly, but you are checking them too soon. It takes some time to load data from Local Storage.

    Yes Variables are loaded correctly but the map does not respond until I press start Layout. Only then can you see the finished Levels. BEcause when you got LEVEL > 1, then level icon change to green.

    Now system dont check it. Like below.

    Restart button fixes it right away and everything is OK, but I cant promot user to do it.

    I used WebStorage on C2 before and everything was ok.

  • That's what I keep telling you - you need to wait until the data is read from local storage. This doesn't happen immediately, like with Webstorage. You can't compare Level variable in "On start of layout", because this value has not been retrieved yet.

    What you can do to fix this is move this Level check into a function. And call this function from both "On item Level exists" and "On item Level is missing" events.

    This is another reason why it's better to save all values at once in a dictionary. This way they will all be loaded at once and you won't need a hundred of different "On item exists" events.

  • I just removed "on start of layout" after level check and it works (for now)

    But I have problem with save game, because after finishing level and back to map there aro na data about all of Global Variables. How my save looks:

  • I just removed "on start of layout" after level check and it works (for now)

    So these events now running on every tick? This is not a good solution..

    You need to post your project file, because the problem is with how you save the values. The problem is likely with the order of events in your project.

    Local Storage events need time to complete! This means that you need to wait for them to finish before doing anything else. For example, if you are saving data to local storage, you cannot immediately switch to another layout and try to load the same data, it won't work.

  • Project is so huge that it takes weeks that someone could understand it.

    Now levels selection are just normal even sheets and they are showing corerctly when Layout is starting.

    And save game happens when level ends and its not working. What I can do to make save (if its corect code) to put in to "back to map' event.

    Project was working on C2 with WebStorage but performance was medium. Game is here:

    play.google.com/store/apps/details

  • I don't see any mistakes on that second screenshot where you save all those values. So the problem must be somewhere else.

    Are you sure this event is executed? Do you hear the "winnn" sound playing?

  • OK, maybe I was installing app without deleting previous version. Now its look ok, so Thanks much!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Are you sure this event is executed? Do you hear the "winnn" sound playing?

    However, not everything is always saved in memory after completing the game level. In the example you gave there is something like "StorageData" in save template.

    What "StorageData" is, should I use it?

    Is it better when I do like here?

  • StorageData is a dictionary.

    The purpose of the dictionary in my demo is to save/load all values at once in Local Storage. When you have 50 variables that you want to save in Local Storage, you need 50 "Set item" actions, 50 "Check item exist" actions, 50 "On item exist" events... But if you put all values into the dictionary, then you can save it with just 1 "Set item" action, and load it with just 1 "Check exists" action. Much shorter and cleaner code!

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