How to override a variable's setter in Godot 4?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MadTinkerer

I found an answer for this question for 3.x, but I can’t find an answer for this in 4.x.

In GDScript I have a child class of ProgressBar I’m calling GradientBar because I want the “fill” StyleBox to change bg_color when the GradientBar’s value is changed. Changing the color is not the problem, but I can’t figure out the syntax to have value’s setter overidden.

If you’re defining your own variable it’s simple:

var own_variable : float:
   get:
      return own_variable
   set(amount):
      some_code()
      own_variable = amount      

But if you try to do that with a variable of a parent class the IDE says something like “Member “value” redefined (original in native class ‘ProgressBar’)”.

I’m surprised there isn’t an answer to this. I guess I’ll just have to connect the “value changed” signal to itself like a barbarian.

MadTinkerer | 2022-11-19 16:53

:bust_in_silhouette: Reply From: Toothgapsy

You need to define it via the _set()method like:

See Can not override setters / getters · Issue #40288 · godotengine/godot · GitHub