views:

248

answers:

2

I want to load game objects from definition files (XMLs) and then just create them in game with prepared properties (like weapon, textures, max. speed, range of sight etc.). I thought of IClonable interface but it seems very weird. Also I need to differentiate between Units (soldiers, vehicles maybe aircrafts) and Buildings.

A: 

Sounds like you want a general factory that can call a specific factory. The generic factory takes the definition file and determines the object's generic type "weapon, unit, building". Then it passes the DOM tree to a specific factory for that type. The weapon factory knows how to construct all the different weapons in the system. And it passes the DOM tree to the weapon's constructor so all the details are available to the constructor.

How you write these factories is dependent on what your XML looks like and how "automated" you really want it to be. You need to provide more specifics to get a more specific answer.

EDIT: Now I get it. You want to use the XML files as a template for future object creation. You want a generic "vehicle" object. You load the XML file for "helicopter" and it creates a new "class" of "vehicle" without actually creating a class, right?

So, you create a vehicle "class" which all vehicles descend from. Then you create a vehicle template "class" which all templates descend from. The XML is used to instantiate a template class. The template class issed to instantiate the actual object.

class vehicle_template {
     vechicle_template(xmlstreamthing x);
     // properties found in vehicles, set from xml
};
class vehicle {
     vechicle(vehicle_template v);
     // properties vehicles have, taken from template
};
vehicle vehicle_factory(string s) {
    static map<string,vehicle_template> already_loaded;
    vehicle_template vt = already_loaded.find(s);
    if (vt == null) {
        xmlstreamthing xml = get_xml_from_object_name(s); // you need to figure this part out
        if (xml == null) {
            throw no_such_vehicle(s);
        }
        vt = new vehicle_template(xml);
        already_loaded.add(s,v);
    }
    return new vehicle(vt);
}

Obviously that's not runnable code but it should get your started.

I don't know how you are going to get these objects to behave differently, but that should solve your loading problem. Perhaps you need a duck-typed language where you can add methods to the objects at runtime.

jmucchiello
Thanks but I wanted to avoid parsing definition files over and over when creating object.
mnn
You don't parse it over and over. You turn it into a DOM tree once at first load. Then pass the tree to each factory until it hits the constructor. Then the original factory (which built the DOM tree) destroys the tree. Parse once, traverse a couple times, done
jmucchiello
But when it destroys the DOM tree, it has to create it again when creating another object. I was thinking of string id-based object definition and keeping Dictionary<string, XmlNode> or something like that.
mnn
Dictionary<string, string> makes more sense. Read the XML then throw it away. Although if your object data can't be entirely represented by a set of key/value pairs then you'll need some other way of storing an object archetype.
Kylotan
A: 

Don't implement IClonable. It doesn't differentiate shallow and deep-copying.

I think what you want is the Factory Pattern.

John Rasch