# How to organize XML documents containing dialog trees for NPCs

This topic is 3517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
How about: Each NPC holds a reference to the resource containing its dialog text.

For instance:
<Npc Name="Bob">  <DialogResource>/resources/bob.dialog</DialogResource></Npc>

Now your organization doesn't matter so much, and you can write XSL transforms to move things around, if you decide to later put all of your dialogs under say /resources/dialog/.

##### Share on other sites
But in that case I would have 2 files per NPC as opposed to 1. If I have 20 NPCs in the game that's 40 files instead of 20. Isn't it better to keep the number of files low?

FYI, right now my dialog XML looks like this:

<<?xml version="1.0" ?>
<NPCid value = '1' />
<Questions>
<Question id = '[GUID]'>
<Text>Some Text</Text>
<Text>Blah-blah</Text>
<Action Type = 'GoTo' id = '[GUID]' />
<Action Type = 'Set' var = "Val1" value = "10" />
</Question>
</Questions>

##### Share on other sites
Quote:
 Original post by lougv22FYI, right now my dialog XML looks like this:< Some Text Blah-blah
Why all the ID numbers? Since you are using a textual format to start with, might as well use actual names, and save yourself a world of confusion.

##### Share on other sites
Quote:
 Original post by lougv22But in that case I would have 2 files per NPC as opposed to 1. If I have 20 NPCs in the game that's 40 files instead of 20. Isn't it better to keep the number of files low?

Because dialog does not necessarily have to be associated with just a single NPC, because an NPC is not just composed of dialog, and because the number of files you have can be reduced using resource compilation methods to process it down to a much more scrunched up format that is significantly faster to parse and load than XML.

As an example: Imagine you have two NPCs who are both historians, you can speak to each of them and get a history of the world. They both tell the same story, up to a point, but after that point it diverges. Do you: Duplicate that text, which can go through several reversions, several localizations, etc. or do you centralize that common text so that you have one area to go to in order to localize and make revisions.

Also, don't assume dialog will be simply questions and answers. It's probably better to view it as NPC/PC (or PC/NPC) speech turns, allowing for multiple responses from each party.

<Dialog>	<DialogStart>		So I heard you like mudkips...		<Responses id="mudkipsLoop">			<Response>				<PcSay>No</>				<Action>					<NpcSay>Are ya sure?</>					<Goto id="mudkipsLoop"/>				</>			</>			<Response>				<PcSay>Yes</>				<Action>					<RunScript value="npc-slap-pc-with-mudkip.script"/>				</>			</>		</>	</></>

##### Share on other sites
If you can get a hold of it, I'd look at how dialogue trees were implemented in the original Neverwinter Nights for inspiration. In the Aurora toolset, not only can you get more than two people involved in the dialogue, but the actor corresponding to the current node can do a wide variety of actions in addition to saying the text - move, play audio, play animation, give or take something to/from player, run a script, etc.

Another thing is, if localization is on the program, I'd move the dialogue strings out of the dialogue files completely and replace them with unique string references. There are various ways you could organize the strings, from one giant string table (this is how NWN did it for the shipped content) to one string table per dialogue file (so they are in a 1:1 mapping).

##### Share on other sites
I think it would be good to distinguish between definitions and references.

I would do something like that:
<?xml version="1.0"?><MyGameResources version="1.3">    <NPCs>        <NPC id="Bob">            <Question ref="q.what1">                <Answer ref="a.cook1" condition="hasSkill('cooking') && player.isHungry()">                    <Question ref="q.pancakes1">                        <Answer ref="a.pancakes.ok" action="executeSkill('cooking', 'pancakes');" />                        <Answer ref="a.pancakes.no" />                    </Question>                </Answer>                <Answer ref="a.nothing1" />            </Question>        </NPC>    </NPCs>    <Questions>        <Question id="q.what1">            <text lang="en">What can I do for you?</text>        </Question>        <Question id="q.pancakes1">            <text lang="en">Ok ... How about some pancakes?</text>        </Question>    </Questions>    <Answers>        <Answer id="a.cook1">            <text lang="en">Cook me something good!</text>        </Answer>        <Answer id="a.nothing1">            <text lang="en">Nothing ... your're useless.</text>        </Answer>        <Answer id="a.pancakes.ok">            <text lang="en">Sounds great! Thank you.</text>        </Answer>        <Answer id="a.pancakes.no">            <text lang="en">I hate pancakes. Go to hell.</text>        </Answer>    </Answers></MyGameResources>

There you have your dialog tree an you can easily re-use questions or answers.
You can also localize your game:
<MyGameResources version="1.3">    <Answers>        <Answer id="a.cook1">            <text lang="fr">Cuisine-moi quelquechose de bon !</text>        </Answer>    </Answers></MyGameResources>

I put a possible way to include scripts but of course it highly depends on your game.

To answer to your question, I think it would be tedious to maintain many text files that refer to your XML dialog files.
I would probably define which directories hold my game resources, list all XML files inside and load them, taking care of things like current locale (load only the questions and answers that have a lang attribute equal to currently selected locale).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 29
• 16
• 11
• 10
• 11
• ### Forum Statistics

• Total Topics
634112
• Total Posts
3015586
×