0 votes

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?
Why

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
Godot version 3.2.3
in Engine by (16 points)
edited by

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.

1 Answer

+1 vote

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.
by (355 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.