+1 vote

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 (27 points)
edited by

1 Answer

+1 vote

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:
            break;
        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 (27 points)
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.
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 webmaster@godotengine.org with your username.