to_local ?
Asked Answered
G

3

0

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!")
Gamez answered 24/1 at 14:22 Comment(0)
J
0

Gamez 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.

Jarietta answered 24/1 at 15:25 Comment(0)
S
0

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

Scarcity answered 24/1 at 17:50 Comment(0)
E
0

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”.

Evaluate answered 25/1 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.