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():
func _physics_process(delta):
	var p = get_parent().global_position
	if len(points) > max_length:

Scene tree is as follows:


  • Line2D
  • Polygon2D

Or I can create the scene tree this way:


  • 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