Loading data from sor into an array?

I have a multi-dimensional array and I need to save its data into my shared object file and then load it back into the array again. Given that the array has loads of elements I don’t want to do it variable by variable. I saved it out by sending the information out using a for loop and putting in & to spearate the data.
Next I load it up into a variable and convert it to a string. Now I want to read the string and load it up into the array again, incrementing the array every time it hits &. The code is below for this. It is supposed to load up the contents of element [x] [3] :

function loadGame() {
mysavegame = SharedObject.getLocal(“flashcookie”);
if (mysavegame.data.datastring == undefined) {
// No object exists
trace(“No Saved Game”);
} else {
trace(“Loaded Game”);
bigstring = mysavegame.data.datastring;
bigstring.toString();
trace(bigstring); //traces out my data separated by &
k = 0;
for (i=0; i<bigstring.length; i++) {
if (bigstring.charAt(i) != “&”) {
datum += bigstring.charAt(i);
myArray[k][3] = datum;
i++;
} else {
i++;
k++;
trace("myArray [k] [3] is "+(myArray[k][3]));
}
}
}
}
trace(myArray[1][3]);// traces as undefined!!

There’s probably a much more sensible way to do this but I haven’t been doing actionscript long. The trace from this is weird. The first element always seems to get missed and so do the last four. Furthermore when I try to trace the data outside of the load function it traces as undefined. Why?

This is the Trace:

Loaded Game
392000&467200&199800&321600&297600&198400&195000&201600&139200&162000&171600&102600&106000&
myArray [k] [3] is 467200
myArray [k] [3] is 199800
myArray [k] [3] is 321600
myArray [k] [3] is 297600
myArray [k] [3] is 198400
myArray [k] [3] is 195000
myArray [k] [3] is 201600
undefined

Any ideas out there?

Well I believe that you can just save an array to a SharedObject
So no need to turn it in to a string.


 loadGame = function () {
     savedGame_so = SharedObject.getLocal("flashcookie");
     if (savedGame_so.data.multi_array == undefined) {
         trace("No Saved Game");
     } else {
         trace("Loaded Game");
         _root.multi_array = savedGame_so.data.multi_array;
         trace(_root.multi_array);
     }
 };
 saveGame = function () {
     savedGame_so = SharedObject.getLocal("flashcookie");
     if (savedGame_so.data.multi_array == undefined) {
         trace("Saving Game");
         savedGame_so.data.multi_array = _root.multi_array;// easy
         trace(savedGame_so.data.multi_array);
     } else {
         trace("There Is Already A Game Saved");
     }
 };
 

:slight_smile:

Thanks for your suggestion. When I tried this the trace message I got was as follows:

Loaded Game
[object Object], [object Object], etc. etc.

Which looks a bit odd. Why doesn’t the trace command trace what data was loaded?

But the data seems to have loaded fine, so thanks a lot for that.

Thats because flash does not list out all the info for each array, if you try to trace an array you will get that [object],[object] stuff. You need to get used to using the debugger for flash set some break points to better understand whats going on. It will make things much more simple. If you are still having problems after a while I can help you out.

-Boozy

C:-) Cheers Boozy. I may just do that!

Boondogger