Combining variables as text

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

Combining variables as text

Post by lostmushroom »

Hey Tony. I'm trying to combine multiple variables to make the text display a single sentence to describe a character, something like this:

"Jack, 19, carpenter"

To achieve this, I'm referencing 3 variables: CHR_Name, CHR_Age, CHR_Occupation. I also have another variable that is supposed to include all of this which is CHR_Description.

In the script field, I have the following:

Variable["CHRDescription"] = "[var=CHR_Name], [var=CHR_Age], [CHR_Occupation]";

However, instead of showing up as the correct text, it's just showing up as that line of script.

I feel like I've done something like this before but don't remember exactly how, maybe this way isn't possible.
User avatar
Tony Li
Posts: 22101
Joined: Thu Jul 18, 2013 1:27 pm

Re: Combining variables as text

Post by Tony Li »

Hi,

The [var=variable] tag is for text fields, but the Script field is a Lua code field. Use this instead:

Code: Select all

Variable["CHRDescription"] = Variable["CHR_Name"] .. ", " .. Variable["CHR_Age"] .. ", " .. Variable["CHR_Occupation"]
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Re: Combining variables as text

Post by lostmushroom »

Thanks Tony, that makes sense.

It's displaying correctly now, but I'm having trouble getting it to change visually if the variable changes. If I change one of the variables in the Script field, for example set CHR_Age to 20, it still displays 19 in the on screen text field.

This is the script I'm using to display variables on screen, I think it's one you gave me a while ago :)

Thanks for your help as always.

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 Update()
    {
        GetComponent<TextMeshProUGUI>().text = DialogueLua.GetVariable(variableName).asString;
    }
}
User avatar
Tony Li
Posts: 22101
Joined: Thu Jul 18, 2013 1:27 pm

Re: Combining variables as text

Post by Tony Li »

Hi,

Use this version instead:

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 Start()
    {
        Language.Lua.Assignment.MonitoredVariables.Add(variableName);
        Language.Lua.Assignment.VariableChanged += OnVariableChanged;
        UpdateText();
    }

    private void OnDestroy()
    {
        Language.Lua.Assignment.VariableChanged -= OnVariableChanged;
    }

    private void OnVariableChanged(string variableName, object value)
    {
        UpdateText();
    }

    private void UpdateText()
    {
        GetComponent<TextMeshProUGUI>().text = DialogueLua.GetVariable(variableName).asString;
    }
}
You'll need to be using DS version 2.2.44 or newer.
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Re: Combining variables as text

Post by lostmushroom »

Working perfectly now, thanks so much!
User avatar
Tony Li
Posts: 22101
Joined: Thu Jul 18, 2013 1:27 pm

Re: Combining variables as text

Post by Tony Li »

Happy to help!
Post Reply