Sunday, April 15, 2018

unity - What is a good way to code dialogue creation?


I'm thinking of making a single method/class/something that will handle my dialogue in the game. AKA, something like:



Main text:


Choice 1
Choice 2


So I want a method that will able to put different strings into the main text section and also different strings into the Choice options. I've been thinking about solutions, and so far I've come up with just a simple method that accepts parameters for main text, choice1 and choice2 -- problem being that the strings called will be quite long and I think it looks a little clumsy? Also I won't want to have 2 choices every time, OR I want to have multiple "pages" of dialogue show up before you need to take an action. This would require me to overload methods which may turn out quite ugly?


So yeah I don't have a lot of experience with this and method overloading is all I've thought about for now. I've given thought to interfaces/abstract classes but I'm not entirely sure if they're even relevant for what I want to do. Any thoughts?



Answer



I suggest to use a tree (graph) class aproach. Place Main text into nodes and choise text into arcs (arrows)


Multi page Text can placed into sequential nodes with only one choice arc ("Next page")


enter image description here


Rough c# implementation:



public class DialogueArc {
public String Text { get; set; }
public DialogueTreeNode outgoingNode;
public DialogueArc(String text, DialogueTreeNode N)
{
Text = text;
outgoingNode = N;
}

}

public class DialogueTreeNode
{
public String Text { get; set; }
public List Choiches = new List();
public DialogueTreeNode(String text)
{
Text = text;
}
public void AddArc(string T, DialogueTreeNode N) {
Choiches.Add(new DialogueArc(T, N));

}
}

Dialogue creation example:


    //define nodes
DialogueTreeNode Root = new DialogueTreeNode("First Page dialogue....");
DialogueTreeNode RootPg2 = new DialogueTreeNode("Second page dialogue....");
DialogueTreeNode RootPg3 = new DialogueTreeNode("third page page...");
DialogueTreeNode afterCh1 = new DialogueTreeNode("After choice 1..");
DialogueTreeNode afterCh2 = new DialogueTreeNode("After choice 2..");

DialogueTreeNode afterCh1Ch1 = new DialogueTreeNode("After choice 1_1..");
DialogueTreeNode afterCh2Ch1 = new DialogueTreeNode("After choice 2_1..");
//define choices
Root.AddArc("Next page", RootPg2);
RootPg2.AddArc("Next page", RootPg3);
RootPg3.AddArc("Choice 1 txt", afterCh1);
RootPg3.AddArc("Choice 2 txt", afterCh2);
afterCh1.AddArc("Chise 1_1 txt", afterCh1Ch1);
afterCh2.AddArc("Chise 2_1 txt", afterCh2Ch1);

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...