From 25f0e0ac9129da185bc7522d462fd57c37e7688a Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 24 Jan 2022 14:50:03 +0100 Subject: [PATCH] Add DUPLICATE_INTERNAL_STATE flag --- doc/classes/Node.xml | 11 +++++++++++ scene/main/node.cpp | 6 ++++-- scene/main/node.h | 4 +++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index 6fddc0f44ec..ba7617f4a87 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -280,6 +280,7 @@ Duplicates the node, returning a new node with all of its properties, signals, groups, and children copied from the original. The behavior can be tweaked through the [param flags] (see [enum DuplicateFlags]). Internal nodes are not duplicated. [b]Note:[/b] For nodes with a [Script] attached, if [method Object._init] has been defined with required parameters, the duplicated node will not have a [Script]. + [b]Note:[/b] By default, this method will duplicate only properties marked for serialization (i.e. using [constant @GlobalScope.PROPERTY_USAGE_STORAGE], or in GDScript, [annotation @GDScript.@export]). If you want to duplicate all properties, use [constant DUPLICATE_INTERNAL_STATE]. @@ -1378,6 +1379,16 @@ Duplicate using [method PackedScene.instantiate]. If the node comes from a scene saved on disk, reuses [method PackedScene.instantiate] as the base for the duplicated node and its children. + + Duplicate also non-serializable variables (i.e. without [constant @GlobalScope.PROPERTY_USAGE_STORAGE]). + + + Duplicate using default flags. This constant is useful to add or remove a single flag. + [codeblock] + # Duplicate non-exported variables. + var dupe = duplicate(DUPLICATE_DEFAULT | DUPLICATE_INTERNAL_STATE) + [/codeblock] + The node will not be internal. diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 28e6f297b18..a4b3a7ead5c 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -3012,7 +3012,7 @@ void Node::_duplicate_properties(const Node *p_root, const Node *p_original, Nod } } for (const PropertyInfo &E : props) { - if (!(E.usage & PROPERTY_USAGE_STORAGE)) { + if (!(p_flags & DUPLICATE_INTERNAL_STATE) && !(E.usage & PROPERTY_USAGE_STORAGE)) { continue; } const StringName name = E.name; @@ -3800,7 +3800,7 @@ void Node::_bind_methods() { ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree); ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween); - ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANTIATION | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS)); + ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_DEFAULT)); ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false)); ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder); @@ -3955,6 +3955,8 @@ void Node::_bind_methods() { BIND_ENUM_CONSTANT(DUPLICATE_GROUPS); BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS); BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANTIATION); + BIND_ENUM_CONSTANT(DUPLICATE_INTERNAL_STATE); + BIND_ENUM_CONSTANT(DUPLICATE_DEFAULT); BIND_ENUM_CONSTANT(INTERNAL_MODE_DISABLED); BIND_ENUM_CONSTANT(INTERNAL_MODE_FRONT); diff --git a/scene/main/node.h b/scene/main/node.h index 9bc0100914b..daa901c39fe 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -100,8 +100,10 @@ public: DUPLICATE_GROUPS = 2, DUPLICATE_SCRIPTS = 4, DUPLICATE_USE_INSTANTIATION = 8, + DUPLICATE_INTERNAL_STATE = 16, + DUPLICATE_DEFAULT = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION, #ifdef TOOLS_ENABLED - DUPLICATE_FROM_EDITOR = 16, + DUPLICATE_FROM_EDITOR = 32, #endif };