I don't understand Getters and Setters

Godot Version
v4.3.stable.official [77dcf97d8]

Question

So, playing around with setters and getters to understand how they work, I tried this simple code

extends Sprite2D

var value : int :
	set(value):
		print(value)

func _process(delta):
	value += 10

I expect the value in “value” to increase by 10 every frame in the console, but it gets stuck printing “10” every frame.

The previous code is not the same as writing this? or there is something I don’t understand.

extends Sprite2D

var value : int 

func _process(delta):
	value += 10
	print(value)

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)
1 Like

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.

Is this correct?

Exactly

Getters are similar, you just need to return value, but you can
return value or return 5, it only your choise

1 Like

You mean getters right? Yea, that makes sense, a way to modify the value in a var when you call it.

Thanks for the help!

yeah, typo

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.

When you just type health - 1, you are not actually setting the variable at all, you are just calculating a number which then gets thrown away.

To subtract one, and actually save that value, you can do either of these:

# normal way to set a variable
health = health - 1
# shorthand for setting a variable to itself minus something
health -= 1

The shorthand also works for +, *, and /.

I assume this will still call the getter, since it’ll need to know what value it’s subtracting from, but now it should also call the setter.

1 Like

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…

But what is the functional code is

health = -0.1

Anyway, thank you for pointing me the right way.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.