[SOLVED] setget not working

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

Got a really weird issue, my setget methods aren’t being called:

var turn = 0.0 setget set_turn, get_turn

func set_turn(value):
	print("set_turn called")
	turn = clamp(value, -0.01, 0.01)

func get_turn():
	print("get_turn called")
	return turn

Nothing prints, my clamp isn’t run. The instance variable updates, but it doesn’t call the setter and getter methods.

The odd thing is I’ve got setgets in a singleton and they’re working fine but when I add them to node scripts they just don’t run. This is the second node script this has happened on now. I’ve got no errors or warnings in my project.

Am I missing something obvious here?

:bust_in_silhouette: Reply From: klaas

Hi,
be aware …

Whenever the value of variable is modified by an external source (i.e.
not from local usage in the class), the setter function (setterfunc
above) will be called.

… so it not get called when used from within the class itself. For that its good to call the setter function directly.

Ahhh, I see! I read “not local variable” in the docs and assumed they meant no for a variable declared within a method but yes for an instance method called from wherever. Only on external calls then. That’s fine.

I’ll mark this as solved. Thanks buddy!

DaddyMonster | 2020-09-01 18:26