Retrieve Conversation by Name and Iterate over convo nodes

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
sazr
Posts: 5
Joined: Thu Jun 02, 2016 9:29 pm

Retrieve Conversation by Name and Iterate over convo nodes

Post 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");
    }
}
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Retrieve Conversation by Name and Iterate over convo nodes

Post 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.
sazr
Posts: 5
Joined: Thu Jun 02, 2016 9:29 pm

Re: Retrieve Conversation by Name and Iterate over convo nodes

Post by sazr »

Thanks! Works out great
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Retrieve Conversation by Name and Iterate over convo nodes

Post by Tony Li »

Great! Glad to help.
Post Reply