-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Expose Node
callbacks for adding and removing children
#43729
Conversation
Oh well perhaps I'm simply over-engineering this, because it's possible to "override" func add_child(child: Node, legible:bool = false) -> void:
.add_child(child, legible)
print("New child added: " + str(child))
func remove_child(child: Node):
.remove_child(child)
print("Existing child removed: " + str(child)) But as you see, this (talking about Godot 3.2):
Also, I'm not sure whether that's only GDScript's feature, or even a bug as a feature, due to things like #42968 (I've completely forgot about this behavior even after working on this, duh 😕). Anyways, that's what C++ part of the engine provides, that's how I've discovered this in fact, so perhaps these can also be exposed to scripting, for:
|
Interesting, didn't know these were available to override. That'll be a great work around. Tested this build and it doesn't work for all nodes. Not 100% of how the virtual definitions work here, but these methods seem to exist for Node specific CPP implementations. Things like Control appear to have their own handling of it. I may of missed something, but it didn't appear as though Lines 438 to 447 in 5b99365
When looking at this, I was thinking it might be easiest to make a new signal and add it as the last line in these functions: Lines 1165 to 1182 in 5b99365
Something like:
This would also avoid the |
|
The reason why I went for approach with exposing callbacks is the previous rejection/concern of the idea that But yeah the use case is also described in #12224. |
Not sure what you mean, is this speculative, or are you referring to a use case that you measured?
Tested, appears to be working now. |
I haven't tried to implement per-node I'll amend the PR description to say it only helps your proposal then, because it seems like we might have different use cases. |
My main use case with this PR would be getting notified of children added when a scene is istantiated, so that each node in the istanced scene receives all the relevant notification for all of their children, top to bottom. |
@samdze if I understand correctly, then yes, If what you ask for is getting notification to scene root, the closest thing you can use is |
71cb8d3
to
c58391c
Compare
We just merged a PR which solves the two related proposals with new signals: #57541. That being said we discussed this PR in a PR review meeting too and it seems still relevant to expose the existing Node |
Thanks, this PR would be nice to implement, but it's possible to workaround it, especially with #57541. If someone feels motivated enough, feel free to build upon this PR, as I don't currently use |
Superseded by #62661. |
Helps godotengine/godot-proposals#724.
Helps godotengine/godot-proposals#2544.
I've found out that the engine already has such mechanism through virtual
add_child_notify
,remove_child_notify
, and evenmove_child_notify
(currently used only on C++ level forControl
nodes such asGraphEdit
).With this, you don't have to:
get_tree().connect("node_added", self, "_on_node_added")
and filter nodes withget_parent()
checks._process()
.This way, there's no need to introduce signals such as
node_added
/child_added
, and instead use existing callbacks.I don't know the performance impact behind
get_script_instance()
checks yet, but perhaps they're negligible enough for this feature to be implemented. The_process
callback checks this every frame in contrast, so I think there's nothing to worry about performance-wise.Usage
This is mostly useful for implementing general-purpose, adaptable classes to handle the API written by other developers, or when better encapsulation is desired (parent manages/controls children, and not vice versa, where
get_parent()
usage is typically discouraged).I made this for 3.2 currently, 4.0 needs a separate PR due to removal of multilevel calls, but only if the idea behind godotengine/godot-proposals#724 is approved of course.
Test project:
expose_node_callbacks_add_remove.zip