# Should NPC dialog be stored in XML or in a script?

I'm developing an action RPG with some friends. I would like to know the differences and pros/cons of making NPC's dialogue using a file in XMLformat instead of using a script.

I see that script method is often used by game developers for NPC text, but is it better then a XML file? We've thought that a XML file with tags like

<FirstText>[text1]<SecondText>[text2]


et cetera is perfect for NPC text and also for possible quests to give the player.

So what are the differences between this two methods? Is a script suitable for this aim?

• That proposed XML format you have there is actually fairly useless (and malformed). – Josh Aug 6 '12 at 16:35
• Do you mean "a script" as in Holliwood-style script as Josh assumed? Or do you mean a script as in interpreted script such as Lua/Python/etc.? – Laurent Couvidou Aug 6 '12 at 16:36
• yes, I mean script such as Lua! @JoshPetrie you're right, but I've only gave an example not the true format. (it was just for saying that each kind of dialogue has a tag and contains the text itself, so I can read easily the right text from java ) – Andrea Tucci Aug 6 '12 at 19:30
• For the record, XML is overly verbose for many applications that don't require data interchange with systems outside of your control. Your framework of choice may have a good XML serializer/deserializer that reduces the importance of that, but often a simple text file will be more than sufficient. – 3Dave Aug 6 '12 at 21:43

Dialogue could be provided in any form/structure you wish it depends on how you parse the information that makes the difference. I will provide you with a basic XML syntax to get you started without understanding your games structure or language I afraid i cant provide an implementation.

<?xml version="1.0" encoding="UTF-8"?>
<npcs>
<npc name="Abstract">
<dialogue>
<text>Welcome #{PlayerName} to Stack Exchange, What would you like to know? </text>
<options>
<option action="dialogue5">Tell me about Stack Exchange?</option>
<option action="quest1">Give me quest</option>
<option action="object1">Give me object</option>
</options>
</dialogue>
<dialogue id="5">
<text>Stack Exchange is a fast-growing network of 87 question and answer sites on diverse topics</text>
<text>We build libraries of high-quality questions and answers, focused on the most important topics in each area of expertise</text>
</dialogue>
</npc>
</npcs>


As you can see its pretty much limitless to how you want to structure the XML.

Here i can provide a list of my npc's, their list of dialogue's, so in this particular example a player could talk to npc it would say "welcome what would you like to know?" and provide options which can be passed onto another dialogue or a quest or give item. So long as you follow the Structure of xml anything goes.

Hope this helps

• Thanks for this example! Having also a similar example on a scripting language such as Lua would be great. (I would like to know how scripting languages can be more powerfull than a XML format file) – Andrea Tucci Aug 6 '12 at 19:36
• Minor quibble: action="dialogue5" isn't very nice and will require string parsing in the driver. It should be something more along the lines of action="dialogue" ident="5" – Izkata Aug 7 '12 at 0:38
• it would be probably better to make it something like action="dialogue" param="5" and action="script" param="functionName()", with lua or something like that you can then easily invoke param as lua script – Kikaimaru Aug 7 '12 at 6:57
• @Izkata +1 I agree was more to simplify the format to see how you progress through the dialogue sets:) – AbstractChaos Aug 7 '12 at 8:53

Although XML is more predictable, kinda have a defined structure and all that. I would go with scripts.

Rolling your own language(if you have the time, but usually this is not the best choice), or even implementing one.

There are LOTS of script languages out there, they're all nice, some examples:
Squirrel, Lua, Angel Script, Python, and many many many others.

The choice of script over XML is not easy. XML is a straight path, easy to follow. Scripts are kind hard to implement, but if well implemented, gives you plenty of freedom.

Scripts are awesome because they gives you an organization that you may be already used to, make it easier for conditional statements, loops, acessing data. And also you can even making npcs execute algorithms made on the script itself. See this Ragnarok Online NPC Example:

mes "Hi adventurer, what do you want to drink today?";
switch(Select("Water", "Potions", "Nothing"))
{
case 0:
if($money >= 100) {$money -= 100;
giveitem waterbottle_id, 1;
next;
}
else
{
mes "Sorry, you don't have enough money :/";
next;
}
break;
case 1:
mes "Sorry, we're out of potions today.";
next;
break;
default:
break;
}
mes "Thanks for coming have a nice day!";
close;


This follows a structured form, XML's are like if you were putting lots of labels and jumping over everywhere.

But of course, only choose script if the project really worth it, you have the time, and you're confident while managing VMs.

You should generally prefer structured data (XML) over scripts at every opportunity.

The biggest reason for this is tool support. You can make a dialog editor for a structured format, which supports internationalization, dialog branching, testing a debugging of dialog trees, deadend and infinite loop detection in trees, etc. For a script, the only thing you can do is give te designer a text editor and make him play through all the dialogs in game to try to find bugs.

There is no single part of your engine work more important than your tools. They will make or break your game content. They will determine of you ship on time and under budget. They will allow you to build richer and larger content or restrict you to simpler and less content. All the pretty shaders and fancy dialog UIs and smart AI are useless if your content people can't make use of them.

You can of course also combine structured data and scripts in various ways. Your dialog system will probably need ways to optionally display some options based on certain game conditions. While you should strive for structured data there too, its entirely feasible to allow designers to add script conditions where the pre built ones aren't capable enough. Your NPCs can also use scripts to decide when to enter dialog or which dialog to enter into.

All that said, the actual choice of XML or not is kind of irrelevant. The point of XML is that it is structured and hence machine parseable into processable data structures. Nobody should need to actually be looking at the XML since the game editor UI should be used for making dialog (building a C# tool for this is trivial, and not doing it would be stupid). If XML is a problem for any reason, use another format. Prefer text or merging and conflict resolution in source control. When you ship, you'll be compiling the text files into efficient binary representations.

• what would you recommend using if you were to build this game in JavaScript to be embedded on a website? Can I store the text in a database and access with PHP, or should I go with JSON? If I do go with JSON, can you give a quick pseudocode example of how I would structure a sample script and give a sample parsing code? Thank you – Growler Jul 30 '13 at 13:36
• @Growler: whatever's easiest, it doesn't matter. if you don't understand how to use JSON, use something you do understand. a database is a very reasonable choice, as is most anything else you could come up with. – Sean Middleditch Jul 30 '13 at 16:18

While XML would make a great way to store your dialogue text, the most important aspect is the ease with which you can read it in, as well as how easy it is to write.

Keeping all your dialogue in one file (or multiple files in a single folder) will make it easier to add translations later. All you'd have to do to choose between them is decide which file (or folder of files) to read from.

An XML file would be a great choice, as it's a well-documented format, but is flexible enough to specify all sorts of things, including dialogue between NPCs if designed correctly.