开发者

C# XML serialization and involved inheritance

I have a 2D game engine in XNA that is developing quite well. It's laid out as such:

A central GameManager, contains a List of SceneObjectManagers (for layering).

SceneObjectManager contains a collection of SceneObjects (a class that inehrits from List, with a operator [string] overloaded), which is an abstract base class with a string for the object's ID.

There is a SceneObject2D and AnimatedSceneObject2D classes, which inherit in a chain (2D from SceneObject, aniamted from 2D). They contain relevent information (position, source rectangle, animations, etc).

I then have a second project (the actual game which uses the engine), which has things like PlayerObject, AIObject, FireballScript, etc, which inherit from SceneObject2D and AnimatedSceneObject2D depending on what it needs (if it's displayed, animated, or even neither).

There's more to it, but you get the picture.

I've begun the idea of tackling a level editor, because writing out hundreds to thousands of entities to build a level up and positioning them perfectly by trial and error or extensive on-paper math would just be madness.

Fairly simple concept... brush, select entities, set attributes, and ultimately export a XML file which would contain every entity, their positions an开发者_开发知识库d attributes, ID's, etc. The problem, though, is with the XML serialization itself to save objects.

Essentially, I'd serialize the GameManager, which contains all sceneobjectmanagers, which contains all scene objects. Then I could just load the XML file in and there would be my level. But then there's the problem of inheritance, and more complex objects containing objects (and interfaces and all that).

I can't see myself writing out [XmlInclude(typeof(T))] for every single entity for every class that will be a base class, not to mention the more complex objects.

I started inheriting IXmlSerializable, but before I go too far into that endeavor of writing out WriteXml and ReadXml for all my objects, I'd like to know if there's a better, easier way to go about this.

I'd point out that it needs to be a portable format (hence XML) since I'd like to re-use the editor's exports, with some tweaks, for use in a C++ engine which uses the same classes layout (I'm thinking of using Boost.serialization for it).


I ended up using an open-source XML serializer called YAXLib. It stores the "real" type of the concrete classes when serialized, and I've managed to serialize my collection of scene object managers.

I can deserialize them later on in my C++ engine once I have all of my engine ported over (I'm probably going to write my own deserializer using TinyXML, due to previous experience with it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜