title: Game Logic in XML Files
body: <p>I'm dealing with game dialogue files (conversation between player and non-playable-characters) where dialogue choices and their outcome depend on certain conditions and result in certain actions. Now, I could write a simple parser to handle some sort of language for specifying the pre and post-conditions, but a friend of mine suggested using XML. Conditions could be stored as attributes of a dialogue element and choices and actions are inner elements. I'd then use an eval function to parse these conditions and statements (I'm using Ruby to make this game). To make such an approach simpler, I could then write a simple GUI to manipulate these files without worrying about ugly XML. </p>
  
  <p>But it strikes me as an odd choice to handle logic in XML files. My understanding is that XML files are for the storage and exchange of data, and I always read rants about how people overuse XML for all sorts of things it wasn't designed for. My friends responds by noting how XML IS used for everything, including XHTML and <a href="http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html" rel="noreferrer">this bullet description language</a> (which also illustrates some logic). </p>
  
  <p>To be honest, using XML would simplify a lot of things for me. Writing a parser can be painful and time consuming, and my requirements are generally simple. But is it really okay or would I regret such a choice later?</p>
  
  <p>For people interested in details, here's what a basic dialogue exchange might look like in an XML file:</p>
  
  <pre><code>&lt;dialogue id="101" condition="!npc.carsFixed"&gt;
    &lt;message&gt;Man, fix my car!&lt;/message&gt;
    &lt;choices&gt;
      &lt;choice condition="hero.carFixingSkill &gt; 5" priority="7" id="Sure!"&gt;
        &lt;command&gt;hero.carFixingSkills += 1&lt;/command&gt;
        &lt;command&gt;npc.carFixed = true&lt;/command&gt;
        &lt;command&gt;hero.playSmokeAnimation()&lt;/command&gt;
        &lt;command&gt;nextDialogue = 104&lt;/command&gt;
      &lt;/choice&gt;
      &lt;choice condition="hero.carFixingSkill &lt;= 5" id="I can't..."&gt;
        &lt;command&gt;nextDialogue = 105&lt;/command&gt;
      &lt;/choice&gt;
      &lt;choice id="Fix it yourself"&gt;
        &lt;command&gt;npc.likesHero -= 1&lt;/command&gt;
      &lt;/choice&gt;
    &lt;/choices&gt;
  &lt;/dialogue&gt;
  </code></pre>
  
  <p>The corresponding code if written in Ruby would be:</p>
  
  <pre><code>def dialogue101
    if !npc.carsFixed
      showMessage("Man, fix my car!")
      choices = []
      if hero.carFixingSkill &gt; 5
        choices.push(Choice.new("Sure!", 7)) 
      else
        choices.push(Choice.new("I can't"))
      end
      choices.push(Choice.new("Fix it yourself"))
      choices = selectTopPriority(choices) if choices.size &gt; 4
      result = showChoices(choices)
      case result
      when "Sure"
        hero.carFixingSkills += 1
        npc.carFixed = true
        hero.playSmokeAnimation
        dialogue104
      when "I can't"
        dialogue105
      when "Fix it yourself"
        npc.likesHero -= 1
      end
    end
  end
  </code></pre>
  
  <p>Stuff like likesHero and carFixingSkills are knowledge pieces player and NPCs can have, which would probably be stored in a hash in the real implementation. I find the dialogue file approach more flexible because I could make an editor to easily edit dialogue and conditions/actions, and because of the complex nature of game conversation trees. A scripting language like Ruby or Lua helps, but it'll require complex structures to handle to logic of such trees. </p>
  
  <p>Back to the original question, is XML the right tool for the job or am I missing something?</p>