Reputation: 1
i'm trying to serialize the entire scene so I can save it to a file, and load it later. I know that Pv3D is not the best 3D engine to work with right now, however I don't want to start the whole project again. The problem comes when trying to load the actual file, and assign it's data to the scene it gives a #1009 error (null object).
Here's the code:
/* Papervision3D engine setup code here */
//...
var scene:Scene3D = new Scene3D();
//...
var file:FileReference;
function LoadProyect(e:MouseEvent):void
{
var fd:String = "3Dp Files (*.3dp)";
var fe:String = "*.3dp";
var ff:FileFilter = new FileFilter(fd,fe);
file = new FileReference();
file.browse(new Array(ff));
file.addEventListener(Event.SELECT, onFileSelect);
file.addEventListener(Event.COMPLETE, fileComplete);
}
function onFileSelect(e:Event):void
{
file.load();
}
function fileComplete(e:Event):void
{
var aScene:ByteArray = e.target.data;
var objScene:Object = aScene.readObject();
scene = objScene as Scene3D;
}
So, are Scene3D serializable? (I guess they are, they actually output data when serialized in a plain text file) and is this way possible? Or should I save each object on it's own and load it one by one, and then add it to the scene?
Upvotes: 0
Views: 43
Reputation: 5255
Chapter 2 of "What can you do with bytes?" should give you a starting point for reading and writing objects to ByteArray
.
Speaking of which, the core functionality lies in writeObject()
and readObject()
, both linking to registerClassAlias()
as an related API element.
Its documentation speaks for itself:
Preserves the class (type) of an object when the object is encoded in Action Message Format (AMF). When you encode an object into AMF, this function saves the alias for its class, so that you can recover the class when decoding the object. If the encoding context did not register an alias for an object's class, the object is encoded as an anonymous object. Similarly, if the decoding context does not have the same alias registered, an anonymous object is created for the decoded data.
You need to register the class in order to deserialize objects of it.
The problem with that is that only public
members of a class are de-/serialized this way by default. In order to define a custom de-/serialiszation, you'd have to implement the IExternalizable
interface:
The IExternalizable interface provides control over serialization of a class as it is encoded into a data stream. The writeExternal() and readExternal() methods of the IExternalizable interface are implemented by a class to allow customization of the contents and format of the data stream
Of course, that'd be a lot of work, because Papervision3D does not implement that interface. You could extend the relevant classes and implement the interface, but chances are you miss something. You'd have to know all the inner workings of the classes. See if registerClassAlias()
is sufficient first.
Also check out this related question.
Upvotes: 0