Getter for Vector2 not working

Question

var a := 100
var pos: Vector2:
  get:
    return Vector2(a, a)

var pressed := false

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == MOUSE_BUTTON_LEFT && event.is_pressed():
            if pos.x == 100:
                pressed = true

Here when I access pos inside _input it is returning Vector2(0, 0) rather Vector2(100, 100). I have set breakpoint at last if and inspected the value of pos

The getter is working, it returns a new Vector2 with x and y being 100. You do not actually assign this new vector to pos, so of course the underlying variable still is (0, 0). If you just do print(pos.x) you will see it indeed returns 100. If you want to have pos to have the value (100, 100), you have to assign it.

3 Likes