Set a Node2D or a Control to show behind the root viewport

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

Hello:

How can I set a Node2D or a Control to show behind the root viewport? I have some items that I only want to display if there’s nothing shown in the root viewport at that location. I already know to do get_viewport().transparent_bg=true, but for the life of me I can’t figure out how to use Node2D’s z-indices or Control’s show behind or show on top features to get them to display behind the root viewport.

How do skyboxes do it, for example?

:bust_in_silhouette: Reply From: gamepad_coder

Use Canvas Layers.


Here’s a short clean tutorial vid on YouTube.

Godot Tutorial - How To Set CanvasLayer’s Layer Using GDScript Code (No Commentary) by user “Making Games And More”


Relevant part from this doc page :

Viewport children will draw by default at layer “0”, while a CanvasLayer will draw at any numeric layer. Layers with a greater number will be drawn above those with a smaller number. CanvasLayers also have their own transform and do not depend on the transform of other layers. This allows the UI to be fixed in screen-space while our view on the game world changes.

An example of this is creating a parallax background. This can be done with a CanvasLayer at layer “-1”. The screen with the points, life counter and pause button can also be created at layer “1”.

Blockquote