Skip to content

Commit

Permalink
Fix VariantResource converting from null
Browse files Browse the repository at this point in the history
  • Loading branch information
Xrayez committed Oct 30, 2020
1 parent c385532 commit 4972e9f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
10 changes: 9 additions & 1 deletion core/types/variant_resource.cpp
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
5 changes: 1 addition & 4 deletions core/types/variant_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "core/resource.h"
#include "core/variant.h"

class VariantResource;

class VariantResource : public Resource {
GDCLASS(VariantResource, Resource);

Expand All @@ -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
6 changes: 3 additions & 3 deletions doc/VariantResource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
A [Resource] which holds any [Variant] compatible type.
</brief_description>
<description>
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()
Expand All @@ -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.
</description>
<tutorials>
</tutorials>
Expand All @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion tests/project/goost/core/types/test_variant_resource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,23 @@ 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")


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, "")

0 comments on commit 4972e9f

Please sign in to comment.