Tutorial 05: Basic dialogs

What you learn in this tutorial

  1. Add new characters
  2. Create scripted dialogue
  3. Create new GUI Hudtexts
  4. Run scriptfiles

Introduction

This tutorial and you have downloaded Starting Tutorial File and extracted it to folder, which we refer with (Dage)\

It is recommended you have already took the previous tutorials.

Preparation of the scene

What we want to do, is to create a second character, and we want to be able to talk with him.

This is the dialogue tree we want to create:

First we want to show three options, which two of leads to different conversations, and third will end the discussion.

Let's begin!

Creating second character

We don't use time to model another character, so we reuse the bobby model.

Add this code after mainactor creation in onGameLoad.txt

Robby = TActor.Create();
Robby.LoadBaseMesh('MainActor/Bobby.smd');
Robby.SetMeshScale(0.1,0.1,0.1);
Robby.SetActor_Dummy_Position(0,-5,0);
Robby.SetCollisionScale(3,5,5);
Robby.MouseControl = 0;
Robby.Y = 10;
Robby.AddAnimation('MainActor/standing.smd');
Robby.SetPosition(-5,6,7);
Robby.SetRotation(-51,0,0);
Robby.Visible = 0;

We also want that the Robby will be displayed only in Room 02, so roomscripts.txt in onRoom02Load add

 Robby.Visible = 1;

And respectively onRoom01Load add

 Robby.Visible = 0;

We are done creating second character.

Adding way to start conversation

Now we want to be able to identify and click Robby to start a conversation.

Just add

Robby.onMouseMove = 'RobbyMouseMove';
Robby.onMouseUp = 'RobbyMouseUp';

to onGameLoad where we create actor, and

function RobbyMouseMove()
 MouseText.Text = 'Robby';
end
function RobbyMouseUp()
 MainActor.Say('Hello');
end

to roomscript somewhere.

Okay, so now we can start a conversation. Next we need GUI for conversation.

Adding new GUI elements

We create three new THudText objects, which are used to display options, use can click.

  Option01 = THudtext.Create();
  Option01.Text = 'This is option 01';  
  Option01.SetPosition(400,400,0);
 
  Option02 = THudtext.Create();
  Option02.Text = 'This is option 02';  
  Option02.SetPosition(400,430,0);
 
  Option03 = THudtext.Create();
  Option03.Text = 'This is option 03';  
  Option03.SetPosition(400,460,0);

Setting texts only for to see what the hudtexts look in the game.

We are creating 3 options, but you can create as many as you like.

After we have been satisfied that the settings looks good, we change the creation code to this:

  Option01 = THudtext.Create();
  Option01.Visible = 0;
  Option01.SetPosition(400,400,0);
 
  Option02 = THudtext.Create();
  Option02.Visible = 0;
  Option02.SetPosition(400,430,0);
 
  Option03 = THudtext.Create();
  Option03.Visible = 0;
  Option03.SetPosition(400,460,0);

We don't set Mouse events yet, because TConversation object overrides them.

Basics of TConversation object

First, let's create new scriptfile called conversations.txt

And let's create new function and TConversation object.

function InitDialog()
 if Dialog == nil then -- here we check that we haven't created Dialog before
  Dialog = TConversation.Create(); --here we create dialog
  Dialog.BindHudtext(Option01.ID); -- We bind the hudtexts, we created earlier
  Dialog.BindHudtext(Option02.ID);
  Dialog.BindHudtext(Option03.ID);
 end
 
-- Here we add more soon. [code01]
end

There are two things in TConversation object that allow us to create dialogs. Nodes and Options.

Node is a set of options.

So if we look the picture of our dialogue tree,

“Who are you?” is an option “Where am I?” is an option “Bye” is an option

And these three options create one node.

Each option has attached a function, which says what we want to do, when player click the option.

First, let's make sure there are not any nodes (or options)

by adding inside InitDialog() function

 Dialog.ClearNodes();

Next we want to create new node and add our options to it:

StartNode = Dialog.AddNode();
 
Dialog.AddOption(StartNode,"Who are you?",'WhoAreYou');
Dialog.AddOption(StartNode,"Where am I?",'WhereAmI');
Dialog.AddOption(StartNode,"Bye",'ExitDialog');
 
Dialog.ShowNode(StartNode); -- We want to display this

StartNode is only an integer, we could have write AddOption(0,”Who are you?”,'WhoAreYou') if we would wanted. Next node is 1 and so on.

Now we need to define what happens when use clicks those options (note that BindHudtext overwrites onMouseUp event)

-- We use this function to show the node we want, so after our dialogue is done, we want to return back to the options
function ShowStartNode()
 Dialog.ShowNode(StartNode);
end
 
function WhoAreYou()
  Dialog.HideNodes(); -- Hide options
  MainActor.Say("Who are you?");
  Robby.Say("I'm Robby, and you are?");
  MainActor.Say("I'm Bobby");
  Robby.Say("Nice to meet you");
  MainActor.Say("Likewise",0,'ShowStartNode'); -- After mainactor has said this, we call ShowStartNode function
end

Now the rest functions, they work similarly.

function WhereAmI()
  Dialog.HideNodes();
  MainActor.Say("Where am I?");
  Robby.Say("This is Room02 in Tutorial for");
  Robby.Say("Creating Dage Adventures");
  MainActor.Say("Wow",0,'ShowStartNode');
end
 
function ExitDialog()
  Dialog.HideNodes();
end

Starting the dialogue

Let's go back to roomscripts.txt where we click the Robby, and change the function to start the conversation

function RobbyMouseUp()
     Game.LoadScript('scripts/conversations.txt'); -- First we load up the scriptfile where all the functions are.
     InitDialog(); -- Then we start the dialog
end

Let's test the dialogue

Woah, it almost works. When we click the options, the actor walks around.

We can change this by adding in InitDialog function

Game.DisableScene = 1; -- that disables all but hudobjects

and adding in ExitDialog function

Game.DisableScene = 0;

Next you could try to add new Node with new three options, which one of leads back to StartNode.

Appendix

here is conversations.txt full code

function InitDialog()
 if Dialog == nil then
  Dialog = TConversation.Create();
  Dialog.BindHudtext(Option01.ID);
  Dialog.BindHudtext(Option02.ID);
  Dialog.BindHudtext(Option03.ID);
 end
 
 Dialog.ClearNodes();
 
 StartNode = Dialog.AddNode();
 
 Dialog.AddOption(StartNode,"Who are you?",'WhoAreYou');
 Dialog.AddOption(StartNode,"Where am I?",'WhereAmI');
 Dialog.AddOption(StartNode,"Bye",'ExitDialog');
 
 Dialog.ShowNode(StartNode);
 Game.DisableScene = 1;
end
 
function ShowStartNode()
 Dialog.ShowNode(StartNode);
end
 
function WhoAreYou()
  Dialog.HideNodes();
  MainActor.Say("Who are you?");
  Robby.Say("I'm Robby, and you are?");
  MainActor.Say("I'm Bobby");
  Robby.Say("Nice to meet you");
  MainActor.Say("Lisewise",0,'ShowStartNode');
end
 
function WhereAmI()
  Dialog.HideNodes();
  MainActor.Say("Where am I?");
  Robby.Say("This is Room02 in Tutorial for");
  Robby.Say("Creating Dage Adventures?");
  MainActor.Say("Wow",0,'ShowStartNode');
end
 
function ExitDialog()
  Dialog.HideNodes();
  Game.DisableScene = 0;
end
Back to top
tutorial05/start.txt · Last modified: 2009/07/31 18:23 by rahakasvi