Is there a way to specify draw layer at sprite level?

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

I know that CanvasLayers have alevel attribute which determines the order they are drawn.

Further, I know that Godot renders things according to their order in the scene hierarchy, with lower nodes being drawn on top of higher nodes.

But I am wondering if there is a way to specify the draw level on an object-by-object basis?

For example, suppose I have two sprites at different positions in my scene hierarchy and I want to draw the one higher in the hierarchy on top of the lower one. Is there a way to do this? For instance, can I use code to specify the draw level of the sprite higher in the tree such that it is drawn above the sprite lower in the tree?

The reason I ask is because I have a complex tree structure in which I use nodes as containers for my game objects. For example, I have a node called Enemies into which enemy nodes are placed during the generation of levels. I also have a node for Effects, Hazards, etc. and depending on the effect or hazard, I may want it to be drawn on a different level from other effects or hazards.

:bust_in_silhouette: Reply From: eons

You can use z_index property from Node2D (also known as z order), that defines the “layer” or draw order for the nodes, it can be set global or relative to the parent.

Thank you for this! Worked beautifully

diogenesthefool | 2020-04-14 16:28