Add a field to a Dialogue Entry

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Kit
Posts: 13
Joined: Thu Jan 05, 2023 11:02 am

Add a field to a Dialogue Entry

Post by Kit »

Hi!

I'm trying to add a field to the Dialogue Entry. I somehow need a way to add a field that consists of a list (or an array), so that I can have a list/array of various dialogue options that can be edited and added to in the editor. This is so that one of the dialogue options saved on the list/array can be picked at random. Is there a way to do this? I've experimented with CustomFieldTypes but I would rather just have a whole new field to be shown along with fields such as Dialogue Text and Scripts etc. instead of a field type to use in the Script field.

For now, I've been trying to add strings to a list via. a custom script used in the Script field instead, and then attempted to change the Dialogue Text from there, but I haven't had much luck.
Image
Is there a proper way of changing the string in the Dialogue Text field?
I've tried in many ways, but here are two examples:
PixelCrushers.DialogueSystem.DialogueManager.currentConversationState.subtitle.dialogueEntry.DialogueText = list[nr];
PixelCrushers.DialogueSystem.DialogueManager.currentConversationState.subtitle.formattedText.text = list[nr];

Here's a picture of the random dialogue text picker method:
Image

Hope you can help me!
User avatar
Tony Li
Posts: 21684
Joined: Thu Jul 18, 2013 1:27 pm

Re: Add a field to a Dialogue Entry

Post by Tony Li »

Hi,

If you just want to choose random text, you can use the RandomElement() Lua function. You don't need acustom field. For example, in Dialogue Text:
  • Dialogue Text: [lua( RandomElement("Hello.|Howdy!|Greetings.") )]
The underlying data type of all fields is a string. If you need to store lists for some other reason, please describe what you need it to do. I can probably suggest a good way to handle it.
Kit
Posts: 13
Joined: Thu Jan 05, 2023 11:02 am

Re: Add a field to a Dialogue Entry

Post by Kit »

Hi Tony

Thank you, this was exactly what I needed!

I do have another question that's not really related to my original question. Is it possible to save the last player that spoke in a node as an actor, and then later use this actor variable to set the actor to that character in the next conversation I start?
So, instead of setting the actor in the dialogue entry, can I, from a script, set the actor in the START node of a new conversation to the last active player actor?
User avatar
Tony Li
Posts: 21684
Joined: Thu Jul 18, 2013 1:27 pm

Re: Add a field to a Dialogue Entry

Post by Tony Li »

Yup! The last conversation's primary actor and conversant will be in the properties DialogueManager.currentActor and currentConversant. If the speaker of the last line of dialogue was different, it will be in DialogueManager.currentConversationState.subtitle.speaker.

Once you have this transform, you can pass it to DialogueManager.StartConversation():

Code: Select all

DialogueManager.StartConversation("Next Conversation", DialogueManager.currentActor, npcTransform);
Kit
Posts: 13
Joined: Thu Jan 05, 2023 11:02 am

Re: Add a field to a Dialogue Entry

Post by Kit »

Apologies for the very late reply!

This wasn't entirely what I needed, but that's my bad for explaining it poorly. I worked out what I needed from what you gave me however, so it did help a ton in the end. Thank you!

I have since then encountered another smaller problem that has led me to one question: Is it not at all possible to call functions or access public variables from scripts outside of the Pixel.Crushers namespace? So far, I haven't found a way to get around this at all.

I have this public, kind of global variable that I need to change probably on every Dialogue Entry and I would like to do so through the Dialogue Entry script rather than having to add it as a script/variable manually to every dialogue entry node for every single conversation. Is there a way to do this?
User avatar
Tony Li
Posts: 21684
Joined: Thu Jul 18, 2013 1:27 pm

Re: Add a field to a Dialogue Entry

Post by Tony Li »

Hi,

If you're talking about directly modifying the DialogueEntry class, I strongly recommend against it. You'll lose your modifications every time you update the Dialogue System. The Dialogue System has plenty of hooks that you can use in your own scripts.

The Dialogue System is in the Plugins folder, which Unity compiles separately, without any visibility to any scripts that are outside of the Plugins folder. Your own scripts, which should be outside the Plugins folder, has visibility into all scripts in the Plugins folder and out.

You can add an OnConversationLine method to a script that you've added to the Dialogue Manager GameObject. In that method, you can change your global variable. An example:

Code: Select all

using UnityEngine;
using PixelCrushers;
public void ScoreManager : MonoBehaviour
{
    public int score = 0;
    
    void OnConversationLine(Subtitle subtitle)
    {
        // Get the value of the current dialogue entry's custom "Points" field:
        int points = Field.LookupInt(subtitle.dialogueEntry.fields, "Points");
        
        // Add it to the score:
        score += points;
    }
}
Kit
Posts: 13
Joined: Thu Jan 05, 2023 11:02 am

Re: Add a field to a Dialogue Entry

Post by Kit »

Thank you so much! I'll try this! And thank you for explaining how it all works, it's greatly appreciated.
User avatar
Tony Li
Posts: 21684
Joined: Thu Jul 18, 2013 1:27 pm

Re: Add a field to a Dialogue Entry

Post by Tony Li »

Happy to help!
Post Reply