Trigger conversationEnded when all branches are ended

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
muttsang
Posts: 6
Joined: Sat Nov 11, 2023 10:26 am

Trigger conversationEnded when all branches are ended

Post by muttsang »

Hello, I am using the

Code: Select all

DialogueManager.instance.conversationEnded
event to detect when a conversation has ended and it has worked greatly so far!

I have one issue where I would like this event to only trigger when all of the branches of a single conversation finish but currently this event is triggered every time a branch of the conversation ends.

For example, in the attached image, I have four different branches in a single conversation. I would like to have the

Code: Select all

DialogueManager.instance.conversationEnded
event only fire when all of the branches have been completed rather than having any one of them fire the event.
DialogueConversationWitlMultipleBranches.png
DialogueConversationWitlMultipleBranches.png (5.96 KiB) Viewed 576 times
Is there a way to do this in the asset? I am currently using version 2.2.41 if that helps in any way.
Thank you!
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger conversationEnded when all branches are ended

Post by Tony Li »

Hi,

Here are ways to approach it:

Use Dialogue System variables (or SimStatus) to keep track of which branches have been played to the end. When the conversation ends, check if all of the variables (or SimStatus values) indicate that the branches have all ended.

You could do this in the Conditions/Script fields of the last node in each branch, or in a C# script. For example, if you're using SimStatus, you could use C# code similar to this:

Code: Select all

DialogueManager.instance.conversationEnded += CheckIfAllBranchesEnded;
...
void CheckIfAllBranchesEnded(Transform other)
{
    var conversation = DialogueManager.masterDatabase.GetConversation(DialogueManager.lastConversationID);
    foreach (var entry in conversation.dialogueEntries)
    {
        // If the entry is the end of a branch, check if it's been shown already.
        bool isLeaf = entry.outgoingLinks.Count = 0;
        var wasDisplayed = DialogueLua.GetSimStatus(entry) == DialogueLua.WasDisplayed;
        if (isLeaf && !wasDisplayed)
        {
            // This branch hasn't ended yet, so exit:
            return;
        }
    }
    Debug.Log("YES - ALL BRANCHES ENDED");
}
muttsang
Posts: 6
Joined: Sat Nov 11, 2023 10:26 am

Re: Trigger conversationEnded when all branches are ended

Post by muttsang »

Hi Tony,

Amazing! That worked perfectly thank you! Your solution is also a LOT cleaner than what I had implemented as a workaround!

I've also added a separate flag in the example code you've provided to introduce an "Optional" branch.

Code: Select all

 Conversation conversation = DialogueManager.masterDatabase.GetConversation(DialogueManager.lastConversationID);

 foreach (DialogueEntry entry in conversation.dialogueEntries)
 {
     // If the entry is the end of a branch, check if it's been shown already.     
     bool isLeaf = entry.outgoingLinks.Count == 0;
     bool wasDisplayed = DialogueLua.GetSimStatus(entry) == DialogueLua.WasDisplayed;
     
     //addition
     bool isOptional = (entry.Title.Equals("Optional") || entry.Title.Equals("optional"));

     //addition and slight adjustment
     if (isLeaf && isOptional)
     {
         continue;
     }
     else if (isLeaf && !wasDisplayed)
     {
         // This branch hasn't ended yet, so exit:
         return;
     }
 }
With this small addition, I can mark the last dialogue in the conversation as "Optional" as the last Dialogue's Title and just able to skip checking that!

Thank you so much Tony you have helped me solve a big part of our project :D !
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger conversationEnded when all branches are ended

Post by Tony Li »

Happy to help!
Post Reply