0

My question is regarding using localStorage for a page. I've been making a game in JavaScript, and as it's quite a lengthy one to play, one problem has been that too often the tab is accidentally closed or the computer shut down before its completion. Therefore, I'm hoping to work out some sort of simple game save procedure so that such events will not cause the whole game to reset.

The data that I need to store consists of three arrays and one primitive value. Each of these are created when the user begins playing the game, and as the game is played, objects will be added to the arrays, and the primitive value- an integer- is incremented. The integer value should not be difficult, but the arrays consist of objects, rather than primitive values, and so JSON.stringify only literally returns "[object Object, object Object, object Object (...)]" as a string.

I've tried iterating over the arrays and stringifying each of the objects individually, but that seems sort of convoluted, I can't quite think round it, and besides, I seem to have done something wrong and it doesn't work regardless.

Here is the relevant moiety of the program thus far, if anyone wants to see for whatever reason:

var frogarray = [parent1, parent2, parent3, parent4]
var nestarray = [nest1]
var genesisarray = [];
pushGenesis( new Genesis() );
var boondollars = 0;

function getProgress () {
    var frogdata = localStorage.getItem("frogs");
    var nestdata = localStorage.getItem("nests");
    var boondata = localStorage.getItem("boondollars");
    var genesisdata = localStorage.getItem("genesis");
    frogarray = JSON.parse( frogdata );
    nestarray = JSON.parse( nestdata );
    genesisarray = JSON.parse( genesisdata );
    boondollars = boondata;
    frogtoUI();
    nesttoUI();
    printboons();
}

function saveProgress() {
    var storagefrog = [];
    var storagenest = [];
    var storagegenesis = [];

    function stringifying ( array, name, storagearray ) {
       for ( i = 0; i < array.length; i++ ) {
          var s = JSON.stringify( array[i] );
          storagearray.push(s);
          localStorage.setItem(name, JSON.stringify(storagearray));
       }
    }
    stringifying(frogarray, "frogs", storagefrog)
    stringifying(nestarray, "nests", storagenest)
    stringifying(genesisarray, "genesis", storagegenesis)
    localStorage.setItem("boondollars", boondollars)
}

Is there any way that I can retrieve and store arrays of objects in localStorage correctly- a simpler or cleaner way, if possible?

Edit:

Here's an example of an object from each array:

frogarray or genesisarray:

parent1 = {
gender: "female",
colour: "red",
eyecolour: "blue",
pattern: "plain",
sized: "small",
}

nestarray:

nest1 = {
typ: "normal",
nestTime: 0,
frog1: "",
frog2: "",
currentbreed: false,
}
user2870301
  • 31
  • 1
  • 3
  • FWIW, you are doing a bit too much encoding. It should be just `localStorage.setItem(name, s);`. `s` is already a string. – Felix Kling Oct 15 '13 at 04:08
  • 2
    Your question isn't about localStorage, it's about why JSON.stringify doesn't serialize your objects properly. Please list examples of your objects and the code you actually used to stringify them. There's nothing inherently wrong with stringifying an array of objects, e.g. `JSON.stringify([{'one':'two'}, {'three':'four'}])` – Sacho Oct 15 '13 at 04:08
  • My sincere apologies; I've added examples of the objects. As for the code I used to stringify them, what is given in the first section is all; I did nothing more on stringifying. – user2870301 Oct 15 '13 at 04:59
  • your array is a simple array and not an json object. please refer to http://stackoverflow.com/questions/3357553/how-to-store-an-array-in-localstorage – Rafael Herscovici Oct 15 '13 at 04:59
  • And, I'm sorry for the typo; I realized later that it should have been `localStorage.setItem(name, storagearray);` rather than 's'. Regardless, you're probably right that it's over-encoded; I tend to do that when it won't function correctly. >_ – user2870301 Oct 15 '13 at 05:00
  • Is it? It contains objects. (Parent1, parent2 etc., in case it wasn't clear, are objects. I probably should have been more explicit about that.) I've already read that question; I wouldn't have asked this question if it had solved the problem. Please correct me if I'm wrong, I would greatly appreciate it; I'm still in the learning curve. – user2870301 Oct 15 '13 at 05:04

0 Answers0