Calling a custom unity function after the end of a dialogue

Announcements, support questions, and discussion for the Dialogue System.
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Calling a custom unity function after the end of a dialogue

Post by simplepleasuresdxy »

Hi there, very new to Unity and the Dialogue system

I was hoping to inquire about how to call a function after the end of a scene. I assume it revolves around the events system, but I'm having trouble finding help about it though even if I did, I think I'd still have trouble.

I created a custom function called "GameController.Instance.StopDialogue();" where by the function activates things in the scene. To be as crystal clear as possible, this function does NOT stop the dialogue. It's only a sort of alert to some other functions that tells them that it's time to activate.

What would I need to do to have the dialogue activate that function "GameController.Instance.StopDialogue();" at the end of a dialogue? Additionally, is there a way to activate the opposite function at the beginning of a dialogue?

Thanks!
User avatar
Tony Li
Posts: 22147
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling a custom unity function after the end of a dialogue

Post by Tony Li »

Hi,

The Dialogue System has many events you can hook into.

You can add a (second, third, etc.) Dialogue System Trigger component to a conversation participant and set its Trigger dropdown to OnConversationStart or OnConversationEnd. It will run its Actions when a conversation starts or ends.

If you want your code to know when a conversation starts or ends, you can add a script to the Dialogue Manager or a conversation participant that has OnConversationStart(Transform) or OnConversationEnd(Transform) methods, or hooks into the C# events DialogueManager.instance.conversationStarted and conversationEnded. (See Script Messages)

However, in this case, you might just want to use the Sequence or Script fields in your conversation's dialogue entries. In the first node (i.e., the one linked from <START>), use a sequencer command in the Sequence field or a Lua function in the Script field. More info: Sequencer Commands, C# Methods to Lua Functions. Another alternative is to use Scene Events.

To directly answer your question, I recommend using the C# events. In your GameController script, do something like:

Code: Select all

using PixelCrushers.DialogueSystem;
...
void OnEnable()
{
    DialogueManager.instance.conversationStarted += OnConversationStarted;
    DialogueManager.instance.conversationEnded += OnConversationEnded;
}

void OnDiable()
{
    DialogueManager.instance.conversationStarted -= OnConversationStarted;
    DialogueManager.instance.conversationEnded -= OnConversationEnded;
}

void OnConversationStarted(Transform actor) { StartDialogue(); }
void OnConversationEnded(Transform actor) { StopDialogue(); }
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Calling a custom unity function after the end of a dialogue

Post by simplepleasuresdxy »

Hi Tony ... uh, I have no idea what any of that means, lul.

What's a dialogue system trigger component? What's a conversation participant? I'm so confused. I just want to make a simple visual novel thing. no fancy whatever. You don't even see yourself in game. I don't know where to start even more now, lul
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Calling a custom unity function after the end of a dialogue

Post by simplepleasuresdxy »

I gave the Dialogue Manager the "Dialogue Systems Events" and put the scripts in the on Conversation Start and End but they're not doing anything.
User avatar
Tony Li
Posts: 22147
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling a custom unity function after the end of a dialogue

Post by Tony Li »

Hi,

Assuming your GameController script is on a GameObject, assign the GameController GameObject to the Dialogue System Events component. Then you should be able to select GameController > StartDialogue and GameController > StopDialogue from the dropdowns.

dialogueSystemEvents.png
dialogueSystemEvents.png (72.1 KiB) Viewed 676 times

p.s. - Sorry about the crazy information overload in my previous reply! :-)
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Calling a custom unity function after the end of a dialogue

Post by simplepleasuresdxy »

Tony Li wrote: Tue Jul 25, 2023 8:51 pm Hi,

Assuming your GameController script is on a GameObject, assign the GameController GameObject to the Dialogue System Events component. Then you should be able to select GameController > StartDialogue and GameController > StopDialogue from the dropdowns.


dialogueSystemEvents.png


p.s. - Sorry about the crazy information overload in my previous reply! :-)

So ... uh, I also don't know what that means. I don't know what it means to hav ea game controller script on a game object or why that would work or how you're even getting "GameController.StartDialogue"

My scripts are really REALLY dumb.
Image
Image
Image

GameController.Instance.PlayDialogue and stop dialogue do something. I don't want to go into details because I don't even understand it, tbh. All I know is that they work when I put it on this script

Image
Image

When I click the check mark, the "PlayDialogue" thing does what it needs to do on the start of the scene.

How do I get this to work?
User avatar
Tony Li
Posts: 22147
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling a custom unity function after the end of a dialogue

Post by Tony Li »

Hi,

Let's get it working first, and then we can go over why it works.

1. Please remove the Dialogue System Events component from your Dialogue Manager GameObject.

2. Right-click in the Project window, and select Create > C# Script. This will create a new script that you can name. Name it "MyDialogueEvents".

3. Then open the script in your code editor. You should be able to double-click on the script file to open it.

4. In your code editor, select everything in the script file and delete it. Replace it with this:

Code: Select all

using UnityEngine;

public class MyDialogueEvents : MonoBehaviour
{
    void OnConversationStart(Transform actor)
    {
        GameController.Instance.PlayDialogue();
    }

    void OnConversationEnd(Transform actor)
    {
        GameController.Instance.StopDialogue();
    }
}
5. Back in the Unity editor, drag this script file onto the Dialogue Manager GameObject in the Hierarchy. Your Dialogue Manager GameObject will probably look something like this: (I collapsed some of the components to make the screenshot smaller.)
myDialogueEvents.png
myDialogueEvents.png (22.87 KiB) Viewed 670 times

6. Give it a try! :-)
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Calling a custom unity function after the end of a dialogue

Post by simplepleasuresdxy »

Oh dear lord that worked for both conversation start and end.

Hot damn, okay.
User avatar
Tony Li
Posts: 22147
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling a custom unity function after the end of a dialogue

Post by Tony Li »

Great! You don't need those other scripts in this case. (You still need the GameController script, so don't delete it!)

The Dialogue System provides several ways to be notified when conversations start and end.

One way to be notified is through the Dialogue System Events component, which we tried earlier. But it would have required some changes to your existing scripts.

Another way to be notified is writing a script that has OnConversationStart() and OnConversationEnd() methods. That's what we just did.
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Calling a custom unity function after the end of a dialogue

Post by simplepleasuresdxy »

Understood. Thank you so much.
Post Reply