+2 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)
    #...
asked May 15 in Engine by woopdeedoo (35 points)

1 Answer

+3 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
    #...
answered May 15 by 2D||!2D (315 points)
selected May 17 by woopdeedoo

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 How to use this Q&A? before posting your first questions.