Announcements, support questions, and discussion for the Dialogue System.
HunterTom
Posts: 18 Joined: Fri Dec 24, 2021 4:58 pm
Post
by HunterTom » Mon Feb 14, 2022 1:41 pm
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
Tony Li
Posts: 21981 Joined: Thu Jul 18, 2013 1:27 pm
Post
by Tony Li » Mon Feb 14, 2022 3:09 pm
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
Post
by HunterTom » Mon Feb 14, 2022 3:55 pm
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
Tony Li
Posts: 21981 Joined: Thu Jul 18, 2013 1:27 pm
Post
by Tony Li » Mon Feb 14, 2022 4:09 pm
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.