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