Bazgir Posted March 9, 2017 Share Posted March 9, 2017 Hello, 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. Thanks Link to comment Share on other sites More sharing options...
Tom Atom Posted March 9, 2017 Share Posted March 9, 2017 Hi, your loading is strange - you should do this: player = JSON.parse(localStorage.getItem('player')); When saving, you stored string into storage and when loading, you get this string and with JSON.parse you turn it back into object. Link to comment Share on other sites More sharing options...
aaryte Posted March 9, 2017 Share Posted March 9, 2017 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')); console.log(bar); That'll log something like "Object {a: 1, b: 2}" to the console. Link to comment Share on other sites More sharing options...
Bazgir Posted March 9, 2017 Author Share Posted March 9, 2017 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 More sharing options...
bruno_ Posted March 9, 2017 Share Posted March 9, 2017 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 More sharing options...
Bazgir Posted March 9, 2017 Author Share Posted March 9, 2017 Hello again, My next issue was that Chrome wouldn't run the updated code unless I was incognito, but that's resolved and the game is saving properly. Thank you everyone for your help Link to comment Share on other sites More sharing options...
Recommended Posts