Getting speaker and listener game objects. Is this good?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Getting speaker and listener game objects. Is this good?

Post by Abelius »

Hi there,

I needed to get the game object associated with the current speaker and/or listener in an active convo, and after some research, including one of my old posts (deja vu feeling here), I've figured out a solution to use from a PlayMaker CallMethod action...

Here's the code I've used:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PixelCrushers.DialogueSystem.SequencerCommands
{
    public class GetCurrentSpeakerListener : SequencerCommand
    {
        public GameObject CurrentSpeaker()
        {
            if (!DialogueManager.isConversationActive) return null;
            Transform subject = GetSubject("speaker");
            return subject.gameObject;
        }

        public GameObject CurrentListener()
        {
            if (!DialogueManager.isConversationActive) return null;
            Transform subject = GetSubject("listener");
            return subject.gameObject;
        }
    }
}
It's working and that's great, but I'd still like a sanity check from you, Tony.

Thanks.
Unity 2019.4.9f1
Dialogue System 2.2.15
User avatar
Tony Li
Posts: 22149
Joined: Thu Jul 18, 2013 1:27 pm

Re: Getting speaker and listener game objects. Is this good?

Post by Tony Li »

Hi,

In a sequencer command script, you can just use the properties speaker and listener. They'll already be defined for you.
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Re: Getting speaker and listener game objects. Is this good?

Post by Abelius »

I'm not sure what you mean.

What I need is to get the GO of the speaker and the listener inside a PlayMaker FSM, not running a sequencer command.

Do you mean those are already defined in another place? Can I get them running a LUA command from PlayMaker or something like that?

Thanks.

Ps.: BTW, the above script has mysteriously stopped working today. I don't have a clue why.
Unity 2019.4.9f1
Dialogue System 2.2.15
User avatar
Tony Li
Posts: 22149
Joined: Thu Jul 18, 2013 1:27 pm

Re: Getting speaker and listener game objects. Is this good?

Post by Tony Li »

Hi,

Did you paste in the code that you intended to show? It looks like it's an implementation of the SequencerCommand command. Maybe you meant:

Code: Select all

public class GetCurrentSpeakerListener : MonoBehaviour
In the general case, you can get the current speaker and listener's GameObjects like this:

Code: Select all

public GameObject CurrentSpeaker()
{
    if (!DialogueManager.isConversationActive) return null;
    return DialogueManager.currentConversationState.subtitle.speakerInfo.transform.gameObject;
}

public GameObject CurrentListener()
{
    if (!DialogueManager.isConversationActive) return null;
    return DialogueManager.currentConversationState.subtitle.listenerInfo.transform.gameObject;
}
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Re: Getting speaker and listener game objects. Is this good?

Post by Abelius »

Great! It's working again with your code.

The only reason for the namespace and why it inherited from SequencerCommand is because I ripped the thing from a current script I had, related to those. But yeah, this is much better and straightforward.

I knew it was a good idea to check with you. :mrgreen:

Thank you!
Unity 2019.4.9f1
Dialogue System 2.2.15
Post Reply