UI Bar connected to Variables

Announcements, support questions, and discussion for the Dialogue System.
Albyd
Posts: 29
Joined: Thu Jul 21, 2022 4:46 pm

UI Bar connected to Variables

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

Re: UI Bar connected to Variables

Post 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;
}
Albyd
Posts: 29
Joined: Thu Jul 21, 2022 4:46 pm

Re: UI Bar connected to Variables

Post 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.
Attachments
error.png
error.png (50.75 KiB) Viewed 131 times
confidence_function.png
confidence_function.png (54.74 KiB) Viewed 131 times
User avatar
Tony Li
Posts: 20867
Joined: Thu Jul 18, 2013 1:27 pm

Re: UI Bar connected to Variables

Post 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.
Albyd
Posts: 29
Joined: Thu Jul 21, 2022 4:46 pm

Re: UI Bar connected to Variables

Post 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?
Attachments
variable.png
variable.png (11.34 KiB) Viewed 97 times
slider.png
slider.png (56.52 KiB) Viewed 97 times
dialogue.png
dialogue.png (186.02 KiB) Viewed 97 times
User avatar
Tony Li
Posts: 20867
Joined: Thu Jul 18, 2013 1:27 pm

Re: UI Bar connected to Variables

Post 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;
    }
}
Albyd
Posts: 29
Joined: Thu Jul 21, 2022 4:46 pm

Re: UI Bar connected to Variables

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

Re: UI Bar connected to Variables

Post 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;
}
Albyd
Posts: 29
Joined: Thu Jul 21, 2022 4:46 pm

Re: UI Bar connected to Variables

Post by Albyd »

Nope, the value is not increasing.
The debug is not catching any change in the Confidence variable.
User avatar
Tony Li
Posts: 20867
Joined: Thu Jul 18, 2013 1:27 pm

Re: UI Bar connected to Variables

Post by Tony Li »

Hi,

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