Arbitrary object deserialization

Started by
1 comment, last by Wavesonics 16 years, 2 months ago
So here is the idea. We have game objects described in XML, such as:

<GameObject>
  <width>100</width>
  <height>50</height>
  <texturepath>button.png</texturepath>
</GameObject>



Obviously simplified, but what we do is load this from XML into a Property object. Then the calling code gets this object and can tweak the values easily to make a unique button. Once done, the Property object is passed to a templated function: createObject<Button>(). Then GameObjectFactory creates an object to the specifications and returns it as the correct type. Our only problem is the way in which we currently set the members of the objects being created, currently it is crude and inflexible. It was only intended as a hold over till we could find a better way, so now we are trying to find a better way. Does anyone have any ideas? The factory gets a bunch of "properties" all of the correct data types, but how does it know how to put them into the right parts of this specific class of objects? ====== [edit] So it turns our this is a whole area of study called XML Data Binding, doesn't seem to be a lightweight way of doing it elegantly ATM. [Edited by - Wavesonics on January 29, 2008 4:03:42 PM]
==============================
A Developers Blog | Dark Rock Studios - My Site
Advertisement
It doesn't; the object classes do. Give them a constructor that accepts a dictionary of values and sets its members accordingly. The factory determines the type of thing to instantiate and calls an appropriate constructor, passing it the dictionary that it created by parsing the XML.
Brilliant! That is a more elegant way of implementing the method I had just thought of, ++rating :)
==============================
A Developers Blog | Dark Rock Studios - My Site

This topic is closed to new replies.

Advertisement