Z-value for custom draw calls

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PixelWizzard
:warning: Old Version Published before Godot 3 was released.

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.

:bust_in_silhouette: Reply From: Bojidar Marinov

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…

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

PixelWizzard | 2016-03-18 15:07

:bust_in_silhouette: Reply From: boruok

here how can you draw with directy with VisualServer → Godot — рисование без правил / Хабр

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