0 votes

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?

in Engine by (12 points)
edited by

3 Answers

0 votes

_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)
by (3,259 points)

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!

0 votes

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

by (16 points)

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)
0 votes

would like to get some help on this too

overriding set_position seem to be never called at all

by (25 points)
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.