Trigger event on variable change
Hi there, how to make a custom event that triggers when a variables value is changed.
thanks!
Answer by jdean300 · Jun 22, 2016 at 05:31 PM
Using simple variables, the best you can do is constantly check in update whether or not is has changed:
private int m_MyVar = 0;
public int myVar = 0;
public delegate void OnVariableChangeDelegate(int newVal);
public event OnVariableChangeDelegate OnVariableChange;
private void Update()
{
if (myVar != m_MyVar && OnVariableChange != null)
{
m_MyVar = myVar;
OnVariableChange(myVar);
}
}
However, if you can use properties this is a little cleaner because you know when a variable is set:
private int m_MyVar = 0;
public int MyVar
{
get {return m_MyVar;}
set {
if (m_MyVar == value) return;
m_MyVar = value;
if (OnVariableChange != null)
OnVariableChange(m_MyVar);
}
}
public delegate void OnVariableChangeDelegate(int newVal);
public event OnVariableChangeDelegate OnVariableChange;
To subscribe and listen to either of these events:
private void Start()
{
componentWithEvent.OnVariableChange += VariableChangeHandler;
}
private void VariableChangeHandler(int newVal)
{
//do whatever
}
Thanks for the fast answer. Constantly checking for my variables is what i am doing at the moment but i think doing it with events will be more efficient ;) So i will try your second option...seems that is exactly what i need.
ty very much
Hm i tried it but i wont get it to work:
using UnityEngine;
using UnityEngine.UI;
public class DamageUI : $$anonymous$$onoBehaviour
{
public static float damage;
public static float $$anonymous$$Damage;
public static float maxDamage;
[SerializeField]
private Slider slider;
Text damageText; // Reference to the Text component.
void Awake()
{
// Set up the reference.
damageText = GetComponent<Text>();
}
void Start()
{
slider.$$anonymous$$Value = $$anonymous$$Damage;
slider.maxValue = maxDamage;
slider.value = maxDamage / 2;
}
void Update()
{
damage = slider.value;
damage = $$anonymous$$athf.Round(damage);
// Set the displayed text to be the word "Score" followed by the score value.
damageText.text = "Damage: " + damage;
}
public float Damage
{
get
{
return damage;
}
set
{
if (damage == value)
return;
damage = value;
if (OnChangeDamage != null)
OnChangeDamage(damage);
}
}
public delegate void ChangeDamage(float _damage);
public static event ChangeDamage OnChangeDamage;
}
When i try to get a Debug.Log on it from another class:
void Start()
{
ChangeDamageEvent.OnChangeDamage += DamageChanged;
}
void DamageChanged(float _damage)
{
Debug.Log("Damage changed to: " + _damage);
}
Nothing shows up on the console when i drag the slider around so the damage value changes
Did you ever subscribe to the event? you need something like: OnChangeDamage += objectThatRespondsToEvent.FunctionThatHandlesEvent;
Okay I think I see what you are trying to do:
void Start()
{
//Add the following to the end of your start
OnChangeDamage += UpdateDamageText;
}
private void UpdateDamageText(float dmg)
{
damageText.text = "Damage: " + dmg;
}
although you could just handle this inside the property setter:
public float Damage
{
get
{
return damage;
}
set
{
if (damage == value)
return;
damage = value;
damageText.text = "Damage: " + damage;
}
}
Events are better suited for communicating changes in one script to another script.
Isnt that exactly what i do here?
void Start()
{
ChangeDamageEvent.OnChangeDamage += DamageChanged;
}
void DamageChanged(float _damage)
{
Debug.Log("Damage changed to: " + _damage);
}
Handling it inside the property setter is not an option... i need to do that in another class.
There are more locations that need to know the dmg has changed. Thats what i need the event for ;)
Your answer
Follow this Question
Related Questions
Changing a value over time 1 Answer
onClick Event for UI Text? 1 Answer
UI stopped working 0 Answers
UnityEvent.Invoke() causing ArgumentOutOfRangeException 0 Answers
Getting an object's y coordinate 2 Answers