1

I made a "save and load" function for my incremental game.

They both work, but when I load the data, I have to click a button to "update" the values. This is not a problem with the main currency (scrap), but the problem is with the variables collector,collectorCost,drone and droneCost.

Both the parts have the same code, so I don't understand why it shouldn't be updated automatically.

The "save and load" functions are called when clicking a button

I don't know if i need to show more code. I'm kind of new to this whole scene :).

Edit: Tried load() on page load but that doesn't change anything.

<button onclick="save()">Save Game</button>
<button onclick="load()">Load Game</button>
function save(){
  var save = {
    scrap: scrap,
    collector: collector,
    collectorCost: collectorCost,
    drone: drone,
    droneCost: droneCost
  };
  localStorage.setItem("save",JSON.stringify(save));
};

function load(){
  var savegame = JSON.parse(localStorage.getItem("save"));
  if(typeof savegame.scrap !== "undefined") scrap = savegame.scrap;
  if(typeof savegame.collector !== "undefined") collector = savegame.collector;
  if(typeof savegame.collectorCost !== "undefined") collectorCost = savegame.collectorCost;
  if(typeof savegame.drone !== "undifined") drone = savegame.drone;
  if(typeof savegame.droneCost !== "undifined") droneCost = savegame.droneCost;
};

I expect the numbers to be updated automatically, which is for the main currency but not for: collector, collectorCost, drone and droneCost.

They only change after that I click the button to buy any of them

Thijmen_l
  • 11
  • 2
  • Are there any errors in the JavaScript console? – Barmar Apr 10 '19 at 12:25
  • I tried to fix the spelling of the question, but I didn't understand what you mean with `but its happening to the parts that you buy`, can you rephrase it? – Christian Vincenzo Traina Apr 10 '19 at 12:26
  • maybe this can help you https://stackoverflow.com/questions/20231163/is-html5-localstorage-asynchronous – h1b9b Apr 10 '19 at 12:32
  • It sounds like you just need to call the `load()` function when the page first loads. – Barmar Apr 10 '19 at 12:32
  • @h1b9b how does that help? If it were asynchronous it could be a problem, but that question says it isn't. – Barmar Apr 10 '19 at 12:33
  • @Barmar the spec doesn't say if it sync or async, the second answer on that question get into the details of it – h1b9b Apr 10 '19 at 12:35
  • @h1b9b It ends with **The last sentence mandates that scripts accessing the same storage object are required to see things synchronously.** – Barmar Apr 10 '19 at 12:36
  • @Barmar the answer was edited in 2016 and all the recent comment on that question experience the same behavior – h1b9b Apr 10 '19 at 12:38
  • @Thijmen_I if you already on save have the new values why load them from the localstorage ? – h1b9b Apr 10 '19 at 12:39
  • i dont have it saving/loading automaticly. i thought i had to have a separated button for saving to string the data and put it in local storage. and after that when i want to play again after closing the game i load it via the load game button – Thijmen_l Apr 10 '19 at 12:41
  • There are no errors in the javascript. – Thijmen_l Apr 10 '19 at 12:47
  • tried to call *load()* on page load but that doesnt change anything – Thijmen_l Apr 10 '19 at 12:54
  • Are you positive that the data actually ends up correctly in the localstorage when the game is saved? I’m guessing that the variables that aren’t restored are actually undefined there. – bleistift2 Apr 10 '19 at 13:00
  • well, i guess it is correctly stored, because when i do click on of the buttons the variables do update to their correct values. – Thijmen_l Apr 10 '19 at 13:02
  • Please provide more context on how the functions are called. If there is more code inside `save()` and `load()`, please also show that. – bleistift2 Apr 10 '19 at 13:15
  • added the html part how i call the functions *save()* and *load()* and there is no more code inside *save()* and *load()* – Thijmen_l Apr 10 '19 at 13:26
  • “I expect the numbers to be updated automatically […] They only change after that I click the button to buy any of them.” Could it be that the issue is in the UI? If you *inspect* the variables after the load, by using `console.log` or the browser’s debugger, are they what you expect? Is it only the *display* that is not updated? – bleistift2 Apr 10 '19 at 13:28
  • after i inspect the elements it indeed looks like it is the UI.. the elements do update when inspected. how would i go about updating the UI when the load() function happens? – Thijmen_l Apr 10 '19 at 13:47
  • This is impossible to say without knowing how your UI code works. How do you reflect the changes in the UI when the player buys the items? You need to trigger that logic in your `load` function, too. Likewise you might check what part of your code causes the update in the currency display. – bleistift2 Apr 10 '19 at 19:53
  • so i guess im a bit late :) but i found the problem, in a function i was calling some data where there was none. nonetheless thanks guys – Thijmen_l May 06 '19 at 12:14

0 Answers0