Drawing on additional viewports

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

Hello!

I have simple tree:

Node2d
+ViewportContainer
++Viewport

I set margins(right=512,bottom=600) of the VieportContainar to cover half of the screen. Size of viewport from the tree is 512x600. Size of main viewport is 1024x600. I attached script to Node2d in which I draw a rectangle:

extends Node2D
func _draw():
draw_rect(Rect2(Vector2(0,300),Vector2(1000,400)),Color(1,0.0,0.0,0.2))

I wander why this rectangle is drawn only on the area autside of aditional Viewport. How can I draw something on this aditional Viewport? How get access to the space of this Viewport to use Physics2DShapeQueryParameters. I dug through documentation, but clearly I don’t understand something important.

What are you trying to achieve btw?

Reloecc | 2020-09-17 07:11

:bust_in_silhouette: Reply From: Reloecc

func draw() is meant to be used to create an art for a node. Like… if you want to create sprite / texture / image for a node, you use draw(). It’s called only once on the app start by default (you may force to redraw() by code if desired). It’s not really meant to continuously render content.

Reason your rectangle is drawn outside of your viewport is that you call draw in you Node2D (root parent). The texture of that node is being drawn, but your viewport’s camera is probably looking at empty space in your scene.