UI Bar connected to Variables
UI Bar connected to Variables
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
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
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
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.
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 (50.75 KiB) Viewed 650 times
-
- confidence_function.png (54.74 KiB) Viewed 650 times
Re: UI Bar connected to Variables
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.
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
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?
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 (11.34 KiB) Viewed 616 times
-
- slider.png (56.52 KiB) Viewed 616 times
-
- dialogue.png (186.02 KiB) Viewed 616 times
Re: UI Bar connected to Variables
Hi,
What is your script?
It should look something like:
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
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.
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
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
Nope, the value is not increasing.
The debug is not catching any change in the Confidence variable.
The debug is not catching any change in the Confidence variable.
Re: UI Bar connected to Variables
Hi,
Does the Debug.Log line appear at all in the Console?
Does the Debug.Log line appear at all in the Console?