Page 1 of 2

UI Bar connected to Variables

Posted: Wed May 15, 2024 7:02 am
by Albyd
Hi there,
I would like to connect a UI bar to a variable I increase through the dialogue system.
Is there a way to do it and show it through a simple animation?

Many thanks,
Alberto

Re: UI Bar connected to Variables

Posted: Wed May 15, 2024 8:38 am
by Tony Li
Yes. Please see: Monitor Lua Variable Changes. For example, say you have a variable named Score. You can monitor it like this:

Code: Select all

public Slider scoreSlider; //<-- Assign in inspector.

void Start()
{
    Language.Lua.Assignment.MonitoredVariables.Add("Score");
    Language.Lua.Assignment.VariableChanged += OnVariableChanged;
}

void OnVariableChanged(string variableName, object variableValue)
{
    if (variableName == "Score") scoreSlider.value = (float)variableValue;
}

Re: UI Bar connected to Variables

Posted: Tue May 21, 2024 12:36 pm
by Albyd
Hi there, thanks again for the reply.
I'm getting this error.

I've created a custom function and called it "Confidence".
What am I doing wrong?

Sorry but I'm not that great at programming.

Re: UI Bar connected to Variables

Posted: Tue May 21, 2024 3:09 pm
by Tony Li
Hi,

A function named "Confidence" and a variable named "Confidence" are two separate things. Is there one in specific that you'd like to use?

What DS version are you using? You may need to update to get access to MonitoredVariables and VariableChanged.

Re: UI Bar connected to Variables

Posted: Fri May 24, 2024 6:21 am
by Albyd
I would like to use a Confidence Variable.
I've updated the dialogue system and is not giving me errors anymore.

I've created a slider bar with fill, connected the script to the slider and assigned it to the script.
I then created a Confidence variable on the dialogue system and assigned Variable["Confidence"] = 0.5 through the script section in one of my dialogues.

The slider bar is in a canvas outside the dialogue UI.
Unfortunately, it doesn't seem to increase the slider when I run the dialogue.

What am I doing wrong?

Re: UI Bar connected to Variables

Posted: Fri May 24, 2024 9:55 am
by Tony Li
Hi,

What is your script?

It should look something like:

Code: Select all

using UnityEngine;
using UnityEngine.UI;
using PixelCrushers.DialogueSystem;
public class Confidence_bar : MonoBehaviour
{
    public Slider scoreSlider; //<-- Assign in inspector.

    void Start()
    {
        Language.Lua.Assignment.MonitoredVariables.Add("Confidence");
        Language.Lua.Assignment.VariableChanged += OnVariableChanged;
    }

    void OnVariableChanged(string variableName, object variableValue)
    {
        if (variableName == "Confidence") scoreSlider.value = (float)variableValue;
    }
}

Re: UI Bar connected to Variables

Posted: Fri May 24, 2024 11:57 am
by Albyd
I was just missing "using PixelCrushers.DialogueSystem;".
For the rest, the script is the same and unfortunately, even including the missing bit, still doesn't show any changes in the slider.

Re: UI Bar connected to Variables

Posted: Fri May 24, 2024 12:20 pm
by Tony Li
Try adding a Debug.Log to check if the variable changed method is being called:

Code: Select all

void OnVariableChanged(string variableName, object variableValue)
{
    Debug.Log($"{variableName} changed to {variableValue}");
    if (variableName == "Confidence") scoreSlider.value = (float)variableValue;
}

Re: UI Bar connected to Variables

Posted: Fri May 24, 2024 12:35 pm
by Albyd
Nope, the value is not increasing.
The debug is not catching any change in the Confidence variable.

Re: UI Bar connected to Variables

Posted: Fri May 24, 2024 2:14 pm
by Tony Li
Hi,

Does the Debug.Log line appear at all in the Console?