From bbd4873eead0a942ec682fc3315668818a1babfe Mon Sep 17 00:00:00 2001 From: Ninni Pipping Date: Thu, 1 Jun 2023 17:23:36 +0200 Subject: [PATCH] Fix index in loading of `Array[Node]` --- scene/resources/packed_scene.cpp | 58 ++++++++------------------------ scene/resources/packed_scene.h | 2 +- 2 files changed, 15 insertions(+), 45 deletions(-) diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cpp index 23942658ccf..359c4765a2e 100644 --- a/scene/resources/packed_scene.cpp +++ b/scene/resources/packed_scene.cpp @@ -244,35 +244,11 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const { uint32_t name_idx = nprops[j].name & (FLAG_PATH_PROPERTY_IS_NODE - 1); ERR_FAIL_UNSIGNED_INDEX_V(name_idx, (uint32_t)sname_count, nullptr); - const StringName &prop_name = snames[name_idx]; - const Variant &prop_variant = props[nprops[j].value]; - - if (prop_variant.get_type() == Variant::ARRAY) { - const Array &array = prop_variant; - if (Engine::get_singleton()->is_editor_hint()) { - if (array.get_typed_builtin() == Variant::NODE_PATH) { - // If editor, simply set the original array of NodePaths. - node->set(prop_name, prop_variant); - continue; - } - } - for (int k = 0; k < array.size(); k++) { - DeferredNodePathProperties dnp; - dnp.path = array[k]; - dnp.base = node; - // Use special property name to signify an array. This is only used in deferred_node_paths. - dnp.property = String(prop_name) + "/indices/" + itos(k); - deferred_node_paths.push_back(dnp); - } - - } else { - // Do an actual deferred set of the property path. - DeferredNodePathProperties dnp; - dnp.path = prop_variant; - dnp.base = node; - dnp.property = prop_name; - deferred_node_paths.push_back(dnp); - } + DeferredNodePathProperties dnp; + dnp.value = props[nprops[j].value]; + dnp.base = node; + dnp.property = snames[name_idx]; + deferred_node_paths.push_back(dnp); continue; } @@ -465,27 +441,21 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const { for (const DeferredNodePathProperties &dnp : deferred_node_paths) { // Replace properties stored as NodePaths with actual Nodes. - Node *other = dnp.base->get_node_or_null(dnp.path); - - const String string_property = dnp.property; - if (string_property.contains("/indices/")) { - // For properties with "/indices/", the replacement takes place inside an Array. - const String base_property = string_property.get_slice("/", 0); - const int index = string_property.get_slice("/", 2).to_int(); + if (dnp.value.get_type() == Variant::ARRAY) { + Array paths = dnp.value; bool valid; - Array array = dnp.base->get(base_property, &valid); + Array array = dnp.base->get(dnp.property, &valid); ERR_CONTINUE(!valid); + array = array.duplicate(); - if (array.size() >= index) { - array.push_back(other); - } else { - array.set(index, other); + array.resize(paths.size()); + for (int i = 0; i < array.size(); i++) { + array.set(i, dnp.base->get_node_or_null(paths[i])); } - - dnp.base->set(base_property, array); + dnp.base->set(dnp.property, array); } else { - dnp.base->set(dnp.property, other); + dnp.base->set(dnp.property, dnp.base->get_node_or_null(dnp.value)); } } diff --git a/scene/resources/packed_scene.h b/scene/resources/packed_scene.h index 29df382c1fe..35799dc1eec 100644 --- a/scene/resources/packed_scene.h +++ b/scene/resources/packed_scene.h @@ -72,7 +72,7 @@ class SceneState : public RefCounted { struct DeferredNodePathProperties { Node *base = nullptr; StringName property; - NodePath path; + Variant value; }; Vector nodes;