+2 votes

Is it possible draw stuff using the _draw() method above every other object?
I tried to draw some rectsangles but they were hidden behind an instanced object I spawned during runtime. But I want all my drawn shapes to be always at the top of everything.

in Engine by (217 points)
edited by

2 Answers

+2 votes
Best answer

There are a few options you might do:

  1. Move drawing to a child node, and change it's Z-index.
  2. Use VisualServer::canvas_item_create and similar functions to create a non-existent (in the scene tree) canvas item, whose Z-index you might change.
  3. Toggle Show Behind Parent on those child instanced nodes...
by (1,586 points)
selected by

Thank you. I totally forgot about the show behind parent option. This is already all I need.

0 votes

here how can you draw with directy with VisualServer -> https://habr.com/ru/post/553742/

extends Node2D

onready var drawer = $"../Drawer"

var counter = 0

func custom_draw_line(start, goal, color, width=1.0, antialising=false):
    VisualServer.canvas_item_add_line(drawer.get_canvas_item(), start, goal, color, width, antialising)

func _process(delta):
    if Input.is_action_just_pressed("mouse_left"):
        counter += 2
        custom_draw_line(Vector2(100, 100)+Vector2(counter, counter), Vector2(300, 150)+Vector2(counter, counter), Color.green)

another example:

extends Node2D

onready var drawer = $"../Drawer"

var counter = 0

var surface

func _ready():
    surface = VisualServer.canvas_item_create()
    VisualServer.canvas_item_set_parent(surface, drawer.get_canvas_item())

func custom_draw_line(start, goal, color, width=1.0, antialising=false):
    VisualServer.canvas_item_add_line(surface, start, goal, color, width, antialising)

func _process(delta):
    if Input.is_action_just_pressed("mouse_left"):
        counter += 2
        custom_draw_line(Vector2(100, 100)+Vector2(counter, counter), Vector2(300, 150)+Vector2(counter, counter), Color.green)
    elif Input.is_action_just_pressed("mouse_right"):
        VisualServer.canvas_item_clear(surface)
        counter = 0
by (69 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.