Skip to content

Commit

Permalink
Add a type conversion method to Variant Utility and expose to scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Apr 3, 2023
1 parent df7834a commit 52b86a7
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 0 deletions.
85 changes: 85 additions & 0 deletions core/variant/variant_utility.cpp
Expand Up @@ -741,6 +741,90 @@ struct VariantUtilityFunctions {
return obj.get_type();
}

static inline Variant type_convert(const Variant &p_variant, const Variant::Type p_type) {
switch (p_type) {
case Variant::Type::NIL:
return Variant();
case Variant::Type::BOOL:
return p_variant.operator bool();
case Variant::Type::INT:
return p_variant.operator int64_t();
case Variant::Type::FLOAT:
return p_variant.operator double();
case Variant::Type::STRING:
return p_variant.operator String();
case Variant::Type::VECTOR2:
return p_variant.operator Vector2();
case Variant::Type::VECTOR2I:
return p_variant.operator Vector2i();
case Variant::Type::RECT2:
return p_variant.operator Rect2();
case Variant::Type::RECT2I:
return p_variant.operator Rect2i();
case Variant::Type::VECTOR3:
return p_variant.operator Vector3();
case Variant::Type::VECTOR3I:
return p_variant.operator Vector3i();
case Variant::Type::TRANSFORM2D:
return p_variant.operator Transform2D();
case Variant::Type::VECTOR4:
return p_variant.operator Vector4();
case Variant::Type::VECTOR4I:
return p_variant.operator Vector4i();
case Variant::Type::PLANE:
return p_variant.operator Plane();
case Variant::Type::QUATERNION:
return p_variant.operator Quaternion();
case Variant::Type::AABB:
return p_variant.operator ::AABB();
case Variant::Type::BASIS:
return p_variant.operator Basis();
case Variant::Type::TRANSFORM3D:
return p_variant.operator Transform3D();
case Variant::Type::PROJECTION:
return p_variant.operator Projection();
case Variant::Type::COLOR:
return p_variant.operator Color();
case Variant::Type::STRING_NAME:
return p_variant.operator StringName();
case Variant::Type::NODE_PATH:
return p_variant.operator NodePath();
case Variant::Type::RID:
return p_variant.operator ::RID();
case Variant::Type::OBJECT:
return p_variant.operator Object *();
case Variant::Type::CALLABLE:
return p_variant.operator Callable();
case Variant::Type::SIGNAL:
return p_variant.operator Signal();
case Variant::Type::DICTIONARY:
return p_variant.operator Dictionary();
case Variant::Type::ARRAY:
return p_variant.operator Array();
case Variant::Type::PACKED_BYTE_ARRAY:
return p_variant.operator PackedByteArray();
case Variant::Type::PACKED_INT32_ARRAY:
return p_variant.operator PackedInt32Array();
case Variant::Type::PACKED_INT64_ARRAY:
return p_variant.operator PackedInt64Array();
case Variant::Type::PACKED_FLOAT32_ARRAY:
return p_variant.operator PackedFloat32Array();
case Variant::Type::PACKED_FLOAT64_ARRAY:
return p_variant.operator PackedFloat64Array();
case Variant::Type::PACKED_STRING_ARRAY:
return p_variant.operator PackedStringArray();
case Variant::Type::PACKED_VECTOR2_ARRAY:
return p_variant.operator PackedVector2Array();
case Variant::Type::PACKED_VECTOR3_ARRAY:
return p_variant.operator PackedVector3Array();
case Variant::Type::PACKED_COLOR_ARRAY:
return p_variant.operator PackedColorArray();
default:
ERR_PRINT("Invalid type argument to type_convert(), use the TYPE_* constants. Returning the unconverted Variant.");
return p_variant;
}
}

static inline String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
if (p_arg_count < 1) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
Expand Down Expand Up @@ -1599,6 +1683,7 @@ void Variant::_register_variant_utility_functions() {

FUNCBINDVR(weakref, sarray("obj"), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDR(_typeof, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDR(type_convert, sarray("variant", "type"), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDVARARGS(str, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDR(error_string, sarray("error"), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDVARARGV(print, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
Expand Down
17 changes: 17 additions & 0 deletions doc/classes/@GlobalScope.xml
Expand Up @@ -1321,6 +1321,23 @@
[/codeblock]
</description>
</method>
<method name="type_convert">
<return type="Variant" />
<param index="0" name="variant" type="Variant" />
<param index="1" name="type" type="int" />
<description>
Converts the given [param variant] to the given [param type], using the [enum Variant.Type] values. This method is generous with how it handles types, it can automatically convert between array types, convert numeric [String]s to [int], and converting most things to [String].
If the type conversion cannot be done, this method will return the default value for that type, for example converting [Rect2] to [Vector2] will always return [code]Vector2.ZERO[/code]. This method will never show error messages as long as [param type] is a valid Variant type.
The returned value is a [Variant], but the data inside and the [enum Variant.Type] will be the same as the requested type.
[codeblock]
type_convert("Hi!", TYPE_INT) # Returns 0
type_convert("123", TYPE_INT) # Returns 123
type_convert(123.4, TYPE_INT) # Returns 123
type_convert(5, TYPE_VECTOR2) # Returns (0, 0)
type_convert("Hi!", TYPE_NIL) # Returns null
[/codeblock]
</description>
</method>
<method name="typeof">
<return type="int" />
<param index="0" name="variable" type="Variant" />
Expand Down
119 changes: 119 additions & 0 deletions tests/core/variant/test_variant_utility.h
@@ -0,0 +1,119 @@
/**************************************************************************/
/* test_variant_utility.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef TEST_VARIANT_UTILITY_H
#define TEST_VARIANT_UTILITY_H

// There is no variant_utility.h, so include the cpp.
#include "core/variant/variant_utility.cpp"

#include "tests/test_macros.h"

namespace TestVariantUtility {

TEST_CASE("[VariantUtility] Type conversion") {
Variant converted;
converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::NIL);
CHECK(converted.get_type() == Variant::Type::NIL);
CHECK(converted == Variant());

converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::INT);
CHECK(converted.get_type() == Variant::Type::INT);
CHECK(converted == Variant(0));

converted = VariantUtilityFunctions::type_convert("123", Variant::Type::INT);
CHECK(converted.get_type() == Variant::Type::INT);
CHECK(converted == Variant(123));

converted = VariantUtilityFunctions::type_convert(123, Variant::Type::STRING);
CHECK(converted.get_type() == Variant::Type::STRING);
CHECK(converted == Variant("123"));

converted = VariantUtilityFunctions::type_convert(123.4, Variant::Type::INT);
CHECK(converted.get_type() == Variant::Type::INT);
CHECK(converted == Variant(123));

converted = VariantUtilityFunctions::type_convert(5, Variant::Type::VECTOR2);
CHECK(converted.get_type() == Variant::Type::VECTOR2);
CHECK(converted == Variant(Vector2(0, 0)));

converted = VariantUtilityFunctions::type_convert(Vector3(1, 2, 3), Variant::Type::VECTOR2);
CHECK(converted.get_type() == Variant::Type::VECTOR2);
CHECK(converted == Variant(Vector2(1, 2)));

converted = VariantUtilityFunctions::type_convert(Vector2(1, 2), Variant::Type::VECTOR4);
CHECK(converted.get_type() == Variant::Type::VECTOR4);
CHECK(converted == Variant(Vector4(1, 2, 0, 0)));

converted = VariantUtilityFunctions::type_convert(Vector4(1.2, 3.4, 5.6, 7.8), Variant::Type::VECTOR3I);
CHECK(converted.get_type() == Variant::Type::VECTOR3I);
CHECK(converted == Variant(Vector3i(1, 3, 5)));

{
Basis basis = Basis::from_scale(Vector3(1.2, 3.4, 5.6));
Transform3D transform = Transform3D(basis, Vector3());

converted = VariantUtilityFunctions::type_convert(transform, Variant::Type::BASIS);
CHECK(converted.get_type() == Variant::Type::BASIS);
CHECK(converted == basis);

converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::TRANSFORM3D);
CHECK(converted.get_type() == Variant::Type::TRANSFORM3D);
CHECK(converted == transform);

converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::STRING);
CHECK(converted.get_type() == Variant::Type::STRING);
CHECK(converted == Variant("[X: (1.2, 0, 0), Y: (0, 3.4, 0), Z: (0, 0, 5.6)]"));
}

{
Array arr;
arr.push_back(1.2);
arr.push_back(3.4);
arr.push_back(5.6);

PackedFloat64Array packed;
packed.push_back(1.2);
packed.push_back(3.4);
packed.push_back(5.6);

converted = VariantUtilityFunctions::type_convert(arr, Variant::Type::PACKED_FLOAT64_ARRAY);
CHECK(converted.get_type() == Variant::Type::PACKED_FLOAT64_ARRAY);
CHECK(converted == packed);

converted = VariantUtilityFunctions::type_convert(packed, Variant::Type::ARRAY);
CHECK(converted.get_type() == Variant::Type::ARRAY);
CHECK(converted == arr);
}
}

} // namespace TestVariantUtility

#endif // TEST_VARIANT_UTILITY_H
1 change: 1 addition & 0 deletions tests/test_main.cpp
Expand Up @@ -86,6 +86,7 @@
#include "tests/core/variant/test_array.h"
#include "tests/core/variant/test_dictionary.h"
#include "tests/core/variant/test_variant.h"
#include "tests/core/variant/test_variant_utility.h"
#include "tests/scene/test_animation.h"
#include "tests/scene/test_arraymesh.h"
#include "tests/scene/test_audio_stream_wav.h"
Expand Down

0 comments on commit 52b86a7

Please sign in to comment.