How to show the value of variables?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

How to show the value of variables?

Post by HaiiroMukuro »

Hello! :)
I declare a variable named "Money."
I create a UI text named "CurrentMoney" and place it on the upper right corner.
Then use I the script.

Code: Select all

Variable["Money"] = Variable["Money"] + 20;
ShowAlert("Gained 20 Dollars")
Now, the player has 20 dollars. How will I show the value of "Money" using the UI text I created?
User avatar
Tony Li
Posts: 21079
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to show the value of variables?

Post by Tony Li »

Hi,

You'll need a small script (or something equivalent like a PlayMaker FSM). There are several ways to do this. This is just one. We'll add a Lua function called "AddMoney". Replace your Script with this:

Code: Select all

AddMoney(20)
instead of this:

Code: Select all

Variable["Money"] = Variable["Money"] + 20;
ShowAlert("Gained 20 Dollars")
Then create a C# script named LuaMoneyFunctions containing this code and add it to the Dialogue Manager:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class LuaMoneyFunctions : MonoBehaviour {

    void OnEnable() {
        Lua.RegisterFunction("AddMoney", this, SymbolExtensions.GetMethodInfo(() => AddMoney((double)0)));
    }

    void OnDisable() {
        Lua.UnregisterFunction("AddMoney");
    }

    public void AddMoney(double amount) {
        DialogueManager.ShowAlert("Gained " + amount + " Dollars");
        var money = DialogueLua.GetVariable("Money").AsFloat + amount;
        DialogueLua.SetVariable("Money", money);
        var currentMoney = GameObject.Find("CurrentMoney");
        if (currentMoney == null) {
            Debug.LogError("Can't find GameObject named 'CurrentMoney'!");
        } else { 
            var currentMoneyText = currentMoney.GetComponent<UnityEngine.UI.Text>();
            if (currentMoneyText == null) {
                Debug.LogError("CurrentMoney doesn't have a Text component!");
            } else {
                currentMoneyText.text = money.ToString();
            }
        }
    }
}
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

Re: How to show the value of variables?

Post by HaiiroMukuro »

Thanks! I also add a class in the script if the player wants to spend money. Thanks a lot! :D
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

Re: How to show the value of variables?

Post by HaiiroMukuro »

I still have a problem. I have Button that shows status. The statuses are Money, IQ, and Reputation. On the scene where I created the button for status works fine. But when I load next scene, the button cannot be click. By the way, I put the canvas,button and the stats inside the dialogue manager.
User avatar
Tony Li
Posts: 21079
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to show the value of variables?

Post by Tony Li »

Make sure your new scene has an EventSystem. One solution is to make the EventSystem a child of the Dialogue Manager. This way it will stick around when you change scenes.
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

Re: How to show the value of variables?

Post by HaiiroMukuro »

Oh, that's how Event System works. Thanks :D
User avatar
Tony Li
Posts: 21079
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to show the value of variables?

Post by Tony Li »

You're welcome!

Unity UI requires an EventSystem for interaction (button clicks, scrolling, etc.). Even if your scene doesn't use the Dialogue System, if it has an interactable UI element, make sure it has an EventSystem. :-)
Post Reply