From 4972e9f2b66ef7441e2657634c23131997de29a8 Mon Sep 17 00:00:00 2001 From: "Andrii Doroshenko (Xrayez)" Date: Fri, 30 Oct 2020 15:22:40 +0200 Subject: [PATCH] Fix `VariantResource` converting from `null` --- core/types/variant_resource.cpp | 10 +++++++++- core/types/variant_resource.h | 5 +---- doc/VariantResource.xml | 6 +++--- .../goost/core/types/test_variant_resource.gd | 14 +++++++++++++- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/core/types/variant_resource.cpp b/core/types/variant_resource.cpp index b8acc9f0..c18ffe7d 100644 --- a/core/types/variant_resource.cpp +++ b/core/types/variant_resource.cpp @@ -1,15 +1,23 @@ #include "variant_resource.h" void VariantResource::set_type(Variant::Type p_type) { + const Variant::Type prev_type = type; type = p_type; // Convert previous value to a new type, if possible. - if (value.get_type() != Variant::NIL) { + if (prev_type != Variant::NIL) { value = convert(value, type); + } else { + value = create(type); } emit_changed(); _change_notify(); } +Variant VariantResource::create(const Variant::Type &p_type) { + Variant::CallError error; + return Variant::construct(p_type, nullptr, 0, error); +} + Variant VariantResource::convert(const Variant &p_value, const Variant::Type &p_to_type) { Variant::CallError error; const Variant *args[1]; diff --git a/core/types/variant_resource.h b/core/types/variant_resource.h index 811e3ae4..89c126df 100644 --- a/core/types/variant_resource.h +++ b/core/types/variant_resource.h @@ -4,8 +4,6 @@ #include "core/resource.h" #include "core/variant.h" -class VariantResource; - class VariantResource : public Resource { GDCLASS(VariantResource, Resource); @@ -23,11 +21,10 @@ class VariantResource : public Resource { void set_type(Variant::Type p_type); int get_type() const { return type; } + static Variant create(const Variant::Type &p_type); static Variant convert(const Variant &p_value, const Variant::Type &p_to_type); virtual String to_string() { return String(value); } - - VariantResource() {} }; #endif // GOOST_VARIANT_RESOURCE_H diff --git a/doc/VariantResource.xml b/doc/VariantResource.xml index 4199e32f..881e66c6 100644 --- a/doc/VariantResource.xml +++ b/doc/VariantResource.xml @@ -4,7 +4,7 @@ A [Resource] which holds any [Variant] compatible type. - This class allows to store any [Variant] compatible built-in types as a [Resource], such as [Vector2], [Dictionary], [Array], and even [VariantResource] itself. These can be edited in the inspector and saved to disk to be reused throughout the project, as resources are shared between instances by default. + This class allows to store any [Variant] compatible built-in types as a [Resource], such as [Vector2], [Dictionary], [Array], and even nested[VariantResource]s themselves. These can be edited in the inspector and saved to disk to be reused throughout the project, as resources are shared between instances by default. [VariantResource] is normally edited via the editor inspector, but the [member type] and [code]value[/code] can be changed via code as well: [codeblock] var res = VariantResource.new() @@ -13,7 +13,7 @@ res.type = TYPE_VECTOR2 # The previous value is converted to a new type. print(res.value) # prints (1, 2) [/codeblock] - The conversion logic is mostly equivalent to [method @GDScript.convert], except that the [constant @GlobalScope.TYPE_NIL] is never automatically converted to other types. + The conversion logic is mostly equivalent to [method @GDScript.convert], except that the [constant @GlobalScope.TYPE_NIL] is never automatically converted to other types, and a new empty [Variant] is constructed instead. @@ -26,7 +26,7 @@ [codeblock] extends Node2D - export(VariantResource) res = VariantResource.new() + export(VariantResource) var res = VariantResource.new() func _ready(): # Whenever the color is changed, redraw the canvas. diff --git a/tests/project/goost/core/types/test_variant_resource.gd b/tests/project/goost/core/types/test_variant_resource.gd index 82054bb6..a6d681d3 100644 --- a/tests/project/goost/core/types/test_variant_resource.gd +++ b/tests/project/goost/core/types/test_variant_resource.gd @@ -36,7 +36,7 @@ func test_set_value_convert_type(): func test_changed_signal(): watch_signals(res) res.value = "Godot" - var err = res.connect("changed", self, "_on_changed", [res]) + var err = res.connect("changed", self, "_on_changed", [res], CONNECT_ONESHOT) assert_eq(err, OK) res.value = "Goost" assert_signal_emitted(res, "changed") @@ -44,3 +44,15 @@ func test_changed_signal(): func _on_changed(p_res): assert_eq(p_res.value, "Goost") + + +func test_convert_from_null_to_string(): + res.value = null + assert_eq(res.type, TYPE_NIL) + + res.type = TYPE_STRING + assert_ne(res.value, "Null") + assert_ne(res.value, "Nil") + assert_ne(res.value, "null") + assert_ne(res.value, "nil") + assert_eq(res.value, "")