Page 1 of 1

Retrieve Conversation by Name and Iterate over convo nodes

Posted: Wed Jul 13, 2016 2:48 am
by sazr
Hello, I am attempting to iterate over every node in a conversation and check that we have the correct audio file assigned to it. I'm hoping to run this script in the inspector rather than during the game. It it possible to iterate over a conversation and inspect/change the nodes when not running the game? If it is can you point me in the right direction by suggesting what functions I can use to:

- Find a conversation by name. Eg "Chapter 2"
- Iterate over every node in a conversation and retrieve the node 'Dialogue Text' and 'Sequence' text/object?

Heres my current code:

Code: Select all

using UnityEngine;
using UnityEditor;
using System.Collections;
using PixelCrushers.DialogueSystem;

[ExecuteInEditMode]
public class ValidateConversationNodes : MonoBehaviour {

    public string ConversationTranscript = "abc.txt";
    public string ConversationName = "Conversation 2";
    public bool ValidateConversation = false;

    public void Update()
    {
        if (ValidateConversation)
            PerformConversationValidation();
    }

    private void PerformConversationValidation()
    {
        // Find conversation by name
        // For each node in conversation:
        //      - Retrieve the nodes' sequence and dialogue text
        //      - Confirm the node calls the correct audio file (check sequence)

        ValidateConversation = false;
        Debug.Log("Successfully validated\n");
    }
}

Re: Retrieve Conversation by Name and Iterate over convo nodes

Posted: Wed Jul 13, 2016 1:15 pm
by Tony Li
Hi,

You first need a reference to the DialogueDatabase that contains the conversation. The code below adds a public variable for a DialogueDatabase:

Code: Select all

public class ValidateConversationNodes : MonoBehaviour {

    public DialogueDatabase database; //<--ADD THIS.
    public string ConversationTranscript = "abc.txt";
    ...etc...
Working with the database is quite easy. In brief:
Here's some basic code:

Code: Select all

private void PerformConversationValidation() {
    // Make sure we have a reference to a database:
    if (database == null) database = EditorTools.FindInitialDatabase();
    if (database == null) {
        Debug.LogError("No dialogue database!");
        return;
    }
    
    // Find conversation by name:
    var conversation = database.GetConversation(ConversationName);
    if (conversation == null) {
        Debug.LogError("Conversation " + ConversationName + " not found in database!");
        return;
    }
    
     // For each node in conversation:
     //      - Retrieve the nodes' sequence and dialogue text
     //      - Confirm the node calls the correct audio file (check sequence)
     foreach (var dialogueEntry in conversation.dialogueEntries) {
         var dialogueText = dialogueEntry.DialogueText;
         var sequence = dialogueEntry.Sequence;
         
         // If you modify the sequence:
         dialogueEntry.Sequence = your_modified_sequence;
     }

    ValidateConversation = false;
    Debug.Log("Successfully validated\n");
}
A few notes:

1. The DialogueEntry class has helper properties for DefaultText, Sequence, and other common fields. If you're using language localization, please be aware that many of these properties return the localized field for the current language. If you want the default dialogue text (not the localized version), use the DefaultDialogueText property instead. (See the docs for more info.)

2. If you've already written your content, it might not be worthwhile to look into entrytags. However, they can make this kind work easier. Here are the docs: How to Use entrytags.

3. The code above calls EditorTools.FindInitialDatabase(), which is a Dialogue System editor class. I'm assuming that your code will be marked editor only so it won't be included in builds, since EditorTools won't compile in builds. If you want to run it in builds, you can remove that line.

Re: Retrieve Conversation by Name and Iterate over convo nodes

Posted: Thu Jul 14, 2016 4:18 am
by sazr
Thanks! Works out great

Re: Retrieve Conversation by Name and Iterate over convo nodes

Posted: Thu Jul 14, 2016 8:37 am
by Tony Li
Great! Glad to help.