XML – The Dialog System’s Bread and Butter

I knew that we needed to write our dialogs outside of Unity, because it would be way to much text, and editing and keeping track of text within Unity is a pain. I knew from previous experience that XML is used a lot in Unity and AAA games but I only had very basic insights on how to use it. I researched on my own for a while and had a few different approaches based on tutorials from the Unity wiki or some other sites.

what other two words start with F and Y ?
Very first test, just to see whether I could get Unity to read my file
xml with conditions and nodes
Attempt at a variable system, when we hadn’t decided on the InventoryItem system we are using now

For the first few tests I was using the Unity XmlSerializer and I noticed that it had quite a few flaws that would make my work more annoying. For once I had to write three scripts just to read the file properly. I also found out that the serializer only goes to a certain depth of tags, which meant that I would have to create a different XML file for each character, so we would end up with quite a few files.

too many files
One script contains the Class “DialogString” another script is used to store multiple DialogStrings and the third script actually reads the XML-file

So eventually I decided to ask Markus, my programming professor, for help. I was quite happy to hear that my approach wasn’t absolutely terrible, but he recommended reading the XML file “directly” instead of using unity’s serializer. He gave me an example Unity Project which explained the method very well, so I could quickly implement it. Now I needed only one script to read the file, and I could put all dialogs in one single XML file too! I had already revised the XML file after our decision on the InventoryItem approach so I only had to tweak that a little bit.

Template and Example of a PlayerTopic (aka DialogItem aka InventoryItem)
The structure of the final XML file, the different tags refer to different methods that will be called by the reader

I then wrote a prototype Scene for it, using a few buttons and textfields to test whether it was all working as intended.

I know it's pretty disgusting
Lots of beautiful, beautiful, debug text

Finally, this is the core method for reading the XML-file.

Go through the XML file and search for the NPC name, then for the item name, and then do something for each element in that item. Also features a default answer of the item is not found

Now we only had to implement this in the main project. That’ll be a whole other post though.

1 thought on “XML – The Dialog System’s Bread and Butter”

Leave a Reply

Your email address will not be published.