Jump to content

Help with localStorage


Recommended Posts


I'm running into an issue where I don't quite understand the proper usage of localStorage to be able to save the user's progress. So far while the game works on android, when you close the app or even minimize/relaunch it, it starts a fresh copy of the game instead of picking up where you left off.

I understand that I need to tell it to save the player, but I'm not sure how to get it to save everything within the player (such as this.player.clickDmg, for example).

I've included below the player as well as their stats, and what I've got as the save function (baked into the on click for any clickable button in the game that makes adjustments to any of the player's stats), but I'm not sure how to get it to call those saved stats once the game is run.

How I picture the below code to work is that it sets the base stats, checked localStorage to see if anything is saved, and if there is saved progress ideally it would overwrite the default stats with the user's actual stats.

this.player = {
                clickDmg:            2,
                activeDmg:           2,
                paranoiaDmg:         2,
                DPS:                 0,
                PPS:                 0,
                paranoiaDPS:         0,
                Paranoia:            0,
                ParanoiaDebuff:      1,
                pickedLevel:         1,
                paranoiaBGone:       0,
                paranoiaTemp:        1,
                clickDmgBuff:        2,
                paranoiaDmgBuff:     1,
                paranoiaSpeedBuff:   2,
                DPSBuff:             30,
                PPSBuff:             10,
                paranoiaBGoneBuff:   3,
                paranoiaDPSBuff:     10,
                monstersKilledTotal: 0,
                totalTaps:           0,
                highestLevel:        0,
                highestParanoia:     0,
                highestUpgrades:     0,
                highestDPS:          0,
                highestTapsPS:       0,

if (localStorage.player) {
    localStorage.getItem('player', JSON.stringify(this.player));

saveGameProgress: function(player) {
    localStorage.setItem('player', JSON.stringify(this.player));


Any advice would be appreciated.



Link to comment
Share on other sites

Your saving code looks OK, but the loading code is definitely wrong. getItem() only takes one argument, and you need to JSON.parse() the return value.

Example use:

var foo = {a:1, b:2}
localStorage.setItem('x', JSON.stringify(foo));
var bar = JSON.parse(localStorage.getItem('x'));

That'll log something like "Object {a: 1, b: 2}" to the console.

Link to comment
Share on other sites

Hi guys, thank you for the help. So if I understand correctly, for loading it'd be something like:

this.player = +JSON.parse(localStorage.getItem('player')) || {
                clickDmg:            2,
                activeDmg:           2,

or alternatively

this.player = {
                clickDmg:            2,
                activeDmg:           2,

if (localStorage.player) {
            this.player = JSON.parse(localStorage.getItem('player'));

However I've tried both and neither are having the results I'm trying to achieve (default stats if no save data, if save data use the saved stats).

Link to comment
Share on other sites

You have a '+' sign in JSON.parse in the first.

In the second, the validation is wrong, you want something like:

var savedPlayer = localStorage.getItem('player');
if (savedPlayer) {
 this.player = JSON.parse(savedPlayer);

This assuming you have local storage available, in older browsers that's not the case.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...