Page 1 of 1

Combining variables as text

Posted: Sun Oct 13, 2024 3:43 pm
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.

Re: Combining variables as text

Posted: Sun Oct 13, 2024 8:15 pm
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"]

Re: Combining variables as text

Posted: Mon Oct 14, 2024 10:11 am
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;
    }
}

Re: Combining variables as text

Posted: Mon Oct 14, 2024 10:38 am
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.

Re: Combining variables as text

Posted: Mon Oct 14, 2024 10:45 am
by lostmushroom
Working perfectly now, thanks so much!

Re: Combining variables as text

Posted: Mon Oct 14, 2024 11:23 am
by Tony Li
Happy to help!