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

Add a Node.node_added signal/method to detect the addition of children and subchildren nodes #2544

Closed
Tracked by #15
ghsoares opened this issue Apr 2, 2021 · 5 comments · Fixed by godotengine/godot#57541
Milestone

Comments

@ghsoares
Copy link

ghsoares commented Apr 2, 2021

Describe the project you are working on

A project based of components added to a node

Describe the problem or limitation you are having in your project

In my project, a custom node can have multiple components that describes it's behaviour, each component can be added as the direct child or subchild of this node. I need a way to easily tell this node that a child or subchild node was added, even if not inside tree yet, something that SceneTree.node_added cannot do.

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

The easiest way I could think to tell the node that a direct/indirect child node was added, was to use some signal or virtual method. Something like Node.node_added(node: Node, parent: Node). This could be the same to when a direct/indirect child node was removed, something like Node.node_removed(node: Node, prevParent: Node), the second parameter of the signal/method would be the previous parent of the removed node. It's more interesting to be a virtual method, so it could be called even when is not added to the tree yet.

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

Let's say that I have this tree:
image
And in some point, a node called Node6 is added to Node5, and I want to tell the Node1 that this new node was added, then from SceneTree would call a notification, signal or method to tell the parents and grandparents that this node was added. The same would be if this node was removed from the tree.

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

I'm not sure if there's a better way to do this, but it would be a nice addition to the engine IMO.

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

Because it requires to modify the core structure of a Node, to be able to add this behaviour.

@Calinou
Copy link
Member

Calinou commented Apr 2, 2021

Related to/duplicate of #627?

@ghsoares
Copy link
Author

ghsoares commented Apr 2, 2021

@Calinou It's not duplicate, this issue talks about the node of a group is added in the tree, my proposal talks about the node that is added to another node as child/subchild, the use cases from this proposal and mine are different.

@Xrayez
Copy link
Contributor

Xrayez commented Apr 2, 2021

Closely related to #724 (this proposal suggests adding a method as an alternative), but could be considered a duplicate for organization purposes. The use cases seem to be different, though.

It's more interesting to be a virtual method, so it could be called even when is not added to the tree yet.

See my proposed solution to this (and current workaround): godotengine/godot#43729.

@Shadowblitz16
Copy link

C# can't override add_child or remove_child and I would like to use a signal like this for laying out children in a grid.

@Calinou Calinou changed the title Add a "Node.node_added" signal/method to detect the addition of children and subchildren nodes Add a Node.node_added signal/method to detect the addition of children and subchildren nodes Aug 8, 2021
@cloewen8
Copy link

cloewen8 commented Nov 8, 2021

I have a use case for this. I'm working on an inventory system. It works by having items directly parented to it. When an item is "picked up" I need to invoke a method in the item. My current workaround is to call this method manually. It would be great to have the inventory do this automatically using node_added, so I know it is always called.

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

Successfully merging a pull request may close this issue.

6 participants