Event That Is Called Every Time The Speaker Changes

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
junwi21
Posts: 35
Joined: Wed Jan 27, 2021 12:18 am

Event That Is Called Every Time The Speaker Changes

Post by junwi21 »

I want to run some code every time the speaker in a conversation changes. Where / how do I do this?
I want this to apply to all conversations throughout the game.

Also, I need who is speaking (gameObject or name text) as an argument.
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Event That Is Called Every Time The Speaker Changes

Post by Tony Li »

Hi,

Add a script with an OnConversationLine method to the Dialogue Manager. Example:

Code: Select all

using UnityEngine;
using UnityEngine.Events;
using PixelCrushers.DialogueSystem;

public class EventEveryTimeSpeakerChanges : MonoBehaviour
{
    public UnityEvent onSpeakerChange;
    
    private Transform lastSpeaker;
    
    void OnConversationStart(Transform actor) { lastSpeaker = null; }
    
    void OnConversationLine(Subtitle subtitle)
    {
        if (!string.IsNullOrEmpty(subtitle.formattedText.text) && subtitle.speakerInfo.transform != lastSpeaker)
        {
            lastSpeaker = subtitle.speakerInfo.transform;
            onSpeakerChange.Invoke();
        }
    }
}
Post Reply