Exportforce
Exportforce

Reputation: 97

Problems with loading from LocalStorage with JS

//Set Variables
var cookies = 0;
var cursors = 0;
var prestige = 0;
var cursorCookies = cursors * 0.1;
var save = {
    cookies: cookies,
    cursors: cursors,
    prestige: prestige
}

function loadgame(){
var savegame = JSON.parse(localStorage.getItem("save"));
 if (typeof savegame.cookies !== "undefined") cookies = savegame.cookies;
 if (typeof savegame.cursors !== "undefined") cursors = savegame.cursors;
 if (typeof savegame.prestige !== "undefined") prestige = savegame.prestige; 
}
onload(loadgame());

function round(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

function CookieClick(number)
{
  cookies = cookies + number;
  document.getElementById("cookies").innerHTML = round(cookies,2)
}
function buyCursor(){
    var cursorCost = Math.floor(10 * Math.pow(1.1,cursors));     //works out the cost of this cursor
    if(cookies >= cursorCost){                                   //checks that the player can afford the cursor
      cursors = cursors + 1;                                   //increases number of cursors
      cursorCookies = cursors * 0.1;
        cookies = cookies - cursorCost;                          //removes the cookies spent
        document.getElementById('cursors').innerHTML = cursors;  //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;  //updates the number of cookies for the user
    }
    var nextCost = Math.floor(10 * Math.pow(1.1,cursors));       //works out the cost of the next cursor
    document.getElementById('cursorCost').innerHTML = nextCost;  //updates the cursor cost for the user
}




window.setInterval(function(){
CookieClick(cursorCookies); 
localStorage.setItem("save",JSON.stringify(save)); 
document.getElementById('saveinfo').innerHTML = "Saved: " + Date();
}, 1000);

It's my first time working with javascript and localstorage so please bear with me, thanks :)

All that I am trying at the moment is to load 3 variables I am saving but something doesn't go right. My Testsite: http://atz-exportforce320313.codeanyapp.com/

Thanks for any help !

Upvotes: 1

Views: 2662

Answers (2)

GottZ
GottZ

Reputation: 4947

as i pointed out in facebook messenger:

if you access a variable inside the localStorage that has not been created yet, .getItem() will simply return undefined
if you pipe undefined through JSON.parse() it will just return null aswell.
to fix this you just have to check the response of JSON.parse() before trying to use it.

a quick example:

var savegame = JSON.parse(localStorage.getItem('save'));
if (savegame === null) {
    // initialize default values here or simply abort further execution
    return;

    // if cookies, cursors and prestige are global variables you can access them through window:
    var variables = ['cookies', 'cursors', 'prestige'];

    variables.forEach(function(name) {
        if (name in savegame) window[name] = savegame[name];
    });
}

now additionally you are trying to save the state through this line:
localStorage.setItem("save",JSON.stringify(save));
wich will not work since you are not writing your variables into save
you essentially have to add them together from your global variables first like:

var variables = ['cookies', 'cursors', 'prestige'];
variables.forEach(function(name) {
    save[name] = window[name];
});
localStorage.setItem('save', JSON.stringify(save));

PS:
in general i do not recommend defining your stuff inside the global scope

Upvotes: 1

Sagar
Sagar

Reputation: 497

Alright finally got to debug out your problem. The reason your code is not working properly is that at the start for first time, there is absolutely nothing inside your localstorage.

So you have to first check if the value retrieved from localstorage is blank or not.

Because of the problem here, your setinterval was not being called at all and thus there was no storage happening.

var savegame = JSON.parse(localStorage.getItem("save"));
 if (savegame !== null && typeof savegame.cookies !== "undefined") cookies = savegame.cookies;
 if (savegame !== null && typeof savegame.cursors !== "undefined") cursors = savegame.cursors;
 if (savegame !== null && typeof savegame.prestige !== "undefined") prestige = savegame.prestige; 
}

Also in case you have solved your first issue and still wondering why the loaded storage is showing all values 0, it's because you never update your save variable. Do that and you'll have your code working fine

Upvotes: 0

Related Questions