Check if a dialogue conversation is happening in code

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
n_hagialas
Posts: 36
Joined: Wed May 03, 2017 4:34 pm

Check if a dialogue conversation is happening in code

Post by n_hagialas »

Hi Tony,

I wanted to know if there was a way to check if the player is currently in a dialogue system conversation, trigger, or event (perhaps 1 line or property that encompasses all of them)?

The situation is that I have a player with a canMove boolean. I turn this off with SendMessage to ensure that the player can't move during dialogue sequences / conversations. However, we just recently implemented a new attack that turns the canMove variable on at the end of a coroutine. This means that the player can use this new attack and lounge into a dialogue system trigger and the variable canMove will turn back on after 1 second which might mean they can move in the middle of a conversation, etc.

I want to implement something like this in my coroutine:
if (a dialogue event / trigger / conversation is active)
WaitUntil(DialogueEvent.end)(as in user finishes the event or whatever it is) --> then canMove = true;
else (canMove = true);

Could you please advise on how to go about this solution?

Thank you!
-Nik
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Check if a dialogue conversation is happening in code

Post by Tony Li »

Hi Nik,

Check DialogueManager.IsConversationActive. This returns true if any conversation is currently active.

If you allow simultaneous conversations, you may only want to know if the player is in a conversation. To do this, add code like this to the player instead:

Code: Select all

bool isInConversation = false;

void OnConversationStart(Transform other) {
    isInConversation = true;
}

void OnConversationEnd(Transform other) {
    isInConversation = false;
}
Then you can check isInConversation.

(The Dialogue System automatically calls OnConversationStart and OnConversationEnd on any GameObjects that are involved in conversations. See Script Messages for more info.)
n_hagialas
Posts: 36
Joined: Wed May 03, 2017 4:34 pm

Re: Check if a dialogue conversation is happening in code

Post by n_hagialas »

Thank you, Tony. That worked!
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Check if a dialogue conversation is happening in code

Post by Tony Li »

Glad to help!
Lauri K
Posts: 8
Joined: Tue Aug 04, 2020 3:08 pm

Re: Check if a dialogue conversation is happening in code

Post by Lauri K »

Hi,

could someone open up this a bit? If I add below Tony's code to my Player, how to check e.g. from NPC's dialogue triggers that Player is already in a conversation, preferably with Lua?

I have added below code to my player but don't know now how to read "isInConversation" boolean. Also the methods and variables are grayed out in Visual Studio, which says it's methods are not used anywhere.

What I'd like to have is a Lua condition in Dialogue System Trigger, so that Player can be having just one conversation at the same time (but other, NPC conversations, running on the background without interruptions). I haven't figured it still out how to execute.

Cheers,
Lauri

Tony Li wrote: Wed Mar 07, 2018 10:10 pm Hi Nik,

Check DialogueManager.IsConversationActive. This returns true if any conversation is currently active.

If you allow simultaneous conversations, you may only want to know if the player is in a conversation. To do this, add code like this to the player instead:

Code: Select all

bool isInConversation = false;

void OnConversationStart(Transform other) {
    isInConversation = true;
}

void OnConversationEnd(Transform other) {
    isInConversation = false;
}
Then you can check isInConversation.

(The Dialogue System automatically calls OnConversationStart and OnConversationEnd on any GameObjects that are involved in conversations. See Script Messages for more info.)
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Check if a dialogue conversation is happening in code

Post by Tony Li »

Hi,

Add this script to your player:

PlayerConversationTracker.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class PlayerConversationTracker : MonoBehaviour
{
    void OnConversationStart(Transform other)
    {
        DialogueLua.SetVariable("IsPlayerInConversation", true);
    }
    
    void OnConversationEnd(Transform other)
    {
        DialogueLua.SetVariable("IsPlayerInConversation", false);    
    }
}
When the player is in a conversation, it will set a Lua variable named "IsPlayerInConversation" true. When the player ends the conversation, it will set the variable false.

In your Dialogue System Trigger, set the Conditions > Lua Conditions to require:

Code: Select all

Variable["IsPlayerInConversation"] ~= true
This requires that the variable is not true. If it's not true, the Dialogue System Trigger is allowed to start its conversation.
DrewThomasArt
Posts: 60
Joined: Thu Mar 24, 2022 12:07 am

Re: Check if a dialogue conversation is happening in code

Post by DrewThomasArt »

Tony Li wrote: Thu Aug 06, 2020 3:31 pm
When the player is in a conversation, it will set a Lua variable named "IsPlayerInConversation" true. When the player ends the conversation, it will set the variable false.

In your Dialogue System Trigger, set the Conditions > Lua Conditions to require:

Code: Select all

Variable["IsPlayerInConversation"] ~= true
This requires that the variable is not true. If it's not true, the Dialogue System Trigger is allowed to start its conversation.
EDIT: NEVERMIND, I had to set the variable to false, not just the bool, because that's what the Dialogue System Saver was saving. I was focused on the bool "IsConversationActive" when there was also the variable "IsPlayerInActiveConversation".
SOLVED. For future viewers, use "DialogueLua.SetVariable("IsPlayerInConversation", false);" in "void ApplyData" if you are saving the game from conversations where the variable is set to true.

Hi Tony,

This all works perfectly, but when I load scenes up from the main menu, conversations won't start for any object that has the
"Variable["IsPlayerInConversation"] ~= true" condition for starting conversations.
Even though I can see in the Inspector that the variable is set to false.
Everything is coded as explained in this thread,

Conversations will, of course, start on objects that do not have this variable condition, and then when that conversation ends, I can resume talking to anything, even objects with this condition.

[SOLVED]
Last edited by DrewThomasArt on Fri Nov 11, 2022 7:30 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Check if a dialogue conversation is happening in code

Post by Tony Li »

Just checking -- is this working now?
DrewThomasArt
Posts: 60
Joined: Thu Mar 24, 2022 12:07 am

Re: Check if a dialogue conversation is happening in code

Post by DrewThomasArt »

Tony Li wrote: Fri Nov 11, 2022 1:32 pm Just checking -- is this working now?
It's working.
Sorry for the confusion. Wanted to keep it up in case it's helpful, I'll edit it so it's less confusing
Post Reply