Run Code on Conversation Escape

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Kerple
Posts: 9
Joined: Sat Aug 05, 2023 1:14 pm

Run Code on Conversation Escape

Post by Kerple »

I'm using a dialogue system to access a computer UI. On activation of the conversation the computer turns on and displays the UI. However, if the player hits escape the rest of the conversation that resets the computer and turns it off doesn't run.

So I'm looking to run code if the player leaves this particular conversation early by hitting escape.
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Run Code on Conversation Escape

Post by Tony Li »

Hi,

In code, you can hook into DialogueManager.instance.conversationEnded:

Code: Select all

DialogueManager.instance.conversationEnded += OnConversationEnded;
DialogueManager.StartConversation("title");
...
void OnConversationEnded(Transform actor)
{
    DialogueManager.instance.conversationEnded -= OnConversationEnded;
    if (DialogueManager.lastConversationEnded == "title")
    {
        // (do your stuff here)
    }
}
Or add an OnConversationEnd() method to a script that's on the Dialogue Manager or the computer:

Code: Select all

void OnConversationEnd(Transform actor)
{
    if (DialogueManager.lastConversationEnded == "title")
    {
        // (do your stuff here)
    }
}
If you put it on the computer GameObject, it will only be called if the computer GameObject is the conversation actor or conversation conversant. If you put it on the Dialogue Manager, it will be called for every conversation.

Or add a Dialogue System Events component to the computer GameObject and configure the OnConversationEnd() UnityEvent.

If you only want to run code if the player pressed the Dialogue Manager's Input Settings > Cancel Conversation Input, use an OnConversationCancelled() method instead.
Kerple
Posts: 9
Joined: Sat Aug 05, 2023 1:14 pm

Re: Run Code on Conversation Escape

Post by Kerple »

I was able to fix it with your help thank you very much!
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Run Code on Conversation Escape

Post by Tony Li »

Glad to help!
Post Reply