When a user completes the game loop or starts a new game, I want to clear all local storage while still keeping certain values intact.
Currently, I am able to do this for sound volume values:
// code inside a conditional statement triggered when starting a new game
if (newGameBool === '1') {
var tst = myAu;
// myAu is the stored sound value set by the user using an input range
localStorage.clear();
localStorage.setItem("Au", tst); // After clearing local storage, set the same value again
UI.myLoad(); // Reload function that interacts with local storage
}
How can I achieve the same for keys with iterating numbers attached to them?
Here is how I save these keys:
var i = +v + +1;
localStorage.setItem("v", i);
var vv = localStorage.getItem("v");
localStorage.setItem("LdrBrd_" + vv, JSON.stringify(LdrBrd)); // Saves data with iterating key names
Implementing retrieval similar to the sound function:
var gv = v + 1; // Retrieve the value from local storage and adjust for off-by-one error. gv is a local variable.
if (newGameBool === '1') {
var ldd, vg;
for (var ii = 0; ii < gv; ii++) {
var ld = localStorage.getItem("LdrBrd_" + ii);
if (ld != null) {
// Values to retain beyond the clearing point
ldd = JSON.parse(ld); // Parse saved JSON string data
vg = ii; // Number of values retrieved
}
}
localStorage.clear();
for (var xx = 0; xx < vg; xx++) {
var nld = localStorage.getItem("LdrBrd_" + xx);
if (nld != null) {
localStorage.setItem("LdrBrd_" + ii, JSON.stringify(ldd));
}
}
localStorage.setItem("v", vg);
UI.myLoad();
}
I have been using console.log() at different points to monitor the process. I commented out the clear function just to check if the values were incorrect, but they did not save at all. I attempted to create a fiddle, but local storage was not functioning there. In Visual Studio, everything works fine, but the script for this file is nearly 2000 lines long, so I tried to organize it as best as I could.
Thank you in advance for any assistance or advice.