Page 1 of 1

Getting speaker and listener game objects. Is this good?

Posted: Thu Feb 08, 2024 1:44 pm
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.

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

Posted: Thu Feb 08, 2024 4:57 pm
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.

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

Posted: Fri Feb 09, 2024 10:51 am
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.

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

Posted: Fri Feb 09, 2024 11:01 am
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;
}

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

Posted: Fri Feb 09, 2024 11:54 am
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!