Page 1 of 1
Beginner's question about dialogue Fields and C#
Posted: Sun Nov 01, 2020 5:50 am
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!
Re: Beginner's question about dialogue Fields and C#
Posted: Sun Nov 01, 2020 8:12 am
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;
}
Re: Beginner's question about dialogue Fields and C#
Posted: Sun Nov 01, 2020 11:35 am
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...
Re: Beginner's question about dialogue Fields and C#
Posted: Sun Nov 01, 2020 12:27 pm
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.
Re: Beginner's question about dialogue Fields and C#
Posted: Sun Nov 01, 2020 1:31 pm
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!
Re: Beginner's question about dialogue Fields and C#
Posted: Sun Nov 01, 2020 1:37 pm
by Tony Li
Oops, sorry, typo. I forgot to add ".fields":
Code: Select all
string fieldValue = Field.LookupValue(subtitle.dialogueEntry.fields, "My Custom Field");
Re: Beginner's question about dialogue Fields and C#
Posted: Tue Nov 03, 2020 4:49 am
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!
Re: Beginner's question about dialogue Fields and C#
Posted: Tue Nov 03, 2020 8:10 am
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);
}
}
Re: Beginner's question about dialogue Fields and C#
Posted: Tue Nov 03, 2020 3:49 pm
by AlainB
Aaaaaargh!
I tried to access it from DialogueManager.currentConversationState, but can't figure out how it works...
Asking you AGAIN....
Re: Beginner's question about dialogue Fields and C#
Posted: Tue Nov 03, 2020 4:00 pm
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);
}