Using DialogueSystemEvents

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
HunterTom
Posts: 18
Joined: Fri Dec 24, 2021 4:58 pm

Using DialogueSystemEvents

Post by HunterTom »

Hi,

I want to some event that can be called whenever a new conversation node shows up. Is onConversationLine the right event?

How can I subscribe my own C# function to that event using code? Can I do it in a class that doesn't inherit from Monobehavior?

Here is my attempt

Code: Select all

public static event DialogueSystemEvents.ConversationEvents.onConversationLine testEvent =
        new DialogueSystemEvents.SubtitleEvent();
However, the "onConversationLine " doesn't seem to be accessible from DialogueSystemEvents.ConversationEvents
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using DialogueSystemEvents

Post by Tony Li »

Hi,

DialogueSystemEvents is a MonoBehaviour, so it must be on a GameObject. Assuming it's on the Dialogue Maanger GameObject, you could do something like this:

Code: Select all

void RegisterForEvents()
{
    var events = DialogueManager.instance.GetComponent<DialogueSystemEvents>();
    events.conversationEvents.onConversationLine.AddListener(HandleNewSubtitle);
}

void UnregisterFromEvents()
{
    var events = DialogueManager.instance.GetComponent<DialogueSystemEvents>();
    events.conversationEvents.onConversationLine.RemoveListener(HandleNewSubtitle);
}

void HandleNewSubtitle(Subtitle subtitle)
{
    Debug.Log("New subtitle: " + subtitle.formattedText.text);
}
Call RegisterForEvents() to register for the event. Remember to UnregisterFromEvents() when you're done.
HunterTom
Posts: 18
Joined: Fri Dec 24, 2021 4:58 pm

Re: Using DialogueSystemEvents

Post by HunterTom »

Thank you!

By the way is it safe to call

Code: Select all

dialogueSystemEvents.conversationEvents.onConversationLineEnd.RemoveAllListeners();
? I am just a bit concerned if Dialogue System for Unity has some built-in listerners and if I call that it would destroy the whole system
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using DialogueSystemEvents

Post by Tony Li »

To be safe, I'd remove specific listeners. The core Dialogue System asset doesn't add listeners to DialogueSystemEvents, but it's possible that some addons or utility scripts here in the forum or on the Extras page might do so.
Post Reply