Help coding dialog system?

I’m trying to make a short, visual novel-like game for my final project, but I’m stumped when it comes to coding up the dialog system. Here’s a visual example of how the game’s supposed to work:

Any advice would be really appreciated!

Take a look at Ink (ink - inkle's narrative scripting language). It’s free, royalty-free and they have some good unity examples in their unity plugin.

There are other dialogue engines out there, too. I recommend ink, however.

Thing is, we’re not allowed to use add-ons or plugins. Have to make the code ourselves.

I coded a dialogue system for a project last year. The project is dead now, so I don’t mind sharing it. The problem is I couldn’t visit the link you shared (it claims I don’t have permission to do so), so I can’t really see what you need. The dialogue system I coded consisted of allowing to provide NPC’s with dialogues, and display those dialogues on interaction with the NPC. The player could not say anything (like in RPG games), just read what NPC’s had to say. Just let me know if you want to see how I did it, and I’ll share the codes with you, perhaps they’ll give you some ideas.

Here’s the file. It’s more like a visual novel where the dialogue just starts right off the bat.

3850114--325639--Capture.PNG

Yeah, I’m afraid the code I wrote last year is way too complex and broad for this, which is too specific.

From the chart you showed me I gather there is no movement nor NPC’s, just text being displayed and choices to make. Because of that you won’t need anything beyond a canvas. Just display the text on a panel, and under it place three buttons, with the three possible choices (the good one, the bad one, the neutral one). Those three buttons should be child objects of a parent object, named (for example) “Buttons”. In this parent object, place a script that controls the morality of the choices; for instance, “MoralController” (or “God”, if you’re feeling goofy). This script should have a Morality value (integer), which is equal to 0. If the good choice is made, add 1 to Morality, so that it will equal 1. If bad choice, substract one to morality, so that it will equal -1. If neutral choice, do nothing and Morality will still be equal to 0.

Display the good, bad or neutral ending checking the Morality value; if Morality is -1, then bad ending. If Morality is 0, neutral ending. If it’s 1, then good ending.

This is the first way of doing it that comes to my mind, I’m sure there are many others (and probably better ones).

I’ll suggest two ways. The first is a “proper” approach:

Proper Approach
There are three major parts to dialogue:

  • Specifying the dialogue tree content.
  • Running the logic (knowing which branch to follow, and tracking values like morality).
  • Show the text and player options.

If you split up the tasks into these three parts, it makes each part easier to code.

Specifying the dialogue tree content
While you could write a custom editor (e.g., like Simple node editor ), it’s overkill for your project. Set up a simple text file format like:

1 text This is the intro. [2]
2 text This is more story. [3,4,5]
3 option Bad option. {+1 BAD} [6]
4 option Neutral option. {+1 NEUTRAL} [6]
5 option Good option. {+1 GOOD} [6]
6 text This is more text after the options.

This is just an example. The actual format is up to you. In the example above:

  • Line 1’s [2] means line 1 links to line 2.
  • Line 2’s [3,4,5] means linke 2 links to options 3, 4, and 5. etc.

Running the logic
Read the text file. In a line like this:

1 text This is the intro. [2]

You’ll need to parse it into:

  • “1”: The line identifier.
  • “text”: Is this text or a player option?
  • “This is the intro.”: The text/option to show.
  • “[2]”: Where it links to.

I’m also assuming that you’ll parse substrings like {+1 BAD} to add +1 to BAD if the player chooses this option.

Showing the text and player options
Set up a UI with a Text element for the text, and some number of Buttons for player options. When the logic gets to each “node” of your dialogue, set the Text element to the node’s text, and set the Buttons’ text to the follow-up options. Configure each Button’s OnClick() event to call back to the logic part with a button number so the logic knows which button was clicked.

This approach separates data (your dialogue) from code, which is the proper way to do things.

In a second reply, I’ll describe a hacky approach. It’s quicker to implement, but it could become a monster to maintain.

2 Likes

Hacky Approach
Make each dialogue tree node a component, something roughly like this:

public class DialogueNode : MonoBehaviour
{
    public string text;
    public DialogueOption[] options;

    void OnGUI() // Using legacy GUI just to show concept.
    {
        GUILayout.Label(text); // Show text.
        foreach (var option in options) // Show all of the options.
        {
            if (GUILayout.Button(option.optionText))
            { // If player chooses this option, disable this node and enable the destination node.
                this.enabled = false;
                destination.enabled = true;
                if (!string.IsNullOrEmpty(option.variableToIncrement)) { /* increment variable */ }
            }
        }
    }
}

[Serializable]
public class DialogueOption
{
    public string optionText;
    public string variableToIncrement;  
    public DialogueNode destination; // Node to enable when player chooses this option.
}

I glossed over some thing, but that should get the idea across.

Add a bunch of DialogueNode components to a GameObject. Deactivate all of them except the starting node. Hook up nodes using the options’ Destination fields.

I’ll repeat that this isn’t the most robust way to do it, but it’s a quick way to hack it up.

3 Likes

Tony I think your first post is gonna make him wanna cry, he sounds like a student just getting into this. lol

Gotta start somewhere. ¯_(ツ)_/¯

Brackey’s will get you started on an at least basic dialogue system.