Collisions only registering for newest created CollisionShape2D

Godot Version: v4.2.1

Context: This is just a simple game where a ball (player) (RigidBody2D) has to avoid falling squares. The issue im facing involves lines being drawn in between these squares.

I have a “line” scene. This line is a staticbody2d, with a CollisionShape2D(segment) and a Line2D as children. This line scene is meant to be drawn between two squares, and if the player collides with it, they get more points.

When only one line is drawn on screen, the code works as intended: the ball collides with the line, the line queue_free()'s itself, etc… However, once multiple lines are on the screen, for some reason, only the most recently created line’s collision shape actually registers collisions. So the lines are all drawn fine, its just the collision of these that are causing issues for me.

To make matters weirder, I have print statements for each one of the line scenes, giving the coordinates of both the Line2D and the CollisionShape2D :

Line: [(92.02167, 1399.412), (126.6429, 537.8404)]
Collision: (92.02167, 1399.412)(126.6429, 537.8404)

Line: [(615.0239, 531.8786), (46.34997, 955.9678)]
Collision: (615.0239, 531.8786)(46.34997, 955.9678)

Line: [(227.3602, 156.4796), (615.0239, 531.8786)]
Collision: (227.3602, 156.4796)(615.0239, 531.8786)

As seen above, each of the lines indicate that the Line2D’s coordinates match up perfectly with their respective CollisionShape2D’s. However, again, only the newest created line’s collision actually registers.

This is the code i have on my game scene:

var should_draw_line = randi_range(1, 2)

if should_draw_line == 1 and shape_list.size() >= 3:

shape_one = shape_list[-1]
shape_two = shape_list[-2]
line = line_node.instantiate()
add_child(line)
line_shape_array.append([line, shape_one, shape_two])

It chooses the last two shapes (squares) added to the scene, and adds a line to the scene. This all gets bundled up in an array inside an array.

Next, it passes on to this update_line() function that gets called every process:

func update_lines():
for line_and_shapes in line_shape_array:
var line_node = line_and_shapes[0]
var _shape_one = line_and_shapes[1]
var _shape_two = line_and_shapes[2]

if _shape_one != null and _shape_two != null and line_node != null:

  var first_point = _shape_one.position
  var second_point = _shape_two.position
  line_node.update_line_and_collision(first_point, second_point)

else:
var index = line_shape_array.find(line_and_shapes)
line_shape_array.remove_at(index)
if line_node != null:
line_node.queue_free()

It unpacks every line_and_shapes array inside the line_shape_array. line_and_shapes contains the 3 elements necessary for drawing the line. I get the position of each shape, and call a function on the line scene update_line_and_collision() :

func update_line_and_collision(first_point, second_point):
line_2d.points = [first_point, second_point]
line_collision.shape.a = first_point
line_collision.shape.b = second_point

 print()
 print("Line: ", line_2d.points)
 print("Collision: ", line_collision.shape.a, line_collision.shape.b)

Where in this code, the Line2D and the CollisionShape2D get updated every process.

I have no idea where to even begin solving this. Ive been trying for the past couple of hours, but to no avail. And have even less clue about what to search to find relevant posts (if any). I would appreciate any insight. Thank you in advance.

You should turn on debug collision shapes to visually see the collision shape in the view.

My best guess would be the classic local/global position mistakes. You are probably intending to set a global screen position. And you place the child node location in the parent (rendered line) then you add a child node collision shape with the same global position in the local position of the line. So instead of aligned with the line it’s offset by a factor of two.

Anyway I don’t have the energy to read the code you provided, but skimming it my guess is most of it isn’t relevant to the issue.

The other thing it might be, could be related to resource instances, but let’s just get the debug shapes going first.

Your second guess was right it was an issue with my resources.

For anyone seeing this in the future:
On my line scene, i locate my CollisionShape2D (in the heiarchy). Right click on it, then Sub-resources > Shape
Once on this shape resource, turn “local to scene” on. Solves everything.