Tuesday, 6 October 2009

Tweakables

I've been busy recently with XNA stuff, trying to get a nice physics model of a vehicle going. You soon discover that you accumulate a huge number of "settings" - constants that affect the behavior of your model. Tweaking these to get the best values you can (or maybe different styles of behavior for different "makes" of vehicle), can be a bit of a pain, restarting every time you want to make a tiny change and test it.

Imagine my delight then when I read this article by awesome XNA blogger Sean Hargreaves about "Tweakables" - the idea that you set up these constants so that you can adjust them on the fly, while the game is running. I knew I had to implement this - it's just too useful not too! (See Sean's next article for other potential uses).

I looked at Sean's suggestions for how to implement this in XNA ... but of course, I thought I had a better idea.

So I created a custom attribute, "Tweakable" (of course!), which I added to some of my static properties (which were simple, lazily coded public member variables until then). Then I created a form which iterates through all the types in my game's assembly, and for any "Tweakable" properties it finds, it adds corresponding controls onto the form (some work here to give them nice labels, organise into a tab per type, etc etc). Finally, an event handler is dynamically added to the controls so that the property is updated when the control is "left".

Et voila - when my game loads I also get a form for adjusting all the "tweakable" properties I like!

I intend to implement the ability to save all the current settings to file, and load them again, plus to start with an initial set. This way, I can save settings I like or which are useful for debugging, and I can exchange settings the rest of my "team" (there are three of us working on something together). I also need to expand the types of variables supported (admittedly just floats so far - will certainly need Vector3's which may require a custom control ...).

No comments:

Post a Comment

Post a Comment