How do I update a VisualElement when a value changes in the game?

I have a custom VisualElement that displays some values. These values are pulled from a singleton that stores some globally accessible information. The values can change as the game runs.

I can get the initial values in the singleton from within the VisualElement’s constructor. But, VisualElements do not contain an Update method. So I don’t have an easy way to check if any of the values have changed. What is the correct way to update the visual element when the values change?

I’m sorry if this is a stupid question. I’m new to UI Toolkit and I could not find any answers in the Documentation or by Googling. Maybe I just missed something.

We’ll be releasing data binding very soon that’s going to do exactly that. wait for announcements. Meanwhile, you can create your own update method using UITK schedulers, look it up in the docs. you basically create a scheduler that can call a method looped and start at any given time.

I do it by using one of these methods: one is to suscribe to an event, the second is to use a setter. I’ve never heard of schedulers before.

     private UnityEvent<int> scoreChanged;
     ...
     scoreChanged = new UnityEvent<int>();
     ....
     // when the score changes:
     scoreChanged.Invoke(newValue);

     // the other script adds a listener
     private VisualElement scoreLabel;
    ...
     scoreChanged.AddListener(scoreChange);
     ...
     private void scoreChange(int newValue) {
         scoreLabel.text = newValue.ToString();
     }
private VisualElement scoreLabel;
private int score;
public int Score {
   get {
        return score;
   }
   set {
      score = value;
      scoreLabel.text = score.ToString();
   }
}

...
// setting Score will update the label
 Score  = newValue;

You can also use both methods at the same time.

Are you able to provide a code example of this?

The absence of examples in the docs makes it really unclear how this potentially useful feature is meant to be used.