Adding some additional text fields to the dialogue UI

Announcements, support questions, and discussion for the Dialogue System.
lostmushroom
Posts: 192
Joined: Mon Jul 01, 2019 1:21 pm

Adding some additional text fields to the dialogue UI

Post by lostmushroom »

Hey Tony. I've been playing around with customising the dialogue UI. I'd like to add a few new panels/text fields to the dialogue UI to show variables such as character traits, taglines etc. Previously I've been using quests as a quick way to display variables on screen, but I'm looking for a different solution so I can free up quests to be used for actual quests.

I've set up the panels just to see how it looks, but the text fields are currently reading [var=Chr_Trait] instead of the correct variable. I guess thats because there's nothing telling the new text fields to "read" the dialogue database, so it's not going to show correctly. Is there a way to get them to access the dialogue database and display variables correctly?
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding some additional text fields to the dialogue UI

Post by Tony Li »

Hi,

Yes. Run the text through FormattedText.ParseCode(). Example:

Code: Select all

string traitValue = FormattedText.ParseCode("[var=Chr_Trait]");
traitUIElement.text = traitValue;
lostmushroom
Posts: 192
Joined: Mon Jul 01, 2019 1:21 pm

Re: Adding some additional text fields to the dialogue UI

Post by lostmushroom »

Hey Tony, apologies, bumping an old thread because I never quite got round to implementing this.

I seem to be messing this up - would you mind giving me the for dummies version of what to do? Should I just copy that line of code into the ParseCode function (with variable names changed)?

Also forgot to mention - I'm using TextMeshPro for this, in case it changes anything.
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding some additional text fields to the dialogue UI

Post by Tony Li »

Yes, use that code but change the variable name. Or, to bypass FormattedText.ParseCode() entirely if all you need to show is a variable value:

Code: Select all

traitUIElement.text = DialogueLua.GetVariable("Chr_Trait").asString;
Both work fine in TextMesh Pro, UI Text, or Super Text Mesh.
lostmushroom
Posts: 192
Joined: Mon Jul 01, 2019 1:21 pm

Re: Adding some additional text fields to the dialogue UI

Post by lostmushroom »

Using the first method with FormattedText.ParseCode(), I get a error: CharacterTagline does not exist in the current context.
(I replaced traitUIElement.text with CharacterTagline.text.)

For the second method, where should that line of code go? Still in FormattedText.cs?

Thanks for your help, sorry if I'm being a real noob here..
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding some additional text fields to the dialogue UI

Post by Tony Li »

Hi,

Can you share the line(s) of code you're using?
lostmushroom
Posts: 192
Joined: Mon Jul 01, 2019 1:21 pm

Re: Adding some additional text fields to the dialogue UI

Post by lostmushroom »

Sure, here it is.

Code: Select all

public static string ParseCode(string rawText)
        {
            string traitValue = FormattedText.ParseCode("[var=CHRTrait]");
            CharacterTagline.text = traitValue;

            string text = rawText ?? string.Empty;
            if (text.Contains("["))
            {
                ReplaceLuaTags(ref text);
                ReplaceVarTags(ref text);
            }
            return text;
        }
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding some additional text fields to the dialogue UI

Post by Tony Li »

Hi,

I don't think all of that is necessary. Assuming you have a TextMeshProUGUI variable named CharacterTagline, and you want to set its text to the value of the "CHRTrait" Dialogue System variable, use this one line of code:

Code: Select all

CharacterTagline.text = FormattedText.ParseCode("[var=CHRTrait]");
Or use this one line of code:

Code: Select all

CharacterTagline.text = DialogueLua.GetVariable("CHRTrait").asString;
lostmushroom
Posts: 192
Joined: Mon Jul 01, 2019 1:21 pm

Re: Adding some additional text fields to the dialogue UI

Post by lostmushroom »

Both of those give me the "CharacterTagline does not exist in the current context" error.

Just to check - CharacterTagline is supposed to be referencing a UI object with a TMP Text UI component called CharacterTagline, is that right? Or am I using the wrong thing here?
Attachments
CHRTagline.JPG
CHRTagline.JPG (50.55 KiB) Viewed 474 times
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding some additional text fields to the dialogue UI

Post by Tony Li »

In your script, have you defined a variable named CharacterTagline? For example:

Code: Select all

using UnityEngine;
using TMPro;
using PixelCrushers.DialogueSystem;

public class ShowTagline : MonoBehaviour // Add this script to CharacterTagline GameObject.
{
    public TextMeshProUGUI CharacterTagline; // Assign this in inspector.
    
    private void OnEnable() // When GameObject becomes active, update the text.
    {
        CharacterTagline.text = FormattedText.ParseCode("[var=CHRTrait]");
    }
}
Or, if you want to write a more general-purpose script:

Code: Select all

using UnityEngine;
using TMPro;
using PixelCrushers.DialogueSystem;

public class ShowVariableText : MonoBehaviour // Add this script to a TextMeshProUGUI GameObject.
{
    [VariablePopup] public string variableName;
    
    private void OnEnable()
    {
        GetComponent<TextMeshProUGUI>().text = DialogueLua.GetVariable(variableName).asString;
    }
}
Post Reply