CodeBlanks Logo Icon CodeBlanks

Dialogue data model

Learn the dialogue node and line data model.

Dialogue Data Model

At the core of a dialogue system lie data models that represent speech lines. Each individual line contains the speaker's name, the spoken text, and optionally an audio clip reference.

[System.Serializable]
public class DialogueLine {
    public string speaker;
    public string text;
}

Adding [System.Serializable] to C# classes enables these models to be viewed in the Inspector and serialized to JSON format.

Dialogue Nodes

A sequential sequence of lines forms a Dialogue Node. A node stores an identifier (id), a list of lines, and the identifier of the subsequent node (nextId).

[System.Serializable]
public class DialogueNode {
    public int id;
    public List<DialogueLine> lines;
    public int nextId;
}

With this node structure, conversations are organized into discrete blocks and sequence flow is maintained.

Practice Dialogue line model

Define a Start method. Inside, declare string speaker = "Rehber"; and print "Konusmaci: " + speaker to the console using Debug.Log.

Starter code

Player.cs
using UnityEngine;

public class DialogueModelTester : MonoBehaviour
{
    // Start icinde speaker degiskenini tanimla ve yazdir
}

Sample solution

Player.cs
using UnityEngine;

public class DialogueModelTester : MonoBehaviour
{
    void Start()
    {
        string speaker = "Rehber";
        Debug.Log("Konusmaci: " + speaker);
    }
}

Expected output

Konusmaci: Rehber

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which two pieces of information are fundamentally required in a dialogue line data model?

  • Physics collision layer and audio mixer
  • GPU clock speed and RAM size
  • Speaker name and dialogue text Correct answer
  • Screen resolution and shader code

Explanation: To determine who is speaking and what text will be rendered, speaker name and text fields are essential.

2 Serializable dialogue line

Question
[System.____] public class DialogueLine { public string text; }
Answer
[System.Serializable] public class DialogueLine { public string text; }

Explanation: In Unity, the '[System.Serializable]' attribute is required to make a data model serializable.

3 Dialogue model terms

Term Meaning
DialogueLine Single line and speaker data
DialogueNode Node holding a group of dialogue lines
speaker Name of the speaking character
nextId Next node identifier to continue flow

All lessons in Advanced