0 votes

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.

asked Apr 11, 2018 in Engine by Diet Estus (1,504 points)

1 Answer

+3 votes
Best answer

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.

http://docs.godotengine.org/en/3.0/classes/class_node2d.html?#member-variables

answered Apr 11, 2018 by eons (7,786 points)
selected Apr 11, 2018 by Diet Estus

Thank you for this! Worked beautifully

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.