Can someone explain me what does the to_local do? I read the description but I still don't understand.

func _input(event):
    if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
        if get_rect().has_point(to_local(event.position)):
            print("A click!")
  • Toxe replied to this.

    Hattu to_local() transforms a global position to a position in the local coordinate system of a node, meaning it will be relative to the nodes origin point.

    If you have a node at global position 100/200 and transform the global position 150/350 to the local coordinate system of that node you would get a local position of 50/150.

    To add to @Toxe's explanation, the function takes into account node's rotation and scaling as well, i.e. its complete transformation matrix.

    The key here is that to_local is a method of self (the Node2D or Node3D your script is in). So it’s really short for “self.to_local”, which explains why its behavior is going to depend on “self”.