Page 1 of 1

How to show the value of variables?

Posted: Thu Sep 29, 2016 3:11 pm
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?

Re: How to show the value of variables?

Posted: Thu Sep 29, 2016 5:32 pm
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();
            }
        }
    }
}

Re: How to show the value of variables?

Posted: Sun Oct 02, 2016 3:50 am
by HaiiroMukuro
Thanks! I also add a class in the script if the player wants to spend money. Thanks a lot! :D

Re: How to show the value of variables?

Posted: Wed Oct 05, 2016 1:18 pm
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.

Re: How to show the value of variables?

Posted: Wed Oct 05, 2016 1:26 pm
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.

Re: How to show the value of variables?

Posted: Wed Oct 05, 2016 1:55 pm
by HaiiroMukuro
Oh, that's how Event System works. Thanks :D

Re: How to show the value of variables?

Posted: Wed Oct 05, 2016 2:44 pm
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. :-)