Adding some additional text fields to the dialogue UI
-
- Posts: 192
- Joined: Mon Jul 01, 2019 1:21 pm
Adding some additional text fields to the dialogue UI
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?
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?
Re: Adding some additional text fields to the dialogue UI
Hi,
Yes. Run the text through FormattedText.ParseCode(). Example:
Yes. Run the text through FormattedText.ParseCode(). Example:
Code: Select all
string traitValue = FormattedText.ParseCode("[var=Chr_Trait]");
traitUIElement.text = traitValue;
-
- Posts: 192
- Joined: Mon Jul 01, 2019 1:21 pm
Re: Adding some additional text fields to the dialogue UI
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.
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.
Re: Adding some additional text fields to the dialogue UI
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:
Both work fine in TextMesh Pro, UI Text, or Super Text Mesh.
Code: Select all
traitUIElement.text = DialogueLua.GetVariable("Chr_Trait").asString;
-
- Posts: 192
- Joined: Mon Jul 01, 2019 1:21 pm
Re: Adding some additional text fields to the dialogue UI
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..
(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..
Re: Adding some additional text fields to the dialogue UI
Hi,
Can you share the line(s) of code you're using?
Can you share the line(s) of code you're using?
-
- Posts: 192
- Joined: Mon Jul 01, 2019 1:21 pm
Re: Adding some additional text fields to the dialogue UI
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;
}
Re: Adding some additional text fields to the dialogue UI
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:
Or use this one line of code:
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]");
Code: Select all
CharacterTagline.text = DialogueLua.GetVariable("CHRTrait").asString;
-
- Posts: 192
- Joined: Mon Jul 01, 2019 1:21 pm
Re: Adding some additional text fields to the dialogue UI
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?
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 (50.55 KiB) Viewed 480 times
Re: Adding some additional text fields to the dialogue UI
In your script, have you defined a variable named CharacterTagline? For example:
Or, if you want to write a more general-purpose script:
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]");
}
}
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;
}
}