Beginner's question about dialogue Fields and C#

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
AlainB
Posts: 5
Joined: Sun Nov 01, 2020 5:46 am

Beginner's question about dialogue Fields and C#

Post by AlainB »

Hello!
I have a beginner's question and I don't know if I'm in the right place.

I'm trying to access custom fields in the dialogue current entry from a C# script.
But I can't find a method (neither from DialogueLua nor from DialogueManager) to access this information.
It should be easy, but I can't find it.

Anything I can think of?
Thank you!
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Beginner's question about dialogue Fields and C#

Post by Tony Li »

Hi Alain,

(I'm just copying my reply from the Unity forum. Either place is fine to ask questions.)

When you create content in the Dialogue Editor window, it's added to a dialogue database asset. At runtime, the Dialogue System loads the dialogue database content into an in-memory dialogue database called DialogueManager.masterDatabase, which should be considered static and read-only. It also copies certain info that can change during play -- such as Actors, Quests, and Variables -- into the Dialogue System's runtime Lua environment, which is read-write. Use DialogueLua to access the Lua values.

To read a field from any arbitrary dialogue entry, access it through DialogueManager.masterDatabase. For example:

Code: Select all

DialogueEntry entry = DialogueManager.masterDatabase.GetDialogueEntry(conversationID, entryID);
string customFieldValue = Field.LookupValue(entry.fields, "My Custom Field");
When a conversation is active, the current conversation state is always accessible through DialogueManager.currentConversationState. This includes the current dialogue entry:

Code: Select all

DialogueEntry currentDialogueEntry = DialogueManager.currentConversationState.subtitle.dialogueEntry;
The Dialogue System has a number of C# event hooks and script messages (reference). One of these messages is OnConversationLine(Subtitle), which you can also use to work with the current dialogue entry:

Code: Select all

void OnConversationLine(Subtitle subtitle) // Called when the conversation gets to a dialogue entry.
{
    string fieldValue = Field.LookupValue(subtitle.dialogueEntry.fields, "My Custom Field");
    Debug.Log("Current line's 'My Custom Field' value is: " + fieldValue;
}
AlainB
Posts: 5
Joined: Sun Nov 01, 2020 5:46 am

Re: Beginner's question about dialogue Fields and C#

Post by AlainB »

Thank you for your quick answer!

I must be really stupid but there are no such methods appearing in the Visual Studio "Smart typing tool".

I declarerd the following:

using Language.Lua;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PixelCrushers.DialogueSystem;

But the C# don't show me any specific method (No OnConversation... or anything else) and the "Field" class is declared ambiguous beetween LUA and Dialogue Manager (I don't know if I make myself clear...)....

Well, must be a rookie issue, sorry to take your time...
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Beginner's question about dialogue Fields and C#

Post by Tony Li »

Hi,

You can remove "using Language.Lua".

Messages like OnConversationStart() that are listed here won't show up in the smart typing tool. But you can type them in directly.
AlainB
Posts: 5
Joined: Sun Nov 01, 2020 5:46 am

Re: Beginner's question about dialogue Fields and C#

Post by AlainB »

Sorry, it's me again.

When I copy your code in my class:

void OnConversationLine(Subtitle subtitle) // Called when the conversation gets to a dialogue entry.
{
string fieldValue = Field.LookupValue(subtitle.dialogueEntry, "abcdcustomfield");
Debug.Log("Current line's 'My Custom Field' value is: " + fieldValue);
}

It results of an error message from the string fieldValue declaration: "Conversion of 'PixelCrusher.DialogueSystem.DialogueEntry' in 'System.Collections.Generic.List<PixelCrushers.DialogueSystem.Field>'

They are apparently not from the same type...

That's the last time I bother you, promise! :oops:
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Beginner's question about dialogue Fields and C#

Post by Tony Li »

Oops, sorry, typo. I forgot to add ".fields":

Code: Select all

    string fieldValue = Field.LookupValue(subtitle.dialogueEntry.fields, "My Custom Field");
AlainB
Posts: 5
Joined: Sun Nov 01, 2020 5:46 am

Re: Beginner's question about dialogue Fields and C#

Post by AlainB »

Thank you so much for your help!

I just got one more question (Yes, I know. I told you I wont).

How can I grab informations from Dialogues custom fields that are pointed my a response menu.
For example, if there are two choices avaiable, how can I get the "fields" field from these dialogue choices?

Still a beginner!

Have a grat day!
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Beginner's question about dialogue Fields and C#

Post by Tony Li »

Hi Alain,

The OnConversationResponseMenu method provides the list of responses. (You can also get it from DialogueManager.currentConversationState.) Each response has a property that points to its destination dialogue entry.

Code: Select all

void OnConversationResponseMenu(Response[] responses)
 {
     foreach (var response in responses)
     {
         string menuText = response.formattedText.text;
         string customValue = Field.LookupValue(response.destinationEntry.fields, "My Custom Field);
         Debug.Log("The custom field for '" + menuText + "' is: " + customValue);
     }
 }
AlainB
Posts: 5
Joined: Sun Nov 01, 2020 5:46 am

Re: Beginner's question about dialogue Fields and C#

Post by AlainB »

Aaaaaargh!
I tried to access it from DialogueManager.currentConversationState, but can't figure out how it works...
Asking you AGAIN....
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Beginner's question about dialogue Fields and C#

Post by Tony Li »

Hi,

API reference: DialogueManager.currentConversationState

A conversation state contains:
  • subtitle: The dialogue entry currently being shown. (Or most recently shown, if showing a response menu.)
  • npcResponses: NPC nodes linked from the subtitle's dialogue entry whose conditions are true.
  • pcResponses: Player nodes linked from the subtitle's dialogue entry whose conditions are true.
When a response menu is being shown, the responses are in the conversation state's pcResponses.

Example code:

Code: Select all

foreach (var response in DialogueManager.currentConversationState.pcResponses)
{
    string menuText = response.formattedText.text;
     string customValue = Field.LookupValue(response.destinationEntry.fields, "My Custom Field);
     Debug.Log("The custom field for '" + menuText + "' is: " + customValue);
}
Post Reply