The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+3 votes

When "Z As Relative" is checked on for a Node2D, its Z-Index is computed relatively to its parent's. So if I have a scene tree like this

  • NodeA: Z-Index = 2

    • Node B: Z-Index = 3

Node B's Z-Index is 2+3=5.

However, in a much more complicated tree, it might be a bit complex to compute that absolute Z-Index. Is there a built-in way to obtain that absolute value as the engine would calculate it?

in Engine by (35 points)
edited by

1 Answer

+4 votes

For now, here's what I'm using:

static func get_absolute_z_index(target: Node2D) -> int:
    var node = target;
    var z_index = 0;
    while node and node.is_class('Node2D'):
        z_index += node.z_index;
        if !node.z_as_relative:
        node = node.get_parent();
    return z_index;

It's not thoroughly tested, but I fixed all bugs I noticed and tried to prevent any I could anticipate.

by (35 points)

Excellent! This allowed me to switch a node to using global Z index so I could reparent it safely.

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.