Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give the get_parent( ) function an optional int parameter to specify how many times it should be used (like get_node( "../../" ) ) #4687

Closed
roby405 opened this issue Jun 18, 2022 · 1 comment

Comments

@roby405
Copy link

roby405 commented Jun 18, 2022

Describe the project you are working on

A text editor app

Describe the problem or limitation you are having in your project

Because the UI in the app is getting more complex, I often have a hierarchy of 7+ nodes. While working with buttons, I sometimes need to use the get_parent( ) function 3 times in a row to get to the node I want to use, which makes the code look harder to read.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add an optional int parameter to the get_parent( ) function that specifies how many times in a row you want to use the function. That will help make the code cleaner and easier to read. The function should accept values >=1.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Let's take a scene with this hierarchy:
/root/Node1/Node2/Node3/Node4/Node5

Node5 has the node5.gd script attached.
If we want to get the name of the node three ranks higher in the node hierarchy (Node2), in node5.gd we would have the code:

func foo():
    return get_parent(3).name

instead of the more complicated:

func foo():
    return get_parent().get_parent().get_parent().name

It's the equivalent of get_node("../../../")

If this enhancement will not be used often, can it be worked around with a few lines of script?

It can be worked around with a recursive function:

func repeat_get_parent(times : int, node):
    if times > 1:
        return repeat_get_parent(times-1, node.get_parent())
    elif times == 1:
        return node.get_parent()

Is there a reason why this should be core and not an add-on in the asset library?

get_parent( ) is a commonly used function and I think having an optional parameter to make it easier and cleaner in code to do certain tasks should be a great addition to many Godot users.

@AaronRecord
Copy link

AaronRecord commented Jun 18, 2022

This sounds like it would encourage bad practices, see https://docs.godotengine.org/en/stable/tutorials/best_practices/scene_organization.html#how-to-build-relationships-effectively

I would export a node path on the child node instead (would be nicer with #1048), or maybe use a scene unique node (available in version 3.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants