Merge pull request #73244 from vonagam/fix-int-in-float-typed-array

GDScript: Fix usage of ints with typed array of floats
This commit is contained in:
Rémi Verschelde 2023-02-13 23:43:09 +01:00
commit 02a9a31b1b
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 6 additions and 2 deletions

View file

@ -73,7 +73,7 @@ struct ContainerTypeValidate {
return type != p_type.type || class_name != p_type.class_name || script != p_type.script; return type != p_type.type || class_name != p_type.class_name || script != p_type.script;
} }
// Coerces String and StringName into each other when needed. // Coerces String and StringName into each other and int into float when needed.
_FORCE_INLINE_ bool validate(Variant &inout_variant, const char *p_operation = "use") const { _FORCE_INLINE_ bool validate(Variant &inout_variant, const char *p_operation = "use") const {
if (type == Variant::NIL) { if (type == Variant::NIL) {
return true; return true;
@ -89,6 +89,9 @@ struct ContainerTypeValidate {
} else if (type == Variant::STRING_NAME && inout_variant.get_type() == Variant::STRING) { } else if (type == Variant::STRING_NAME && inout_variant.get_type() == Variant::STRING) {
inout_variant = StringName(inout_variant); inout_variant = StringName(inout_variant);
return true; return true;
} else if (type == Variant::FLOAT && inout_variant.get_type() == Variant::INT) {
inout_variant = (float)inout_variant;
return true;
} }
ERR_FAIL_V_MSG(false, "Attempted to " + String(p_operation) + " a variable of type '" + Variant::get_type_name(inout_variant.get_type()) + "' into a " + where + " of type '" + Variant::get_type_name(type) + "'."); ERR_FAIL_V_MSG(false, "Attempted to " + String(p_operation) + " a variable of type '" + Variant::get_type_name(inout_variant.get_type()) + "' into a " + where + " of type '" + Variant::get_type_name(type) + "'.");

View file

@ -86,7 +86,8 @@ func test():
var typed_int := 556 var typed_int := 556
var converted_floats: Array[float] = [typed_int] var converted_floats: Array[float] = [typed_int]
assert(str(converted_floats) == '[556]') converted_floats.push_back(498)
assert(str(converted_floats) == '[556, 498]')
assert(converted_floats.get_typed_builtin() == TYPE_FLOAT) assert(converted_floats.get_typed_builtin() == TYPE_FLOAT)