## Tutorial 4: Chat With NPC

In this tutorial we will talk about NPC conversation. In many games you have an option to
speak with other characters. It usually looks like several options player can chose to say,
and NPC will respond to each accordingly.
To make things interesting, this tutorial is moved to one such dialog. If you start game example 04,
you can click on the beast model to talk to it. The beast will explain everything you want to know
about dialogs. You can then return here to try to make your own dialog.
To make your own dialogs on your maps start Level Editor.
You should be comfortable with laying objects around by now, so layout a simple map.
Add one monster model from the Monsters tab and set it to be NPC in the properties panel.
Click "Add conversation" button (make sure your NPC is selected).
In the popup window, you can add dialog options and dialog actions. When you create new dialog,
there is only one node existing - RootNode. It should not be deleted or changed in any way.
It is the parent of all nodes. So you select the RootNode and click Add Node button. New dialog pops
up where you can enter text: "hi roger". This is what NPC says first when you click on it.
When you create other nodes that are children of this one, they will be players first dialog options.
NPC can have many entry lines, but only one should be enabled at the time. Others can be enabled later.
Lets add several nodes of player text:
- "Hello there can I ask you something?
- "Give me all you got!"
- "Nothing, good bye."
These will be players first dialog options. Next we add what NPC says to that. Select first player
option and add node after it, NPC could say: "Shoot." On the second line ("Give me all you got")
NPC could respond: "Here you are, all I have is this mushroom". After adding NPC responses you should
always add Player options after them. Dialog branch should not end with NPC node cause it will not be
displayed. So you leave third player option as is, this will automaticaly cause end of the dialog.
You can remove nodes using button "Remove Dialog Node".
You can enable/disable nodes (if node is disabled it will disable all the children nodes as well).
You can link nodes so you conversation can return to the begining wihout exiting.
To link two nodes, you first select one node, then click the button, then select the second node.
You always link Players node to NPC node, so when user clicks on that players text, NPC reply follows.
If you disable dialog node, it will not appear in conversation. To make node appear in game, you use
action "SetDialogNodeEnabled". You can put this action in pickable and trigger items, or in the dialog
itself. For example, player could say: "Do you have anything beside the bloody mushroom?" But this node
should appear only after NPC said: "Here you are, all I have is this mushroom".
To add actions to dialog nodes, you first select the node you want action to be attached to.
Then you select action from drop down combo box, for example "SetDialogNodeEnabled" action. This
action needs target value set to id of the dialog node you want to enable/disable (select id from
drop down box, and type id number in edit box). Value field should contain text "true" or "false".
Attribute field must contain the name of the dialog file (in the case of beast beast_01.dlg).
Thats right, you can enable/disable dialog nodes of other NPC dialogs, thus easilly making posible
NPC saying things like "I heard you spoke with Bidalf already... I have better proposal for you."
Finally click "Add Action" button and action text should appear in the box on the left.
You can add more actions to one dialog node.
To make NPC and PC exchange items, you use action "GiveItemToPC", you can use this action to give
mushroom item to player once NPc says she is giving it. Value field contains the name of the item.
The item should first be added to NPC inventory, but that is in another tutorial...
Conditions will be addressed later.
Click ok on the dialog window to save your dialog and then save your map.
In the bin directory, open game_config.xml for edit and change 'mapfile' attribute.
Instead of existing path "maps/temp/default_map.irr" type in your map file and path to it,
for example: "maps/my_maps_folder/dialog_map.irr".
Start Game_04.exe from bin directory.
You should see your map loaded.
Walk to the character you want to talk to and click on it.
Your dialog should popup. Try out the actions you added.
Warning: Dialogs and actions should be added only after map is fully finished. If you change the map
later (delete models, add new ones, etc) actions might end up pointing to wrong objects and things
might not work as they should, so keep that in mind when you create your game.
Petar Bajic
MPE (C) All Rights Reserved
Homepage: www.mystic-peanut.com
July, 21 2008.