Accessing DS Variable in Script and Displaying in Text issue

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
pierretx
Posts: 4
Joined: Fri Jul 05, 2024 9:47 am

Accessing DS Variable in Script and Displaying in Text issue

Post 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!
User avatar
Tony Li
Posts: 22145
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
pierretx
Posts: 4
Joined: Fri Jul 05, 2024 9:47 am

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

Post 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!
User avatar
Tony Li
Posts: 22145
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
Post Reply