Restoring custom class object array from SharedObject

1.4k Views Asked by At

I have an array of Widgets (a class I created) called "widgetArray" that I save into a shared object.

savedGame1.data.widgetArray = widgetArray;

When I go to load this data out and use the widgets I run into problems. The array is stored fine, but each widget is saved as an object. So I can't simply do:

widetArray = savedGame1.data.widgetArray;

because then if I try to use the widgets:

widgetArray[0].someWidgetFunction();

it does not work because flash thinks they are objects. I have tried typecasting the widgets when I load them, but it only produces a null object.

widgetArray[i] = widgetArray[i] as Widget;//widgetArray[i] becomes null
tempWidget:Widget = widgetArray[i] as Widget;//null as well

This question is asked in one of the forums from a friend, since there were no responses, i thought this is the right place for it... How does anyone else deal with this?

2

There are 2 best solutions below

1
On BEST ANSWER

Make two methods that can save and load a widget, the save method should take the data from the widget class and save it into the shared object the load class will take the data from the shared object and set the properties of the object.

Something like this :

public function save():Object
{
   return {
      "x":this.x,
      "y":this.y,
      "otherStuff":otherStuff
   };
}

public function load(data:Object):void
{
  this.x = data.x;
  this.y = data.y;
  this.otherStuff = data.otherStuff;
}   

You can call the save method and store the results in an array and then store it in the shared object. You only need to save the data that is required to rebuild the widget class, not all the properties of the class.

Edit : Updated based on BlueRaja's comment.

As BlueRaja pointed out IExternalizable is meant to be used for this.

If you have a class like this :

public class MyClass implements IExternalizable
{
    private var one:int = 1;
    private var two:int = 2;

    public function MyClass() 
    {
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeInt(one);
        output.writeInt(two);
    }

    public function readExternal(input:IDataInput):void
    {
        one = input.readInt();
        two = input.readInt();
    }

    public function print():void
    {
        trace(one);
        trace(two);         
    }
}

Then you can save it like this:

registerClassAlias("MyClass", MyClass);

var sharedObject:SharedObject = SharedObject.getLocal("so");

var myClass:MyClass = new MyClass();    
sharedObject.data['storedObject'] = myClass;    
sharedObject.flush();

Then to load it :

registerClassAlias("MyClass", MyClass);

var sharedObject:SharedObject = SharedObject.getLocal("so");

var loadedClass:MyClass = sharedObject.data['storedObject'] as MyClass;
loadedClass.print();

Hope that helps.

0
On

http://tush.wordpress.com/2007/07/08/actionscript-3-serializing-classes-using-registerclassalias/ ... This is also a genuine way to store the custom class objects in shared objects