+3 votes

If I have:

var x setget _set_x

func some_func(x, y, z):
    self.x = x
    # ...

func _set_x(new_x):
    x = new_x
    # ...

Does the setter get called when assigning x inside some_func?
Or should I make an explicit call to _set_x, like:

func some_func(x, y, z):
    _set_x(x)
    #...
in Engine by (196 points)

1 Answer

+4 votes
Best answer

If you are inside the same file:

x = 1 #Will not go through the setter
self.x = 2 #Will go through the setter
_set_x(3) #Will go through the setter

If you are not inside the same file:

$theNode.x = 4 #Will go through the setter
$theNode._set_x(5) #Will go through the setter

In your function above:

func some_func(x, y, z):
    _set_x(x) #This will go through the setter
    #...
by (391 points)
selected by

Ah, thanks.

I noticed the documentation explains this but only sort of tangentially and without explicit mention that it's easy to overlook...

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 Frequently asked questions and 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 [email protected] with your username.