How to put a draw_line over a sprite?

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

I think it is a simple question, but I cant draw a line over the sprite, and I dont know why. Can somebody help me?

I tried every possible hierarchy in scene options

This is my code

func _process(delta): 
update() 
pass
 
func _draw(): 	
if pos1 != Vector2(0,0): 		
 draw_line(pos1, get_global_mouse_position(), color, diametro, true) 
pass

func _on_Area2D_input_event(viewport, event, shape_idx): 	
if event is InputEventMouseButton: 	
if event.is_pressed(): 	
print("Clicked")
pos1 = get_global_mouse_position()	 	
pass

I got a way setting the sprite in the z-index -1. But Im dont sure if it’s the best way to do that.

SubBut | 2021-03-01 22:48

:bust_in_silhouette: Reply From: Jayman2000

Whether or not the line appears above the Sprite depends on:

  1. Which Node is doing the drawing.
  2. The Z index of the Nodes in the SceneTree.
  3. Where that Node is on the SceneTree.

If the scene looked like this,

  • Area2D (with your script attached to it). Z index = 0.
  • Sprite. Z index = 0.
  • CollisionShape2D. Z index = 0.

then its draw order would be:

  1. Area2D (this does nothing since Area2Ds are invisible).
  2. Area2D’s _draw() function (this draws the line).
  3. Sprite (this gets drawn on top of the line).
  4. CollisionShape2D (this does nothing since CollisionShape2Ds are invisible).

To fix your problem, we need to make sure that the Sprite is drawn before the line.

Potential solution 1: Change the Sprite’s Z index
A Node with a lower Z index gets drawn before one with a higher Z index. If the scene looked like this,

  • Area2D (with your script attached to it). Z index = 0.
  • Sprite. Z index = -1.
  • CollisionShape2D. Z index = 0.

then its draw order would be:

  1. Sprite.
  2. Area2D (again, this does nothing).
  3. Area2D’s _draw() function.
  4. CollisionShape2D (again, this does nothing).

Potential solution 2: Make the Sprite the parent
If multiple Nodes have the same Z index, then the one that’s higher on the list gets drawn first. If the scene looked like this,

  • Sprite. Z index = 0.
  • Area2D (with your script attached to it). Z index = 0.
    • CollisionShape2D. Z index = 0.

then its draw order would be:

  1. Sprite.
  2. Area2D.
  3. Area2D’s _draw() function.
  4. CollisionShape2D.

Potential solution 3: Attach the script to the Sprite
A Node’s _draw() function is run directly after the Node is drawn. If the scene looked like this,

  • Area2D. Z index = 0.
  • Sprite (with your script attached to it). Z index = 0.
  • CollisionShape2D. Z index = 0.

then its draw order would be:

  1. Area2D.
  2. Sprite.
  3. Sprite’s _draw() function.
  4. CollisionShape2D.
:bust_in_silhouette: Reply From: BananaBread

Add Line2D node to your scene. You can use this node to draw the line for you, just do:

$Line2D.points = PoolVector2Array(your_points)
…where your_points is of type Array and should contain Vector2s as points of your line (potentially multiline).

Because now you are drawing the line via a node, it obeys usual hierarchy rules. Or you can manually set the Z index of the node.