Xml Object Generation

So (there's that word again) I've been trying to work on a side project for awhile now. I think the whole side project thing formed a core component of my work ethic and if anything (outside of my wife and friends) got me through school, it had to be that. I started work on a new game called "Attack of the Robo Zombies" and my first approach was to go ballz out in making a prototype. I started slacking on creating an kind of design or plan to develop and instead got into the old trap of designing code while writing code. This is not 100% avoidable but you need to know the big picture before moving forward. Another problem was that as a side project, I was just trying to grind out something that didn't have an r&d hook that was keeping me interested in working on it. So in the face of all these problems I've started pretty much from scratch (again).

This time around, I'm building a prototype but I'm going to be using Bullet Physics to run dynamics for the world (making my life easier) and also focusing on developing a "robust" object generation system to make it easier to make the game data driven. This is more or less an experiment with Xml. So I figured, more for myself then anyone, I would dive into it here.

The problem that I encountered with previous projects was never developing a universal method to create objects from files. Usually I would end up writing several loading functions that would get a bit tedious since they only varied by a few things. While there are lots of solutions (and many better then the one I am about to go into), I started thinking about xml and how I wanted my solution to be driven by that as much as possible. For the game I wanted to make, I knew there would be many occasions where I would want to spawn things into the world. So lets say I wrote an xml file for a spawn point:

-spawnthis-
-enititylist-
-entity-
-/enititylist-
-spawnthis-

So from this, I wanted the ability to just say "Generate everything in ." Initially I thought yeah this could work but what if I wanted to add some kind of effect to be created with the spawn:

-spawnthis-
-enititylist-
-entity-
-/enititylist-
-effectlist-
-effect-
-/effectlist-
-spawnthis-

My normal inclination would be to write a special chunk of spawn code that would look for the EntityList and EffectList and generate special groups for each. But lets say down the road I wrote something else that wasn't a spawn point but had to do the exact same thing. So this got me thinking about making a system that forced me to write the object creation code in a generic/reusable fashion. So now I've created a static object called a Generator. The Generator has a map of Creator objects (where Creator is the base class) where each one is mapped to a name that corresponds to an xml tag (ie ). You give the Generator an xml node and it will start with the current node and go to the next one and so on. For each one, it looks for a Creator matching the tag and calls Create method. The creator allows for the unique code to generate that object. In most cases, this would create whatever was called for and also place it into the world (registering with whatever collections are neccessary). Now what if you needed to catch a reference to the newly created object? I provide the ability for a callback to be given to the Generate() function that will return a void* and name of the tag for each time a Creator is called for a node. I'm also going to add the ability to the Generator to store a list of "Default Nodes" that can be referenced by others. So for example:

-grunt-
-stats-
-/grunt-
-entity default="grunt"-

In this case, we add as a default to the Generator. When Generating , is retrieved from the collection of Default Nodes, it will pass both the and nodes to the Creator. This way if anything is not found in it will use defaults from .

There are definite drawbacks to a system like this. For one, it relys a lot on string comparisons and as of now, my use of Tiny Xml is creating a lot of strings on the fly so I'll need to iron that out. My hope is that by developing this feature now, I can use it to move prototype/game development faster. That and keep myself from regressing to doing nothing but playing games in my free time.

No comments: