built-in setters/getters not called as expected

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

The setter and getter functions for built-in objects don’t work as I would expect. According to the docs the setter for position is

set_position(value)

the following code:

extends Node2D


func _ready():
	self.position = Vector2(1, 1)
	print(position)
    # prints (1, 1)


func set_position(new_pos):
	position = Vector2(0, 0)

prints (1, 1) as an output, while I would have expected (0, 0). Compare the above code to

extends Node2D


var my_pos: Vector2 setget set_my_pos


func _ready():
	self.my_pos = Vector2(1, 1)
	print(my_pos)
    # prints (0, 0)


func set_my_pos(new_pos):
	my_pos = Vector2(0, 0)

It seems in the built-in case the setter is not ever called when making the assignment, even when accessed externally. In other experiments, I had similar results for the built-in getter functions.
Obviously this isn’t too hard to work around, but it is a bit confusing. Could someone please help me understand what’s going on?

:bust_in_silhouette: Reply From: raschi.exe

i think that is because you are printing in the ready func, basically what the engine does is setting the position to 1,1 , printing that and then it does the other stuff.
not sure tho

There is no other stuff to speak of here. The question is, why does it set the position to
(1, 1) when I have overwritten the setter to make it set position to (0, 0)

The code in the question isn’t meant to do anything; it just shows that the setter isn’t called for the line

self.position = Vector2(1, 1)

DunorbChops | 2020-10-08 06:28

:bust_in_silhouette: Reply From: Magso

_ready is called only the first frame so (1, 1) is correct. set_position() is a setget method for a property but you have wrote it as a custom function and not calling it anywhere.

func _ready():
    self.set_position(Vector2(0,0))
    print(position)
    #prints (0,0)

A custom function would look like this

func _ready():
    set_my_position(0,0)
    print(position)
    #prints (0,0)

func set_my_position(x, y):
    self.position = Vector2(x, y)

Thanks very much for the response, but I’m afraid I still don’t fully understand. Let me clarify a bit further. According to the docs the setter for position is

set_position(value)

so I would expect it to behave like a standard setter even when overwritten. Compare the code in the original post to the following:

extends Node2D


var my_pos: Vector2 setget set_my_pos


func _ready():
	self.my_pos = Vector2(1,1)
	print(my_pos)
    # prints (0,0)


func set_my_pos(new_pos):
	my_pos = Vector2(0,0)

This code is nearly identical to the original code, but instead of overwriting the built-in setter, it defines its own. I still don’t understand why the behavior is different.

I will edit the original question to be more clear shortly. Thanks!

DunorbChops | 2020-10-07 23:45

:bust_in_silhouette: Reply From: skeleton60

would like to get some help on this too

overriding set_position seem to be never called at all