Who am I? A Description Generator

Writerly scribblesExploration will be an important part of my space game. I want it to matter more than just zipping about, seeing what we can see. To start with I want to reward the player for finding new areas and I want the player to have to actively compete with other characters for the honour of being crowned discoverer of that object (and the honour of naming it, of course). As such these descriptions have to be a little more unique than “brown star”, “dwarf planet”, “moon”. I need them to feel like they’re individual, beautiful entities, born from the swirling fires of the galactic core and battered by the chaos around them.

So how do I do it? For every property, there is an individual component which handles both the value of that property and the different ways of describing it. Currently I have just two ways, “List” which describes the property in a short and concise way, as if it were an item in a bullet-pointed list, and “Verbose”, which describes the property in a full sentence to be used in paragraphs. Other methods will come later as the game expands. All of these components implement the interface IProperty which I define as follows:

Now let’s have a look at how that might be implemented. The below class is a component for describing the population of a planet:

All fairly self-explanatory here, so how do we use this? I’ve made a class called Describer which, when given a GameObject finds all components that implement  IProperty and gets their descriptions. I have implemented  Describer as follows:

This is a bit meatier, but still fairly simple. Let’s take it function by function.

DescribeList takes the object we wish to describe and turns it into an enumerator of strings in List format, as described earlier. It gets an IEnumerator of all the properties in the target GameObject and for every property it runs the function  ToListItem and adds the output to the result variable. For those who aren’t familiar with lambda expressions,  property => result.Add(property.ToListItem ()) is a shortcut for creating a delegate that takes an  IProperty parameter called property and executed ToListItem on it, adding the output to result.

Below that we have GetProperties which takes a GameObject and finds all components attached to that GameObject that implement IProperty, which it then returns.

Finally  DescribeVerbose is identical to  DescribeList with the exception that it returns a single string, it executes ToVerbose instead of ToList and it adds a little string formatting into the lambda.

Below I’ve pasted the output of using this system on a couple of objects. I’ve also added a few other components to show how Describer aggregates them in various ways:

There’s still a bit of work to do on this system, more properties, for one thing. Also, I need to keep it from returning the same sentence structures for different objects, because the lack of variety is inducing repetitiveness.

One nice side effect of this system is that you aren’t guaranteed to read the properties in the same order. This is a blessing and a curse, though. It makes sense to describe things by name first, or at least early in the description. And a strict order for list mode makes a lot of sense.

By now it should be fairly clear how my system can build descriptions from any number of components. It’s extensible, it’s easy to use and it’s quick to code. Hopefully it’ll come in handy for you too some day. If you have any questions / suggestions, feel free to let fly in the comments.

Nick out.

Fun fact: Mosquitoes love me too much.

One thought on “Who am I? A Description Generator

Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">