Page 1 of 1

Event That Is Called Every Time The Speaker Changes

Posted: Wed Jan 27, 2021 12:21 am
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.

Re: Event That Is Called Every Time The Speaker Changes

Posted: Wed Jan 27, 2021 8:51 am
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();
        }
    }
}