Help coding dialog system?

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