Does a setter get used if assignment is made within the same script/class?

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

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)
    #...
:bust_in_silhouette: Reply From: 2D

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
    #...

Ah, thanks.

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

woopdeedoo | 2018-05-17 15:26