+1 vote

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?

in Engine by (1,877 points)
edited by

1 Answer

+3 votes
Best answer

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.

by (4,014 points)
selected by

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.