diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp index ae9727fe79ff..624a6f5e060f 100644 --- a/core/variant/variant_construct.cpp +++ b/core/variant/variant_construct.cpp @@ -68,11 +68,13 @@ void Variant::_register_variant_constructors() { add_constructor>(sarray("from")); add_constructor>(sarray("from")); add_constructor>(sarray("from")); + add_constructor>(sarray("from")); add_constructor>(sarray()); add_constructor>(sarray("from")); add_constructor>(sarray("from")); add_constructor>(sarray("from")); + add_constructor>(sarray("from")); add_constructor>(sarray()); add_constructor>(sarray("from")); diff --git a/core/variant/variant_construct.h b/core/variant/variant_construct.h index 34d228f4d296..f52cc43914c0 100644 --- a/core/variant/variant_construct.h +++ b/core/variant/variant_construct.h @@ -222,6 +222,64 @@ public: } }; +template +class VariantConstructorFromString { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + if (p_args[0]->get_type() != Variant::STRING) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; + return; + } + + VariantTypeChanger::change(&r_ret); + const String &src_str = *VariantGetInternalPtr::get_ptr(p_args[0]); + + if (r_ret.get_type() == Variant::Type::INT) { + r_ret = src_str.to_int(); + } else if (r_ret.get_type() == Variant::Type::FLOAT) { + r_ret = src_str.to_float(); + } + } + + static inline void validated_construct(Variant *r_ret, const Variant **p_args) { + VariantTypeChanger::change(r_ret); + const String &src_str = *VariantGetInternalPtr::get_ptr(p_args[0]); + T ret = Variant(); + if (r_ret->get_type() == Variant::Type::INT) { + ret = src_str.to_int(); + } else if (r_ret->get_type() == Variant::Type::FLOAT) { + ret = src_str.to_float(); + } + *r_ret = ret; + } + + static void ptr_construct(void *base, const void **p_args) { + String src_str = PtrToArg::convert(p_args[0]); + T dst_var = Variant(); + Variant type_test = Variant(dst_var); + if (type_test.get_type() == Variant::Type::INT) { + dst_var = src_str.to_int(); + } else if (type_test.get_type() == Variant::Type::FLOAT) { + dst_var = src_str.to_float(); + } + PtrConstruct::construct(dst_var, base); + } + + static int get_argument_count() { + return 1; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::STRING; + } + + static Variant::Type get_base_type() { + return GetTypeInfo::VARIANT_TYPE; + } +}; + class VariantConstructorCallableArgs { public: static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { diff --git a/doc/classes/float.xml b/doc/classes/float.xml index e3938fb5d588..a196021249ac 100644 --- a/doc/classes/float.xml +++ b/doc/classes/float.xml @@ -26,6 +26,13 @@ Constructs a [float] as a copy of the given [float]. + + + + + Converts a [String] to a [float], following the same rules as [method String.to_float]. + + diff --git a/doc/classes/int.xml b/doc/classes/int.xml index 65ab5d46560a..93fc8386c6c9 100644 --- a/doc/classes/int.xml +++ b/doc/classes/int.xml @@ -52,6 +52,13 @@ Constructs an [int] as a copy of the given [int]. + + + + + Converts a [String] to an [int], following the same rules as [method String.to_int]. + +