0

I'm designing a game with save and load functions. Using the SharedObject technique I'm successfully saving most of the variables. However, there is this one Dictionary that sometimes ends up as "undefined". This happends perhaps 1/3 of the times i load the game.

This is my save function. It runs approximately every 2. second. I've removed a lot of code lines since they aren't relevant (saving the other variables). The trace line is for debugging purposes. Every time, also when the above mentioned error accurs, this line works. Consequently the dictionary isn't "undefined" at this moment.

    private function saveGame():void
    {
        so = SharedObject.getLocal("progress", "/");

        so.data.saved = true;

        so.data.airportDict = airportDict;
        trace(dayCount, so.data.airportDict["Australia"]);  
        so.flush();
    }

The following lines run every time you open the program. The purpose is to decide whether there is a saved file to load or not.

so = SharedObject.getLocal("progress", "/");

if (so.data.saved == true)
{
    loadProgress();
}

else
{
    airportDict = new Dictionary();
    resetAirportDict(); // This function just add lots of data to the dictionary.
}

And finally, the loading function:

private function loadProgress():void
{
    so = SharedObject.getLocal("progress", "/");

    airportDict = so.data.airportDict;

    trace("Successfull start? " + airportDict);
}

As already mentioned, the airport dictionary's value is "undefined" maybe 1/3 of the time I run the program. For no appearant reason. This is a mystery.

user3262713
  • 369
  • 8
  • 20

1 Answers1

0

SharedObject can store primitive types + Array and Object types. For other complex types you have to register the classes you wish to store using registerClassAlias(). In your cases Dictionary is a complex type that is not handled by default by SharedObject + the complex objects you might use as keys will have to be registered as well. So any complex class you use has to be register or else SharedObject will fail.

registerClassAlias("YourClassName", YourClassName);

This piece of code produce error because SharedObject doesn't understand complex objects:

var shareddata:SharedObject = SharedObject.getLocal("mydata")           
var instance:MyClass;
if(shareddata.data["instance"] == undefined)
{
    instance = new MyClass();
    shareddata.data["instance"] = instance;
}
else
{
    instance = shareddata.data["instance"];         
}

But you can easily fix it by registering your class:

import flash.net.registerClassAlias;

registerClassAlias("MyClass", MyClass);
var shareddata:SharedObject = SharedObject.getLocal("mydata")           
var instance:MyClass;
if(shareddata.data["instance"] == undefined)
{
    instance = new MyClass();
    shareddata.data["instance"] = instance;
}
else
{
    instance = shareddata.data["instance"];
}
//no error and you truly get back your custom class instance the second time around.
BotMaster
  • 2,233
  • 1
  • 13
  • 16
  • 1
    Addition: If that complex class extends `DisplayObject`, it must implement `IExternalizable` to be stuffed into a shared object. – Vesper Jul 31 '14 at 14:37
  • Could you explain a bit further how I should implement this into my code? And thanks for your response! – user3262713 Jul 31 '14 at 17:33
  • @BotMaster user3262713 asked you a question. I don't see any answer. That's not respectful: voted down! – helloflash Jan 13 '15 at 15:08