0

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Artemix
  • 8,497
  • 14
  • 48
  • 75
  • If both swfs have the same version of the same class I would image this should automatically deserialize, the only issue I could think of is if the object is a different version on the interacting clients. With AMF being used with BlazeDS a RemoteAlias metadata tag is used or register alias but I believe that's always just a Java class name or PHP class name to map to the AS3 Class, I would imagine since this is all AS3/AMF the type would be retained. Are you sure both clients have the same exact swf? – shaunhusain Jun 08 '12 at 05:23
  • Yeah, they are the same swfs. – Artemix Jun 09 '12 at 16:21
  • I understand, but, the Action class requires in the constructor an EntityVO custom object, why is this working then?, it shouldn't. – Artemix Jun 25 '12 at 16:40

1 Answers1

1

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.

  • In my case, I send an array of "Actions", wich is a class that consist in an action type (string) and an entity (EntityVO class). So, if I do registerClassAlias("model.Action", Action) it should work? – Artemix Jun 08 '12 at 14:25
  • It doesn't work, it says that there is an argument count mismatch on Action (expected 2, got 0), I did this on both ends: registerClassAlias("model.EntityVO", EntityVO); registerClassAlias("actions.Action", Action); – Artemix Jun 09 '12 at 16:21
  • Ok, I needed to add default arguments to my Action class constructor, but, the object type is still "Object". – Artemix Jun 09 '12 at 16:41
  • Yup, it worked, I tested using ByteArray until I noticed that I needed one more class to be added, so far, I have 5 classes registered with registerClassAlias, otherwise, it doesn't work. Thanks for the ByteArray tip :) – Artemix Jun 12 '12 at 01:26
  • You still there? :p it was working like a charm, until I decided to use parameters into my objecst into my Action main object.. I've setted a default constructor, but now the values for the parameters are gone, example, if I set a class like Entity(point:Point = null), the final value for that variable ends up beign null. Any ideas? – Artemix Jun 23 '12 at 01:02
  • I've just tried to add Point using registerClassAlias, but didnt work :p – Artemix Jun 23 '12 at 01:05
  • Update, if I change the type of the parameter constructor from Point to a couple of x and y, it works, but... its not the idea. – Artemix Jun 23 '12 at 01:20
  • I've just tried the byteArray trick, and the result is the same, when I do a readObject it pops an error saying my Point variable is null. – Artemix Jun 23 '12 at 01:38
  • Yeah, I did register Point, but still when I do a readObject from byteArray, I get null as constructor parameter. – Artemix Jun 23 '12 at 14:48
  • I just added some code to explain better the situation, if you have any doubts, let me know :) – Artemix Jun 24 '12 at 18:21