summaryrefslogtreecommitdiff
path: root/scene/resources/packed_scene.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/resources/packed_scene.cpp')
-rw-r--r--scene/resources/packed_scene.cpp292
1 files changed, 138 insertions, 154 deletions
diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cpp
index 60cda637ca..402e67a0f1 100644
--- a/scene/resources/packed_scene.cpp
+++ b/scene/resources/packed_scene.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -34,10 +34,12 @@
#include "core/config/project_settings.h"
#include "core/core_string_names.h"
#include "core/io/resource_loader.h"
+#include "editor/editor_inspector.h"
#include "scene/2d/node_2d.h"
#include "scene/3d/node_3d.h"
#include "scene/gui/control.h"
#include "scene/main/instance_placeholder.h"
+#include "scene/property_utils.h"
#define PACKED_SCENE_VERSION 2
@@ -45,6 +47,30 @@ bool SceneState::can_instantiate() const {
return nodes.size() > 0;
}
+static Array _sanitize_node_pinned_properties(Node *p_node) {
+ if (!p_node->has_meta("_edit_pinned_properties_")) {
+ return Array();
+ }
+ Array pinned = p_node->get_meta("_edit_pinned_properties_");
+ if (pinned.is_empty()) {
+ return Array();
+ }
+ Set<StringName> storable_properties;
+ p_node->get_storable_properties(storable_properties);
+ int i = 0;
+ do {
+ if (storable_properties.has(pinned[i])) {
+ i++;
+ } else {
+ pinned.remove_at(i);
+ }
+ } while (i < pinned.size());
+ if (pinned.is_empty()) {
+ p_node->remove_meta("_edit_pinned_properties_");
+ }
+ return pinned;
+}
+
Node *SceneState::instantiate(GenEditState p_edit_state) const {
// nodes where instancing failed (because something is missing)
List<Node *> stray_instances;
@@ -227,7 +253,7 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
} else {
Node *base = i == 0 ? node : ret_nodes[0];
- if (p_edit_state == GEN_EDIT_STATE_MAIN) {
+ if (p_edit_state == GEN_EDIT_STATE_MAIN || p_edit_state == GEN_EDIT_STATE_MAIN_INHERITED) {
//for the main scene, use the resource as is
res->configure_for_local_scene(base, resources_local_to_scene);
resources_local_to_scene[res] = res;
@@ -289,6 +315,13 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
node->_set_owner_nocheck(owner);
}
}
+
+ // we only want to deal with pinned flag if instancing as pure main (no instance, no inheriting)
+ if (p_edit_state == GEN_EDIT_STATE_MAIN) {
+ _sanitize_node_pinned_properties(node);
+ } else {
+ node->remove_meta("_edit_pinned_properties_");
+ }
}
ret_nodes[i] = node;
@@ -320,15 +353,23 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
continue;
}
- Vector<Variant> binds;
- if (c.binds.size()) {
- binds.resize(c.binds.size());
- for (int j = 0; j < c.binds.size(); j++) {
- binds.write[j] = props[c.binds[j]];
+ Callable callable(cto, snames[c.method]);
+ if (c.unbinds > 0) {
+ callable = callable.unbind(c.unbinds);
+ } else if (!c.binds.is_empty()) {
+ Vector<Variant> binds;
+ if (c.binds.size()) {
+ binds.resize(c.binds.size());
+ for (int j = 0; j < c.binds.size(); j++) {
+ binds.write[j] = props[c.binds[j]];
+ }
}
+
+ const Variant *args = binds.ptr();
+ callable = callable.bind(&args, binds.size());
}
- cfrom->connect(snames[c.signal], Callable(cto, snames[c.method]), binds, CONNECT_PERSIST | c.flags);
+ cfrom->connect(snames[c.signal], callable, varray(), CONNECT_PERSIST | c.flags);
}
//Node *s = ret_nodes[0];
@@ -384,7 +425,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map
// save the child instantiated scenes that are chosen as editable, so they can be restored
// upon load back
- if (p_node != p_owner && p_node->get_scene_file_path() != String() && p_owner->is_editable_instance(p_node)) {
+ if (p_node != p_owner && !p_node->get_scene_file_path().is_empty() && p_owner->is_editable_instance(p_node)) {
editable_instances.push_back(p_owner->get_path_to(p_node));
// Node is the root of an editable instance.
is_editable_instance = true;
@@ -415,61 +456,22 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map
// with the instance states, we can query for identical properties/groups
// and only save what has changed
- List<PackState> pack_state_stack;
-
- bool instantiated_by_owner = true;
-
- {
- Node *n = p_node;
-
- while (n) {
- if (n == p_owner) {
- Ref<SceneState> state = n->get_scene_inherited_state();
- if (state.is_valid()) {
- int node = state->find_node_by_path(n->get_path_to(p_node));
- if (node >= 0) {
- //this one has state for this node, save
- PackState ps;
- ps.node = node;
- ps.state = state;
- pack_state_stack.push_back(ps);
- instantiated_by_owner = false;
- }
- }
+ bool instantiated_by_owner = false;
+ Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, p_owner, &instantiated_by_owner);
- if (p_node->get_scene_file_path() != String() && p_node->get_owner() == p_owner && instantiated_by_owner) {
- if (p_node->get_scene_instance_load_placeholder()) {
- //it's a placeholder, use the placeholder path
- nd.instance = _vm_get_variant(p_node->get_scene_file_path(), variant_map);
- nd.instance |= FLAG_INSTANCE_IS_PLACEHOLDER;
- } else {
- //must instance ourselves
- Ref<PackedScene> instance = ResourceLoader::load(p_node->get_scene_file_path());
- if (!instance.is_valid()) {
- return ERR_CANT_OPEN;
- }
-
- nd.instance = _vm_get_variant(instance, variant_map);
- }
- }
- n = nullptr;
- } else {
- if (n->get_scene_file_path() != String()) {
- //is an instance
- Ref<SceneState> state = n->get_scene_instance_state();
- if (state.is_valid()) {
- int node = state->find_node_by_path(n->get_path_to(p_node));
- if (node >= 0) {
- //this one has state for this node, save
- PackState ps;
- ps.node = node;
- ps.state = state;
- pack_state_stack.push_back(ps);
- }
- }
- }
- n = n->get_owner();
+ if (!p_node->get_scene_file_path().is_empty() && p_node->get_owner() == p_owner && instantiated_by_owner) {
+ if (p_node->get_scene_instance_load_placeholder()) {
+ //it's a placeholder, use the placeholder path
+ nd.instance = _vm_get_variant(p_node->get_scene_file_path(), variant_map);
+ nd.instance |= FLAG_INSTANCE_IS_PLACEHOLDER;
+ } else {
+ //must instance ourselves
+ Ref<PackedScene> instance = ResourceLoader::load(p_node->get_scene_file_path());
+ if (!instance.is_valid()) {
+ return ERR_CANT_OPEN;
}
+
+ nd.instance = _vm_get_variant(instance, variant_map);
}
}
@@ -478,88 +480,38 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map
List<PropertyInfo> plist;
p_node->get_property_list(&plist);
- StringName type = p_node->get_class();
- Ref<Script> script = p_node->get_script();
- if (Engine::get_singleton()->is_editor_hint() && script.is_valid()) {
- // Should be called in the editor only and not at runtime,
- // otherwise it can cause problems because of missing instance state support.
- script->update_exports();
- }
+ Array pinned_props = _sanitize_node_pinned_properties(p_node);
for (const PropertyInfo &E : plist) {
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
continue;
}
- String name = E.name;
- Variant value = p_node->get(E.name);
-
- bool isdefault = false;
- Variant default_value = ClassDB::class_get_default_property_value(type, name);
+ Variant forced_value;
- if (default_value.get_type() != Variant::NIL) {
- isdefault = bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value));
- }
-
- if (!isdefault && script.is_valid() && script->get_property_default_value(name, default_value)) {
- isdefault = bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value));
- }
- // the version above makes more sense, because it does not rely on placeholder or usage flag
- // in the script, just the default value function.
- // if (E.usage & PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE) {
- // isdefault = true; //is script default value
- // }
-
- if (pack_state_stack.size()) {
- // we are on part of an instantiated subscene
- // or part of instantiated scene.
- // only save what has been changed
- // only save changed properties in instance
-
- if ((E.usage & PROPERTY_USAGE_NO_INSTANCE_STATE) || E.name == "__meta__") {
- //property has requested that no instance state is saved, sorry
- //also, meta won't be overridden or saved
+ // If instance or inheriting, not saving if property requested so, or it's meta
+ if (states_stack.size()) {
+ if ((E.usage & PROPERTY_USAGE_NO_INSTANCE_STATE)) {
continue;
}
-
- bool exists = false;
- Variant original;
-
- for (List<PackState>::Element *F = pack_state_stack.back(); F; F = F->prev()) {
- //check all levels of pack to see if the property exists somewhere
- const PackState &ps = F->get();
-
- original = ps.state->get_property_value(ps.node, E.name, exists);
- if (exists) {
- break;
- }
- }
-
- if (exists) {
- //check if already exists and did not change
- if (value.get_type() == Variant::FLOAT && original.get_type() == Variant::FLOAT) {
- //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
- float a = value;
- float b = original;
-
- if (Math::is_equal_approx(a, b)) {
- continue;
- }
- } else if (bool(Variant::evaluate(Variant::OP_EQUAL, value, original))) {
- continue;
+ // Meta is normally not saved in instances/inherited (see GH-12838), but we need to save the pinned list
+ if (E.name == "__meta__") {
+ if (pinned_props.size()) {
+ Dictionary meta_override;
+ meta_override["_edit_pinned_properties_"] = pinned_props;
+ forced_value = meta_override;
}
}
+ }
- if (!exists && isdefault) {
- //does not exist in original node, but it's the default value
- //so safe to skip too.
- continue;
- }
+ StringName name = E.name;
+ Variant value = forced_value.get_type() == Variant::NIL ? p_node->get(name) : forced_value;
- } else {
- if (isdefault) {
- //it's the default value, no point in saving it
+ if (!pinned_props.has(name) && forced_value.get_type() == Variant::NIL) {
+ bool is_valid_default = false;
+ Variant default_value = PropertyUtils::get_property_default_value(p_node, name, &is_valid_default, &states_stack, true);
+ if (is_valid_default && !PropertyUtils::is_property_value_different(value, default_value)) {
continue;
}
}
@@ -585,10 +537,9 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map
*/
bool skip = false;
- for (const PackState &F : pack_state_stack) {
+ for (const SceneState::PackState &ia : states_stack) {
//check all levels of pack to see if the group was added somewhere
- const PackState &ps = F;
- if (ps.state->is_node_in_group(ps.node, gi.name)) {
+ if (ia.state->is_node_in_group(ia.node, gi.name)) {
skip = true;
break;
}
@@ -618,7 +569,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map
// Save the right type. If this node was created by an instance
// then flag that the node should not be created but reused
- if (pack_state_stack.is_empty() && !is_editable_instance) {
+ if (states_stack.is_empty() && !is_editable_instance) {
//this node is not part of an instancing process, so save the type
nd.type = _nm_get_string(p_node->get_class(), name_map);
} else {
@@ -635,7 +586,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map
bool save_node = nd.properties.size() || nd.groups.size(); // some local properties or groups exist
save_node = save_node || p_node == p_owner; // owner is always saved
- save_node = save_node || (p_node->get_owner() == p_owner && instantiated_by_owner); //part of scene and not instantiated
+ save_node = save_node || (p_node->get_owner() == p_owner && instantiated_by_owner); //part of scene and not instanced
int idx = nodes.size();
int parent_node = NO_PARENT_SAVED;
@@ -709,12 +660,32 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName
continue;
}
+ Vector<Variant> binds;
+ int unbinds = 0;
+ Callable base_callable;
+
+ if (c.callable.is_custom()) {
+ CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(c.callable.get_custom());
+ if (ccb) {
+ binds = ccb->get_binds();
+ base_callable = ccb->get_callable();
+ }
+
+ CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(c.callable.get_custom());
+ if (ccu) {
+ unbinds = ccu->get_unbinds();
+ base_callable = ccu->get_callable();
+ }
+ } else {
+ base_callable = c.callable;
+ }
+
//find if this connection already exists
Node *common_parent = target->find_common_parent_with(p_node);
ERR_CONTINUE(!common_parent);
- if (common_parent != p_owner && common_parent->get_scene_file_path() == String()) {
+ if (common_parent != p_owner && common_parent->get_scene_file_path().is_empty()) {
common_parent = common_parent->get_owner();
}
@@ -734,7 +705,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName
NodePath signal_from = common_parent->get_path_to(p_node);
NodePath signal_to = common_parent->get_path_to(target);
- if (ps->has_connection(signal_from, c.signal.get_name(), signal_to, c.callable.get_method())) {
+ if (ps->has_connection(signal_from, c.signal.get_name(), signal_to, base_callable.get_method())) {
exists = true;
break;
}
@@ -765,7 +736,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName
if (from_node >= 0 && to_node >= 0) {
//this one has state for this node, save
- if (state->is_connection(from_node, c.signal.get_name(), to_node, c.callable.get_method())) {
+ if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
exists2 = true;
break;
}
@@ -774,7 +745,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName
nl = nullptr;
} else {
- if (nl->get_scene_file_path() != String()) {
+ if (!nl->get_scene_file_path().is_empty()) {
//is an instance
Ref<SceneState> state = nl->get_scene_instance_state();
if (state.is_valid()) {
@@ -783,7 +754,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName
if (from_node >= 0 && to_node >= 0) {
//this one has state for this node, save
- if (state->is_connection(from_node, c.signal.get_name(), to_node, c.callable.get_method())) {
+ if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
exists2 = true;
break;
}
@@ -830,12 +801,16 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName
ConnectionData cd;
cd.from = src_id;
cd.to = target_id;
- cd.method = _nm_get_string(c.callable.get_method(), name_map);
+ cd.method = _nm_get_string(base_callable.get_method(), name_map);
cd.signal = _nm_get_string(c.signal.get_name(), name_map);
cd.flags = c.flags;
- for (int i = 0; i < c.binds.size(); i++) {
+ cd.unbinds = unbinds;
+ for (int i = 0; i < c.binds.size(); i++) { // TODO: This could be removed now.
cd.binds.push_back(_vm_get_variant(c.binds[i], variant_map));
}
+ for (int i = 0; i < binds.size(); i++) {
+ cd.binds.push_back(_vm_get_variant(binds[i], variant_map));
+ }
connections.push_back(cd);
}
}
@@ -932,7 +907,7 @@ void SceneState::clear() {
base_scene_idx = -1;
}
-Ref<SceneState> SceneState::_get_base_scene_state() const {
+Ref<SceneState> SceneState::get_base_scene_state() const {
if (base_scene_idx >= 0) {
Ref<PackedScene> ps = variants[base_scene_idx];
if (ps.is_valid()) {
@@ -947,8 +922,8 @@ int SceneState::find_node_by_path(const NodePath &p_node) const {
ERR_FAIL_COND_V_MSG(node_path_cache.size() == 0, -1, "This operation requires the node cache to have been built.");
if (!node_path_cache.has(p_node)) {
- if (_get_base_scene_state().is_valid()) {
- int idx = _get_base_scene_state()->find_node_by_path(p_node);
+ if (get_base_scene_state().is_valid()) {
+ int idx = get_base_scene_state()->find_node_by_path(p_node);
if (idx != -1) {
int rkey = _find_base_scene_node_remap_key(idx);
if (rkey == -1) {
@@ -963,11 +938,11 @@ int SceneState::find_node_by_path(const NodePath &p_node) const {
int nid = node_path_cache[p_node];
- if (_get_base_scene_state().is_valid() && !base_scene_node_remap.has(nid)) {
+ if (get_base_scene_state().is_valid() && !base_scene_node_remap.has(nid)) {
//for nodes that _do_ exist in current scene, still try to look for
//the node in the instantiated scene, as a property may be missing
//from the local one
- int idx = _get_base_scene_state()->find_node_by_path(p_node);
+ int idx = get_base_scene_state()->find_node_by_path(p_node);
if (idx != -1) {
base_scene_node_remap[nid] = idx;
}
@@ -1007,7 +982,7 @@ Variant SceneState::get_property_value(int p_node, const StringName &p_property,
//property not found, try on instance
if (base_scene_node_remap.has(p_node)) {
- return _get_base_scene_state()->get_property_value(base_scene_node_remap[p_node], p_property, found);
+ return get_base_scene_state()->get_property_value(base_scene_node_remap[p_node], p_property, found);
}
return Variant();
@@ -1026,7 +1001,7 @@ bool SceneState::is_node_in_group(int p_node, const StringName &p_group) const {
}
if (base_scene_node_remap.has(p_node)) {
- return _get_base_scene_state()->is_node_in_group(base_scene_node_remap[p_node], p_group);
+ return get_base_scene_state()->is_node_in_group(base_scene_node_remap[p_node], p_group);
}
return false;
@@ -1065,7 +1040,7 @@ bool SceneState::is_connection(int p_node, const StringName &p_signal, int p_to_
}
if (base_scene_node_remap.has(p_node) && base_scene_node_remap.has(p_to_node)) {
- return _get_base_scene_state()->is_connection(base_scene_node_remap[p_node], p_signal, base_scene_node_remap[p_to_node], p_to_method);
+ return get_base_scene_state()->is_connection(base_scene_node_remap[p_node], p_signal, base_scene_node_remap[p_to_node], p_to_method);
}
return false;
@@ -1447,6 +1422,11 @@ int SceneState::get_connection_flags(int p_idx) const {
return connections[p_idx].flags;
}
+int SceneState::get_connection_unbinds(int p_idx) const {
+ ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
+ return connections[p_idx].unbinds;
+}
+
Array SceneState::get_connection_binds(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, connections.size(), Array());
Array binds;
@@ -1488,7 +1468,7 @@ bool SceneState::has_connection(const NodePath &p_node_from, const StringName &p
}
}
- ss = ss->_get_base_scene_state();
+ ss = ss->get_base_scene_state();
} while (ss.is_valid());
return false;
@@ -1551,7 +1531,7 @@ void SceneState::set_base_scene(int p_idx) {
base_scene_idx = p_idx;
}
-void SceneState::add_connection(int p_from, int p_to, int p_signal, int p_method, int p_flags, const Vector<int> &p_binds) {
+void SceneState::add_connection(int p_from, int p_to, int p_signal, int p_method, int p_flags, int p_unbinds, const Vector<int> &p_binds) {
ERR_FAIL_INDEX(p_signal, names.size());
ERR_FAIL_INDEX(p_method, names.size());
@@ -1564,6 +1544,7 @@ void SceneState::add_connection(int p_from, int p_to, int p_signal, int p_method
c.signal = p_signal;
c.method = p_method;
c.flags = p_flags;
+ c.unbinds = p_unbinds;
c.binds = p_binds;
connections.push_back(c);
}
@@ -1606,10 +1587,12 @@ void SceneState::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_connection_method", "idx"), &SceneState::get_connection_method);
ClassDB::bind_method(D_METHOD("get_connection_flags", "idx"), &SceneState::get_connection_flags);
ClassDB::bind_method(D_METHOD("get_connection_binds", "idx"), &SceneState::get_connection_binds);
+ ClassDB::bind_method(D_METHOD("get_connection_unbinds", "idx"), &SceneState::get_connection_unbinds);
BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
+ BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
}
SceneState::SceneState() {
@@ -1651,7 +1634,7 @@ Node *PackedScene::instantiate(GenEditState p_edit_state) const {
s->set_scene_instance_state(state);
}
- if (get_path() != "" && get_path().find("::") == -1) {
+ if (!is_built_in()) {
s->set_scene_file_path(get_path());
}
@@ -1701,6 +1684,7 @@ void PackedScene::_bind_methods() {
BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
+ BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
}
PackedScene::PackedScene() {