Godot Engine – Multi-platform 2D and 3D game engine
Reduce code duplication around container types.
Lukas Tenbrink49a009fd2e72
diff --git a/core/io/resource.cpp b/core/io/resource.cppindex 6c245e3259e..0aaa75c3314 100644--- a/core/io/resource.cpp+++ b/core/io/resource.cpp@@ -345,7 +345,7 @@ Variant Resource::_duplicate_recursive(const Variant &p_variant, const Duplicateconst Dictionary &src = p_variant;Dictionary dst;if (src.is_typed()) {- dst.set_typed(src.get_typed_key_builtin(), src.get_typed_key_class_name(), src.get_typed_key_script(), src.get_typed_value_builtin(), src.get_typed_value_class_name(), src.get_typed_value_script());+ dst.set_typed(src.get_key_type(), src.get_value_type());}for (const Variant &k : src.get_key_list()) {const Variant &v = src[k];diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cppindex ea17f39e1e5..dd9ee34fae1 100644--- a/core/io/resource_format_binary.cpp+++ b/core/io/resource_format_binary.cpp@@ -36,6 +36,7 @@#include "core/io/missing_resource.h"#include "core/object/class_db.h"#include "core/object/script_language.h"+#include "core/variant/container_type_validate.h"#include "core/version.h"#include "scene/property_utils.h"#include "scene/resources/packed_scene.h"@@ -790,7 +791,7 @@ Error ResourceLoaderBinary::load() {if (is_get_valid && get_value.get_type() == Variant::ARRAY) {Array get_array = get_value;if (!set_array.is_same_typed(get_array)) {- value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());+ value = Array(set_array, get_array.get_element_type());}}}@@ -802,8 +803,7 @@ Error ResourceLoaderBinary::load() {if (is_get_valid && get_value.get_type() == Variant::DICTIONARY) {Dictionary get_dict = get_value;if (!set_dict.is_same_typed(get_dict)) {- value = Dictionary(set_dict, get_dict.get_typed_key_builtin(), get_dict.get_typed_key_class_name(), get_dict.get_typed_key_script(),- get_dict.get_typed_value_builtin(), get_dict.get_typed_value_class_name(), get_dict.get_typed_value_script());+ value = Dictionary(set_dict, get_dict.get_key_type(), get_dict.get_value_type());}}}diff --git a/core/variant/array.cpp b/core/variant/array.cppindex 100110c6912..a804c924c99 100644--- a/core/variant/array.cpp+++ b/core/variant/array.cpp@@ -869,6 +869,13 @@ const void *Array::id() const {return _p;}+Array::Array(const Array &p_from, const ContainerType &p_element_type) {+ _p = memnew(ArrayPrivate);+ _p->refcount.init();+ set_typed(p_element_type);+ assign(p_from);+}+Array::Array(const Array &p_from, uint32_t p_type, const StringName &p_class_name, const Variant &p_script) {_p = memnew(ArrayPrivate);_p->refcount.init();@@ -907,12 +914,8 @@ bool Array::is_same_instance(const Array &p_other) const {return _p == p_other._p;}-ContainerType Array::get_element_type() const {- ContainerType type;- type.variant_type = _p->typed.variant_type;- type.class_name = _p->typed.class_name;- type.script = _p->typed.script;- return type;+const ContainerType &Array::get_element_type() const {+ return _p->typed;}uint32_t Array::get_typed_builtin() const {diff --git a/core/variant/array.h b/core/variant/array.hindex 9ec32797ead..5c6b817fee7 100644--- a/core/variant/array.h+++ b/core/variant/array.h@@ -187,7 +187,7 @@ public:bool is_same_typed(const Array &p_other) const;bool is_same_instance(const Array &p_other) const;- ContainerType get_element_type() const;+ const ContainerType &get_element_type() const _LIFETIME_BOUND_;uint32_t get_typed_builtin() const;StringName get_typed_class_name() const;Variant get_typed_script() const;@@ -201,6 +201,7 @@ public:return this->span();}+ Array(const Array &p_base, const ContainerType &p_element_type);Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);Array(const Array &p_from);Array(std::initializer_list<Variant> p_init);diff --git a/core/variant/container_type_validate.h b/core/variant/container_type_validate.hindex c2e7d2a9758..867a080e611 100644--- a/core/variant/container_type_validate.h+++ b/core/variant/container_type_validate.h@@ -38,12 +38,16 @@ struct ContainerType {Variant::Type variant_type = Variant::NIL;StringName class_name;Ref<Script> script;++ _FORCE_INLINE_ bool operator==(const ContainerType &p_type) const {+ return variant_type == p_type.variant_type && class_name == p_type.class_name && script == p_type.script;+ }+ _FORCE_INLINE_ bool operator!=(const ContainerType &p_type) const {+ return variant_type != p_type.variant_type || class_name != p_type.class_name || script != p_type.script;+ }};-struct ContainerTypeValidate {- Variant::Type variant_type = Variant::NIL;- StringName class_name;- Ref<Script> script;+struct ContainerTypeValidate : ContainerType {const char *where = "container";private:@@ -188,11 +192,4 @@ public:return true;}-- _FORCE_INLINE_ bool operator==(const ContainerTypeValidate &p_type) const {- return variant_type == p_type.variant_type && class_name == p_type.class_name && script == p_type.script;- }- _FORCE_INLINE_ bool operator!=(const ContainerTypeValidate &p_type) const {- return variant_type != p_type.variant_type || class_name != p_type.class_name || script != p_type.script;- }};diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cppindex dca4b69fac4..2d9804313bb 100644--- a/core/variant/dictionary.cpp+++ b/core/variant/dictionary.cpp@@ -687,20 +687,12 @@ bool Dictionary::is_same_typed_value(const Dictionary &p_other) const {return _p->typed_value == p_other._p->typed_value;}-ContainerType Dictionary::get_key_type() const {- ContainerType type;- type.variant_type = _p->typed_key.variant_type;- type.class_name = _p->typed_key.class_name;- type.script = _p->typed_key.script;- return type;+const ContainerType &Dictionary::get_key_type() const {+ return _p->typed_key;}-ContainerType Dictionary::get_value_type() const {- ContainerType type;- type.variant_type = _p->typed_value.variant_type;- type.class_name = _p->typed_value.class_name;- type.script = _p->typed_value.script;- return type;+const ContainerType &Dictionary::get_value_type() const {+ return _p->typed_value;}uint32_t Dictionary::get_typed_key_builtin() const {@@ -746,6 +738,13 @@ const void *Dictionary::id() const {return _p;}+Dictionary::Dictionary(const Dictionary &p_base, const ContainerType &p_key_type, const ContainerType &p_value_type) {+ _p = memnew(DictionaryPrivate);+ _p->refcount.init();+ set_typed(p_key_type, p_value_type);+ assign(p_base);+}+Dictionary::Dictionary(const Dictionary &p_base, uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {_p = memnew(DictionaryPrivate);_p->refcount.init();diff --git a/core/variant/dictionary.h b/core/variant/dictionary.hindex 447a00afff3..20b0a82981a 100644--- a/core/variant/dictionary.h+++ b/core/variant/dictionary.h@@ -121,8 +121,8 @@ public:bool is_same_typed_key(const Dictionary &p_other) const;bool is_same_typed_value(const Dictionary &p_other) const;- ContainerType get_key_type() const;- ContainerType get_value_type() const;+ const ContainerType &get_key_type() const _LIFETIME_BOUND_;+ const ContainerType &get_value_type() const _LIFETIME_BOUND_;uint32_t get_typed_key_builtin() const;uint32_t get_typed_value_builtin() const;StringName get_typed_key_class_name() const;@@ -137,6 +137,7 @@ public:const void *id() const;+ Dictionary(const Dictionary &p_base, const ContainerType &p_key_type, const ContainerType &p_value_type);Dictionary(const Dictionary &p_base, uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);Dictionary(const Dictionary &p_from);Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init);diff --git a/core/variant/variant_op.h b/core/variant/variant_op.hindex c45a4a711f4..2485700ed1f 100644--- a/core/variant/variant_op.h+++ b/core/variant/variant_op.h@@ -31,6 +31,7 @@#pragma once#include "core/math/math_funcs.h"+#include "core/variant/container_type_validate.h"#include "core/variant/method_ptrcall.h"#include "core/variant/type_info.h"#include "core/variant/variant.h"@@ -710,7 +711,7 @@ public:int bsize = p_array_right.size();if (p_array_left.is_typed() && p_array_left.is_same_typed(p_array_right)) {- r_sum.set_typed(p_array_left.get_typed_builtin(), p_array_left.get_typed_class_name(), p_array_left.get_typed_script());+ r_sum.set_typed(p_array_left.get_element_type());}r_sum.resize(asize + bsize);diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cppindex 1531a6fc247..30af2df8283 100644--- a/core/variant/variant_parser.cpp+++ b/core/variant/variant_parser.cpp@@ -36,6 +36,7 @@#include "core/object/class_db.h"#include "core/object/script_language.h"#include "core/string/string_buffer.h"+#include "core/variant/container_type_validate.h"char32_t VariantParser::Stream::get_char() {// is within buffer?@@ -2114,61 +2115,11 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_strif (dict.is_typed()) {p_store_string_func(p_store_string_ud, "Dictionary[");- Variant::Type key_builtin_type = (Variant::Type)dict.get_typed_key_builtin();- StringName key_class_name = dict.get_typed_key_class_name();- Ref<Script> key_script = dict.get_typed_key_script();-- if (key_script.is_valid()) {- String resource_text;- if (p_encode_res_func) {- resource_text = p_encode_res_func(p_encode_res_ud, key_script);- }- if (resource_text.is_empty() && key_script->get_path().is_resource_file()) {- resource_text = encode_resource_reference(key_script->get_path());- }-- if (!resource_text.is_empty()) {- p_store_string_func(p_store_string_ud, resource_text);- } else {- ERR_PRINT("Failed to encode a path to a custom script for a dictionary key type.");- p_store_string_func(p_store_string_ud, key_class_name);- }- } else if (key_class_name != StringName()) {- p_store_string_func(p_store_string_ud, key_class_name);- } else if (key_builtin_type == Variant::NIL) {- p_store_string_func(p_store_string_ud, "Variant");- } else {- p_store_string_func(p_store_string_ud, Variant::get_type_name(key_builtin_type));- }+ write_container_type(dict.get_key_type(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);p_store_string_func(p_store_string_ud, ", ");- Variant::Type value_builtin_type = (Variant::Type)dict.get_typed_value_builtin();- StringName value_class_name = dict.get_typed_value_class_name();- Ref<Script> value_script = dict.get_typed_value_script();-- if (value_script.is_valid()) {- String resource_text;- if (p_encode_res_func) {- resource_text = p_encode_res_func(p_encode_res_ud, value_script);- }- if (resource_text.is_empty() && value_script->get_path().is_resource_file()) {- resource_text = encode_resource_reference(value_script->get_path());- }-- if (!resource_text.is_empty()) {- p_store_string_func(p_store_string_ud, resource_text);- } else {- ERR_PRINT("Failed to encode a path to a custom script for a dictionary value type.");- p_store_string_func(p_store_string_ud, value_class_name);- }- } else if (value_class_name != StringName()) {- p_store_string_func(p_store_string_ud, value_class_name);- } else if (value_builtin_type == Variant::NIL) {- p_store_string_func(p_store_string_ud, "Variant");- } else {- p_store_string_func(p_store_string_ud, Variant::get_type_name(value_builtin_type));- }+ write_container_type(dict.get_value_type(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);p_store_string_func(p_store_string_ud, "](");}@@ -2213,30 +2164,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_strif (array.is_typed()) {p_store_string_func(p_store_string_ud, "Array[");- Variant::Type builtin_type = (Variant::Type)array.get_typed_builtin();- StringName class_name = array.get_typed_class_name();- Ref<Script> script = array.get_typed_script();-- if (script.is_valid()) {- String resource_text = String();- if (p_encode_res_func) {- resource_text = p_encode_res_func(p_encode_res_ud, script);- }- if (resource_text.is_empty() && script->get_path().is_resource_file()) {- resource_text = encode_resource_reference(script->get_path());- }-- if (!resource_text.is_empty()) {- p_store_string_func(p_store_string_ud, resource_text);- } else {- ERR_PRINT("Failed to encode a path to a custom script for an array type.");- p_store_string_func(p_store_string_ud, class_name);- }- } else if (class_name != StringName()) {- p_store_string_func(p_store_string_ud, class_name);- } else {- p_store_string_func(p_store_string_ud, Variant::get_type_name(builtin_type));- }+ write_container_type(array.get_element_type(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);p_store_string_func(p_store_string_ud, "](");}@@ -2431,6 +2359,32 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_strreturn OK;}+void VariantWriter::write_container_type(const ContainerType &p_type, StoreStringFunc p_store_string_func, void *p_store_string_ud,+ EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {+ if (p_type.script.is_valid()) {+ String resource_text;+ if (p_encode_res_func) {+ resource_text = p_encode_res_func(p_encode_res_ud, p_type.script);+ }+ if (resource_text.is_empty() && p_type.script->get_path().is_resource_file()) {+ resource_text = encode_resource_reference(p_type.script->get_path());+ }++ if (!resource_text.is_empty()) {+ p_store_string_func(p_store_string_ud, resource_text);+ } else {+ ERR_PRINT("Failed to encode a path to a custom script for a container type.");+ p_store_string_func(p_store_string_ud, p_type.class_name);+ }+ } else if (p_type.class_name != StringName()) {+ p_store_string_func(p_store_string_ud, p_type.class_name);+ } else if (p_type.variant_type == Variant::NIL) {+ p_store_string_func(p_store_string_ud, "Variant");+ } else {+ p_store_string_func(p_store_string_ud, Variant::get_type_name(p_type.variant_type));+ }+}+static Error _write_to_str(void *p_ud, const String &p_string) {String *str = (String *)p_ud;(*str) += p_string;diff --git a/core/variant/variant_parser.h b/core/variant/variant_parser.hindex 98f607e7b30..3e65ac400e8 100644--- a/core/variant/variant_parser.h+++ b/core/variant/variant_parser.h@@ -160,5 +160,6 @@ public:typedef String (*EncodeResourceFunc)(void *p_ud, const Ref<Resource> &p_resource);static Error write(const Variant &p_variant, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, bool p_pretty_print = true, int p_recursion_count = 0, bool p_compat = true);+ static void write_container_type(const ContainerType &p_type, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud);static Error write_to_string(const Variant &p_variant, String &r_string, bool p_pretty_print = true, EncodeResourceFunc p_encode_res_func = nullptr, void *p_encode_res_ud = nullptr, bool p_compat = true);};diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cppindex 3a0335cb6e7..9d428afc0b4 100644--- a/modules/gdscript/gdscript_analyzer.cpp+++ b/modules/gdscript/gdscript_analyzer.cpp@@ -42,6 +42,7 @@#include "core/object/class_db.h"#include "core/object/script_language.h"#include "core/templates/hash_map.h"+#include "core/variant/container_type_validate.h"#include "scene/main/node.h"#include "modules/gdscript/gdscript_parser.h"@@ -5733,6 +5734,17 @@ Variant GDScriptAnalyzer::make_variable_default_value(GDScriptParser::VariableNoreturn result;}+GDScriptParser::DataType GDScriptAnalyzer::meta_type_from_container_type(const ContainerType &p_type, const GDScriptParser::Node *p_source) {+ if (p_type.script.is_valid()) {+ return type_from_script(p_type.script, p_source, true);+ } else if (p_type.class_name) {+ return make_native_meta_type(p_type.class_name);+ } else if (p_type.variant_type != Variant::Type::NIL) {+ return make_builtin_meta_type(p_type.variant_type);+ }+ ERR_FAIL_V_MSG(GDScriptParser::DataType(), "Internal error, please report: attempted to convert an untyped ContainerType to a DataType.");+}+GDScriptParser::DataType GDScriptAnalyzer::type_from_script(const Ref<Script> &p_script, const GDScriptParser::Node *p_source, bool p_is_meta_type) {ERR_FAIL_COND_V(!p_script.is_valid(), GDScriptParser::DataType());@@ -5793,28 +5805,16 @@ GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_vaif (p_value.get_type() == Variant::ARRAY) {const Array &array = p_value;- if (array.get_typed_script()) {- result.set_container_element_type(0, type_from_metatype(type_from_script(array.get_typed_script(), p_source, true)));- } else if (array.get_typed_class_name()) {- result.set_container_element_type(0, type_from_metatype(make_native_meta_type(array.get_typed_class_name())));- } else if (array.get_typed_builtin() != Variant::NIL) {- result.set_container_element_type(0, type_from_metatype(make_builtin_meta_type((Variant::Type)array.get_typed_builtin())));+ if (array.is_typed()) {+ result.set_container_element_type(0, type_from_metatype(meta_type_from_container_type(array.get_element_type(), p_source)));}} else if (p_value.get_type() == Variant::DICTIONARY) {const Dictionary &dict = p_value;- if (dict.get_typed_key_script()) {- result.set_container_element_type(0, type_from_metatype(type_from_script(dict.get_typed_key_script(), p_source, true)));- } else if (dict.get_typed_key_class_name()) {- result.set_container_element_type(0, type_from_metatype(make_native_meta_type(dict.get_typed_key_class_name())));- } else if (dict.get_typed_key_builtin() != Variant::NIL) {- result.set_container_element_type(0, type_from_metatype(make_builtin_meta_type((Variant::Type)dict.get_typed_key_builtin())));- }- if (dict.get_typed_value_script()) {- result.set_container_element_type(1, type_from_metatype(type_from_script(dict.get_typed_value_script(), p_source, true)));- } else if (dict.get_typed_value_class_name()) {- result.set_container_element_type(1, type_from_metatype(make_native_meta_type(dict.get_typed_value_class_name())));- } else if (dict.get_typed_value_builtin() != Variant::NIL) {- result.set_container_element_type(1, type_from_metatype(make_builtin_meta_type((Variant::Type)dict.get_typed_value_builtin())));+ if (dict.is_typed_key()) {+ result.set_container_element_type(0, type_from_metatype(meta_type_from_container_type(dict.get_key_type(), p_source)));+ }+ if (dict.is_typed_value()) {+ result.set_container_element_type(1, type_from_metatype(meta_type_from_container_type(dict.get_value_type(), p_source)));}} else if (p_value.get_type() == Variant::OBJECT) {// Object is treated as a native type, not a builtin type.diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.hindex 6d5af9265bf..2ad6bbbd1ee 100644--- a/modules/gdscript/gdscript_analyzer.h+++ b/modules/gdscript/gdscript_analyzer.h@@ -130,6 +130,7 @@ class GDScriptAnalyzer {// Helpers.Array make_array_from_element_datatype(const GDScriptParser::DataType &p_element_datatype, const GDScriptParser::Node *p_source_node = nullptr);Dictionary make_dictionary_from_element_datatype(const GDScriptParser::DataType &p_key_element_datatype, const GDScriptParser::DataType &p_value_element_datatype, const GDScriptParser::Node *p_source_node = nullptr);+ GDScriptParser::DataType meta_type_from_container_type(const ContainerType &p_type, const GDScriptParser::Node *p_source);GDScriptParser::DataType type_from_script(const Ref<Script> &p_script, const GDScriptParser::Node *p_source, bool p_is_meta_type);GDScriptParser::DataType type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source);GDScriptParser::DataType type_from_property_hint_string(const String &p_type_name) const;diff --git a/modules/gdscript/gdscript_function.cpp b/modules/gdscript/gdscript_function.cppindex 6159cf2cae9..8f250f36e6f 100644--- a/modules/gdscript/gdscript_function.cpp+++ b/modules/gdscript/gdscript_function.cpp@@ -33,6 +33,17 @@#include "gdscript.h"#include "core/object/class_db.h"+#include "core/variant/container_type_validate.h"++bool GDScriptDataType::is_type_exact(const ContainerType &p_container_type) const {+ if (p_container_type.script.is_valid()) {+ return (kind == SCRIPT || kind == GDSCRIPT) && script_type == p_container_type.script.ptr();+ } else if (p_container_type.class_name != StringName()) {+ return kind == NATIVE && native_type == p_container_type.class_name;+ } else {+ return kind == BUILTIN && builtin_type == p_container_type.variant_type;+ }+}bool GDScriptDataType::is_type(const Variant &p_variant, bool p_allow_implicit_conversion) const {switch (kind) {@@ -45,18 +56,7 @@ bool GDScriptDataType::is_type(const Variant &p_variant, bool p_allow_implicit_cif (valid && builtin_type == Variant::ARRAY && has_container_element_type(0)) {Array array = p_variant;if (array.is_typed()) {- const GDScriptDataType &elem_type = container_element_types[0];- Variant::Type array_builtin_type = (Variant::Type)array.get_typed_builtin();- StringName array_native_type = array.get_typed_class_name();- Ref<Script> array_script_type_ref = array.get_typed_script();-- if (array_script_type_ref.is_valid()) {- valid = (elem_type.kind == SCRIPT || elem_type.kind == GDSCRIPT) && elem_type.script_type == array_script_type_ref.ptr();- } else if (array_native_type != StringName()) {- valid = elem_type.kind == NATIVE && elem_type.native_type == array_native_type;- } else {- valid = elem_type.kind == BUILTIN && elem_type.builtin_type == array_builtin_type;- }+ valid = container_element_types[0].is_type_exact(array.get_element_type());} else {valid = false;}@@ -65,32 +65,12 @@ bool GDScriptDataType::is_type(const Variant &p_variant, bool p_allow_implicit_cif (dictionary.is_typed()) {if (dictionary.is_typed_key()) {GDScriptDataType key = get_container_element_type_or_variant(0);- Variant::Type key_builtin_type = (Variant::Type)dictionary.get_typed_key_builtin();- StringName key_native_type = dictionary.get_typed_key_class_name();- Ref<Script> key_script_type_ref = dictionary.get_typed_key_script();-- if (key_script_type_ref.is_valid()) {- valid = (key.kind == SCRIPT || key.kind == GDSCRIPT) && key.script_type == key_script_type_ref.ptr();- } else if (key_native_type != StringName()) {- valid = key.kind == NATIVE && key.native_type == key_native_type;- } else {- valid = key.kind == BUILTIN && key.builtin_type == key_builtin_type;- }+ valid = key.is_type_exact(dictionary.get_key_type());}if (valid && dictionary.is_typed_value()) {GDScriptDataType value = get_container_element_type_or_variant(1);- Variant::Type value_builtin_type = (Variant::Type)dictionary.get_typed_value_builtin();- StringName value_native_type = dictionary.get_typed_value_class_name();- Ref<Script> value_script_type_ref = dictionary.get_typed_value_script();-- if (value_script_type_ref.is_valid()) {- valid = (value.kind == SCRIPT || value.kind == GDSCRIPT) && value.script_type == value_script_type_ref.ptr();- } else if (value_native_type != StringName()) {- valid = value.kind == NATIVE && value.native_type == value_native_type;- } else {- valid = value.kind == BUILTIN && value.builtin_type == value_builtin_type;- }+ valid = value.is_type_exact(dictionary.get_value_type());}} else {valid = false;diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.hindex cd78d1c6861..e5c6f4933bd 100644--- a/modules/gdscript/gdscript_function.h+++ b/modules/gdscript/gdscript_function.h@@ -39,6 +39,7 @@#include "core/templates/self_list.h"#include "core/variant/variant.h"+struct ContainerType;class GDScriptInstance;class GDScript;@@ -63,6 +64,7 @@ public:_FORCE_INLINE_ bool has_type() const { return kind != VARIANT; }+ bool is_type_exact(const ContainerType &p_container_type) const;bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const;bool can_contain_object() const {diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cppindex 007190f0cf8..cb832a51384 100644--- a/scene/resources/packed_scene.cpp+++ b/scene/resources/packed_scene.cpp@@ -106,10 +106,7 @@ Variant SceneState::_duplicate_recursive(const Variant &p_variant, HashMap<Nodehas_fallback = false;if (fallback.is_typed()) {const ContainerType &fallback_type = fallback.get_element_type();- has_fallback =- scr_type.variant_type == fallback_type.variant_type &&- scr_type.class_name == fallback_type.class_name &&- scr_type.script == fallback_type.script;+ has_fallback = scr_type == fallback_type;}}dst.resize(src.size());@@ -129,16 +126,10 @@ Variant SceneState::_duplicate_recursive(const Variant &p_variant, HashMap<Nodebool has_fallback = true;Dictionary dst;if (src.is_typed()) {- dst.set_typed(src.get_typed_key_builtin(), src.get_typed_key_class_name(), src.get_typed_key_script(), src.get_typed_value_builtin(), src.get_typed_value_class_name(), src.get_typed_value_script());+ dst.set_typed(src.get_key_type(), src.get_value_type());has_fallback = false;if (fallback.is_typed()) {- has_fallback =- src.get_typed_key_builtin() == fallback.get_typed_key_builtin() &&- src.get_typed_key_class_name() == fallback.get_typed_key_class_name() &&- src.get_typed_key_script() == fallback.get_typed_key_script() &&- src.get_typed_value_builtin() == fallback.get_typed_value_builtin() &&- src.get_typed_value_class_name() == fallback.get_typed_value_class_name() &&- src.get_typed_value_script() == fallback.get_typed_value_script();+ has_fallback = src.get_key_type() == fallback.get_key_type() && src.get_value_type() == fallback.get_value_type();}}@@ -535,7 +526,7 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {if (set_array.is_same_typed(get_array)) {set_array = set_array.duplicate();} else {- set_array = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());+ set_array = Array(set_array, get_array.get_element_type());}}@@ -552,7 +543,7 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {if (set_dict.is_same_typed(get_dict)) {set_dict = set_dict.duplicate();} else {- set_dict = Dictionary(set_dict, get_dict.get_typed_key_builtin(), get_dict.get_typed_key_class_name(), get_dict.get_typed_key_script(), get_dict.get_typed_value_builtin(), get_dict.get_typed_value_class_name(), get_dict.get_typed_value_script());+ set_dict = Dictionary(set_dict, get_dict.get_key_type(), get_dict.get_value_type());}}diff --git a/scene/resources/resource_format_text.cpp b/scene/resources/resource_format_text.cppindex 3ac84e5d63a..181d5c9992b 100644--- a/scene/resources/resource_format_text.cpp+++ b/scene/resources/resource_format_text.cpp@@ -36,6 +36,7 @@#include "core/io/missing_resource.h"#include "core/object/class_db.h"#include "core/object/script_language.h"+#include "core/variant/container_type_validate.h"#include "scene/property_utils.h"#include "scene/resources/packed_scene.h"@@ -649,7 +650,7 @@ Error ResourceLoaderText::load() {if (is_get_valid && get_value.get_type() == Variant::ARRAY) {Array get_array = get_value;if (!set_array.is_same_typed(get_array)) {- value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());+ value = Array(set_array, get_array.get_element_type());}}}@@ -661,8 +662,7 @@ Error ResourceLoaderText::load() {if (is_get_valid && get_value.get_type() == Variant::DICTIONARY) {Dictionary get_dict = get_value;if (!set_dict.is_same_typed(get_dict)) {- value = Dictionary(set_dict, get_dict.get_typed_key_builtin(), get_dict.get_typed_key_class_name(), get_dict.get_typed_key_script(),- get_dict.get_typed_value_builtin(), get_dict.get_typed_value_class_name(), get_dict.get_typed_value_script());+ value = Dictionary(set_dict, get_dict.get_key_type(), get_dict.get_value_type());}}}@@ -803,7 +803,7 @@ Error ResourceLoaderText::load() {if (is_get_valid && get_value.get_type() == Variant::ARRAY) {Array get_array = get_value;if (!set_array.is_same_typed(get_array)) {- value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());+ value = Array(set_array, get_array.get_element_type());}}}@@ -815,8 +815,7 @@ Error ResourceLoaderText::load() {if (is_get_valid && get_value.get_type() == Variant::DICTIONARY) {Dictionary get_dict = get_value;if (!set_dict.is_same_typed(get_dict)) {- value = Dictionary(set_dict, get_dict.get_typed_key_builtin(), get_dict.get_typed_key_class_name(), get_dict.get_typed_key_script(),- get_dict.get_typed_value_builtin(), get_dict.get_typed_value_class_name(), get_dict.get_typed_value_script());+ value = Dictionary(set_dict, get_dict.get_key_type(), get_dict.get_value_type());}}}