Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Dialogue Arrays

Discussion in 'Scripting' started by steadmuffin, Sep 5, 2016.

  1. steadmuffin

    steadmuffin

    Joined:
    Jun 1, 2016
    Posts:
    49
    I'm trying to make dialogue in my game. I'm thinking that I will store dialogue in an array in a script called DialogueHolder which would be attached to a player or unit. To display the dialogue, I will send the array of the dialogue to a DialogueManager script. How would I make the array size of the DialogueManager match the DialogueHolder?
     
  2. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    You could make a function in DialogueManager.
    Code (CSharp):
    1. public void RecieveDialogue (string[] dialog){
    2. //Renderer
    3. }
    Then just send the array by calling the function within the player or unit script.
    Code (CSharp):
    1. public DialogueManager send;
    2. string[] dialog = new string[]{"this is", "an example"};
    3.  
    4.  
    5. //whatever you want to trigger this
    6. send.RecieveDialogue(dialog);
    Finally, you just go into the Unity editor and drag the DialogueManager Script into the open spot on whatever player or unit script you want.
     
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    A other option is making your dialog objects as scriptable objects that live in the project. Each one can hold reference to audio, you facial animation and to subtitle text.

    These can be dragged into your character and triggered when needed
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    use lists instead?

    might want to google "unity node dialogue system", its a far more robust design and it allows for branching dialogue based on interaction from the player (if that's something you're interested in, "dialogue system" is a little vague on the details :p ).
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    First consider how you're going to write your dialogue. Overall, that will be more time-consuming than the programming part. It will also have a big impact on your data structures.

    For example, say you write your dialogue in a spreadsheet program such as MS Excel or Google Sheets, like this:

    Code (spreadsheet):
    1. |ID | Speaker | Text                                     | Links to...
    2. |---+---------+------------------------------------------+------------
    3. |0  | NPC     | What's heavier: an elephant or a canary? | 1 | 2
    4. |1  | PC      | An elephant.                             | 3
    5. |2  | PC      | A canary.                                | 4
    6. |3  | NPC     | Correct!                                 |
    7. |4  | NPC     | Wrong! Try again.                        | 0

    A spreadsheet is a fairly common old-school technique, but it certainly isn't the only way you can write dialogue. The Dialogue System for Unity, for instance, comes with node-based and outline-based editors, and can also import spreadsheets, Chat Mapper, articy:draft, and Neverwinter Nights because different authors have different workflow preferences.

    Anyway, getting back to the spreadsheet, you can then save it as a CSV (comma-separated values) file:

    0,NPC,"What's heavier: an elephant or a canary?",1,2
    1,PC,"An elephant.",3
    2,PC,"A canary.",4
    3,NPC,"Correct!",
    4,NPC,"Wrong! Try again.",0

    and read it into Unity. Here's a rough example:

    Code (csharp):
    1. public class Node {
    2.     public bool isNPC;
    3.     public string text;
    4.     public List<int> links;
    5.  
    6.     public Node(bool isNPC, string text, List<int> links) {
    7.         this.isNPC = isNPC;
    8.         this.text = text;
    9.         this.links = links;
    10. }
    11.  
    12. public class Conversation {
    13.     public TextAsset csvFile; // Assign your CSV file here.
    14.     public List<Node> nodes = new List<Node>();
    15.  
    16.     void Start() { // Convert the CSV file into a list of nodes.
    17.         // Split the CSV text into lines:
    18.         var lines = Regex.Split(csvFile.text, "\n|\r|\r\n");
    19.         foreach (var line in lines) {
    20.             // Split the line into columns:
    21.             var columns = line.Split(',');
    22.             // Add the line as a new node:
    23.             var isNPC = string.Equals(columns[1], "NPC");
    24.             var text = columns[2];
    25.             var links = new List<int>();
    26.             for (int i = 3; i < columns.Length; i++) {
    27.                 links.Add(columns[i]);
    28.             }
    29.             nodes.Add(new Node(isNPC, text, links);
    30.         }
    31.     }
    32. }
    To run a conversation, you start at nodes[0] and follow each node's links[] list to other nodes.

    You can probably see how the choice to use a spreadsheet strongly influenced the code design.

    If you were to write each dialogue node as a separate MonoBehaviour so you could add and edit them in the Unity editor's Inspector, the code would be fairly different:

    Code (csharp):
    1. public class DialogueNode : MonoBehaviour {
    2.     public bool isNPC;
    3.     public string text;
    4.     public DialogueNode[] links;
    5. }