Deserializing objects received using Cirrus

366 Views Asked by At

I'm using Cirrus to pass some values to other players in my game, and some of those values are objects, thing is, that when I receive those objects, they lost their type, and they become generic objects.

I've read that Cirrus uses AMF, but I don't know how to regain the original object type of my data.

Edit.:

//these are the classes involved

registerClassAlias("Action", Action);
registerClassAlias("EntityVO", EntityVO);
registerClassAlias("Point", Point);

//Action takes 3 parameters
Action(type:String = "", entity:EntityVO = null, target:EntityVO = null)

// when EntityVO doesnt require a parameter in the constructor or it has a string/int parameter this works:

var entity = new EntityVO();
var byteArray:ByteArray;
byteArray = new ByteArray();
byteArray.writeObject(action);
byteArray.position = 0;
var object:Object = byteArray.readObject(); //<- works ok

//when I make EntityVO to take a non standard parameter like, a Point, like this:

EntityVO(point:Point = null)

//and I do this:

var entity:EntityVO = new EntityVO(new Point());
var action:Action = new Action("addEntity", entity);
var byteArray:ByteArray;
byteArray = new ByteArray();
byteArray.writeObject(action);
byteArray.position = 0;
var object:Object = byteArray.readObject(); //<- it goes into the EntityVO constructor and says that point is null, (I use point in the constructor to set something)
1

There are 1 best solutions below

10
AudioBubble On

You need to do two things:

  1. registerClassAlias("alias", classOfTheObjectSerialized) this tells Flash player it needs to use "alias" string when reading and writing classes from/to the writable/readable medium (such as Socket, ByteArray, NetConnection and so on).

  2. Ensure you did this on both ends (both sending and receiving) and that the objects being serialized don't have non-default arguments in constructor, their properties are also serializable (i.e. adhere to the same rules as described above).

PS. You also need to be aware of that some objects are inherently not serializable, for instance, none of display objects are, objects that operate on resources such as streams aren't serializable too. Even BitmapData isn't serializable due to not having default constructor.