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.

in Engine by (1,604 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.


by (7,934 points)
selected by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.