Skip to content

Commit

Permalink
Add DUPLICATE_NODE_REFERENCES flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickeon committed Sep 18, 2023
1 parent 4df80b0 commit 70843fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/classes/Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,9 @@
Duplicate using instancing.
An instance stays linked to the original so when the original changes, the instance changes too.
</constant>
<constant name="DUPLICATE_NODE_REFERENCES" value="16" enum="DuplicateFlags">
Duplicate the node's references, changing all exported [Node] properties to be part of the newly duplicated hierarchy, if possible. Does not work without [constant DUPLICATE_USE_INSTANTIATION].
</constant>
<constant name="INTERNAL_MODE_DISABLED" value="0" enum="InternalMode">
Node will not be internal.
</constant>
Expand Down
13 changes: 13 additions & 0 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,18 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c

Variant value = N->get()->get(name).duplicate(true);

if (p_flags & DUPLICATE_NODE_REFERENCES &&
E.type == Variant::OBJECT &&
ClassDB::is_parent_class(E.class_name, SNAME("Node"))) {
Node *node_from_property = Object::cast_to<Node>(value);
if (node_from_property) {
NodePath relative_path = get_path_to(node_from_property);

current_node->set(name, node->get_node(relative_path));
continue;
}
}

if (E.usage & PROPERTY_USAGE_ALWAYS_DUPLICATE) {
Resource *res = Object::cast_to<Resource>(value);
if (res) { // Duplicate only if it's a resource
Expand Down Expand Up @@ -3466,6 +3478,7 @@ void Node::_bind_methods() {
BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANTIATION);
BIND_ENUM_CONSTANT(DUPLICATE_NODE_REFERENCES);

BIND_ENUM_CONSTANT(INTERNAL_MODE_DISABLED);
BIND_ENUM_CONSTANT(INTERNAL_MODE_FRONT);
Expand Down
3 changes: 2 additions & 1 deletion scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ class Node : public Object {
DUPLICATE_GROUPS = 2,
DUPLICATE_SCRIPTS = 4,
DUPLICATE_USE_INSTANTIATION = 8,
DUPLICATE_NODE_REFERENCES = 16,
#ifdef TOOLS_ENABLED
DUPLICATE_FROM_EDITOR = 16,
DUPLICATE_FROM_EDITOR = 32,
#endif
};

Expand Down

0 comments on commit 70843fc

Please sign in to comment.