Page 1 of 1

Accessing DS Variable in Script and Displaying in Text issue

Posted: Mon Aug 12, 2024 7:38 am
by pierretx
Hello,

I have a dialogue system number variable called "Money" that I want to display on my UI, however it doesn't update according to what's entered in the inspector.

Would anyone know what I'm missing here?

Code: Select all

using PixelCrushers.DialogueSystem;

public class Money : MonoBehaviour
{
    public int moneyAmount;
    public TextMeshProUGUI moneyDisplay;

    void Update()
    {
        moneyDisplay.SetText(moneyAmount.ToString()); 
        UpdateMoneyVar();
    }

    public void UpdateMoneyVar()
    {
        int moneyAmount = DialogueLua.GetVariable("Money").asInt;
    }
I have registered 'UpdateMoneyVar()' with Lua in case that helps.

Thank you!

Re: Accessing DS Variable in Script and Displaying in Text issue

Posted: Mon Aug 12, 2024 8:35 am
by Tony Li
Hi,

I see you also have a C# variable named moneyAmount. I recommend having only one variable to track money -- either your C# variable moneyAmount, or your Dialogue System Number variable Money.

If you use the DS variable, then in C# use DialogueLua.GetVariable() and SetVariable() to access it. Example:

Code: Select all

void Update()
{
    moneyDisplay.text = DialogueLua.GetVariable("Money").asInt;
}
Side note: Setting TextMeshProUGUI text in Update() is fine for an initial "get it working" pass. But I recommend changing it slightly in the future. When you set TextMeshProUGUI text, it rebuilds the entire mesh that visually represents the text. This is a somewhat heavy process that you don't need to do every frame. Instead, you can do it only when the Money variable changes. See Monitor Variable Changes.

Re: Accessing DS Variable in Script and Displaying in Text issue

Posted: Tue Aug 13, 2024 9:16 am
by pierretx
Hi Tony,

Thanks for the help.
I just had to do add '.ToString()' at the end to get it to work as text.

One last question though, how can I increase the value of a number DS variable in script?

Thank you!

Re: Accessing DS Variable in Script and Displaying in Text issue

Posted: Tue Aug 13, 2024 9:42 am
by Tony Li
Hi,

Code: Select all

int money = DialogueLua.GetVariable("Money").asInt;
DialogueLua.SetVariable("Money", money + 1);
DialogueManager.SendUpdateTracker(); // Add this line if you need to update the quest tracker HUD.