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_child() returns pointer to new child node #33926

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 12 additions & 8 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,23 +1169,25 @@ void Node::_add_child_nocheck(Node *p_child, const StringName &p_name) {
add_child_notify(p_child);
}

void Node::add_child(Node *p_child, bool p_legible_unique_name) {
Node *Node::add_child(Node *p_child, bool p_legible_unique_name) {

ERR_FAIL_NULL(p_child);
ERR_FAIL_COND_MSG(p_child == this, "Can't add child '" + p_child->get_name() + "' to itself."); // adding to itself!
ERR_FAIL_COND_MSG(p_child->data.parent, "Can't add child '" + p_child->get_name() + "' to '" + get_name() + "', already has a parent '" + p_child->data.parent->get_name() + "'."); //Fail if node has a parent
ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, add_node() failed. Consider using call_deferred(\"add_child\", child) instead.");
ERR_FAIL_NULL_V(p_child, nullptr);
ERR_FAIL_COND_V_MSG(p_child == this, nullptr, "Can't add child '" + p_child->get_name() + "' to itself."); // adding to itself!
ERR_FAIL_COND_V_MSG(p_child->data.parent, nullptr, "Can't add child '" + p_child->get_name() + "' to '" + get_name() + "', already has a parent '" + p_child->data.parent->get_name() + "'."); //Fail if node has a parent
ERR_FAIL_COND_V_MSG(data.blocked > 0, nullptr, "Parent node is busy setting up children, add_node() failed. Consider using call_deferred(\"add_child\", child) instead.");

/* Validate name */
_validate_child_name(p_child, p_legible_unique_name);

_add_child_nocheck(p_child, p_child->data.name);

return p_child;
}

void Node::add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name) {
Node *Node::add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name) {

ERR_FAIL_NULL(p_node);
ERR_FAIL_NULL(p_child);
ERR_FAIL_NULL_V(p_node, nullptr);
ERR_FAIL_NULL_V(p_child, nullptr);

add_child(p_child, p_legible_unique_name);

Expand All @@ -1194,6 +1196,8 @@ void Node::add_child_below_node(Node *p_node, Node *p_child, bool p_legible_uniq
} else {
WARN_PRINTS("Cannot move under node " + p_node->get_name() + " as " + p_child->get_name() + " does not share a parent.");
}

return p_child;
}

void Node::_propagate_validate_owner() {
Expand Down
4 changes: 2 additions & 2 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ class Node : public Object {
StringName get_name() const;
void set_name(const String &p_name);

void add_child(Node *p_child, bool p_legible_unique_name = false);
void add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name = false);
Node *add_child(Node *p_child, bool p_legible_unique_name = false);
Node *add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name = false);
void remove_child(Node *p_child);

int get_child_count() const;
Expand Down