Strange behaviour of script controlled Line2D when moving Scene Root node

Godot Version

4.2.1

Question

I have a Scene, that looks like this:
Screenshot 2023-12-21 112358

The script on the Line2D node looks like this:

@tool
extends Line2D

@export var control_nodes: Array[Node2D]

func _process(delta:float) -> void:
	self.clear_points()
	
	for node in control_nodes:
		self.add_point(node.global_position)

As you can see, it just draws a line along the controll nodes.

If i now move the Line2D or the ControllNodes in the scene around the line always moves the same distance as the node i move (this is what i expected) but if i move the Root, than the lines move twice the distance (but i expected the line to also move the same distance as the Root).

What could be the reason for that?

Points in the line2D are local (in other words relative to the line2d itself), while you are using global positions of control nodes. It works only if root has position of (0, 0), any other value will shift line2d points.

OK, so in my simplified example it would work, to just use “node.position” instead of “node.global_position”.
But what if i have a scenario like this:

Screenshot 2023-12-21 130239

where the nodes to controll the line are children of each other?

In this case, just using “node.position” wouldn’t give me the correct positions. Is there a possibility (maybe a workaround) for the line go along the control points even if the scene root is moved and the controll nodes are children of each other?

What I’m saying is that you need to convert nodes’ global positions to line2d local space. In that case tree structure will be completely irrelevant.

Sorry, maybe im just dumb but didn’t I do exactly that, when I add a point at the global position of the node?

@tool
extends Line2D

@export var control_nodes: Array[Node2D]

func _process(delta:float) -> void:
	self.clear_points()
	
	for node in control_nodes:
		self.add_point(to_local(node.global_position))