setter is just call setter function when you try to set value
value += 10 become set_value(value + 10)
what you do inside is not care by setter, you can do nothing, print, increase, decrease, anything what you want
so, you need something like
var value : int :
set(_value):
value += _value
print(value)
Oh! so if I don’t use the setter, the line value += 10 will modify the var value. BUT if I use the setter, it just passes the data to the setter and will only do that, so I need to specify that it needs to modify the var value.
Would you mind if I ask, did you ever encountered a problem when you want to call a setter then a getter is actually called for some reason?
I have a code
var health := 500.0:
get:
if health > 60.0:
return int(health)
else:
return health
set(new_value):
health = clampf(new_value, 0, max_health)
if health == 0:
died.emit()
hp_changed.emit(health, max_health)
func _on_timer_timeout() -> void:
if gm.timer_running:
health - 0.1
print("timer timeout")
and I am having issue that when the
health - 0.1
gets read, it calls the getter of the health variable and not the setter.
I would understand if it wanted to read the value before doing anything but this way I just don’t understand why it’s doing it. I also tried moving the setter above the getter, as someone elsewhere said but that did not helped.
Thank you for pointing me the right way. What you wrote make in fact call the setter but for some reason, unknown to me some weird duplication started to happen.
First call: Health would be decreased by 1
Second: By 2
Third: By 5…