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 signal to notify when children nodes enter or exit tree #57541

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/classes/Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,18 @@
</member>
</members>
<signals>
<signal name="child_entered_tree">
<argument index="0" name="node" type="Node" />
<description>
Emitted when a child node enters the scene tree, either because it entered on its own or because this node entered with it.
</description>
</signal>
<signal name="child_exited_tree">
<argument index="0" name="node" type="Node" />
<description>
Emitted when a child node exits the scene tree, either because it exited on its own or because this node exited.
</description>
</signal>
<signal name="ready">
<description>
Emitted when the node is ready. Comes after [method _ready] callback and follows the same rules.
Expand Down
14 changes: 14 additions & 0 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ void Node::_propagate_enter_tree() {

data.tree->node_added(this);

if (data.parent) {
Variant c = this;
const Variant *cptr = &c;
data.parent->emit_signal(SNAME("child_entered_tree"), &cptr, 1);
}

data.blocked++;
//block while adding children

Expand Down Expand Up @@ -281,6 +287,12 @@ void Node::_propagate_exit_tree() {
data.tree->node_removed(this);
}

if (data.parent) {
Variant c = this;
const Variant *cptr = &c;
data.parent->emit_signal(SNAME("child_exited_tree"), &cptr, 1);
}

// exit groups

for (KeyValue<StringName, GroupData> &E : data.grouped) {
Expand Down Expand Up @@ -2865,6 +2877,8 @@ void Node::_bind_methods() {
ADD_SIGNAL(MethodInfo("tree_entered"));
ADD_SIGNAL(MethodInfo("tree_exiting"));
ADD_SIGNAL(MethodInfo("tree_exited"));
ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
ADD_SIGNAL(MethodInfo("child_exited_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));

ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_name", "get_name");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_file_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_file_path", "get_scene_file_path");
Expand Down