Problem: set_as_toplevel() changes the drawing order of Line2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gogodot77

I have a simple trail effect. I have a script attached to Line2D as follows:

extends Line2D

export (int) var max_length = 20

func _ready():
	set_as_toplevel(true)
	    	
func _physics_process(delta):
	var p = get_parent().global_position
	add_point(p)
	if len(points) > max_length:
		remove_point(0)

Scene tree is as follows:

KinematicBody2D

  • Line2D
  • Polygon2D

Or I can create the scene tree this way:

KinematicBody2D

  • Polygon2D
  • Line2D

It makes no difference which way I construct the scene tree. Line2D always renders on top of the Polygon2D.

When I comment out the line with set_as_toplevel(true), the problem is fixed (i.e., the draw order is respected - the Line2D is drawn either in front of or behind Polygon2D). But I need set_as_toplevel(true) to make the transformations work.

Why does this happen? How do I fix this?

Edits: Clarity.

Have you tried changing the Z index of the nodes?

sirdorius | 2023-06-23 17:28