From 98c89f17c4ba223a87c20e6fbe2149fd85ea2dde Mon Sep 17 00:00:00 2001 From: Mansur Isaev <737dab2f169a@mail.ru> Date: Fri, 16 Sep 2022 14:12:27 +0400 Subject: Add `Object` support for `String.format` --- core/string/ustring.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'core/string') diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 4e9eb922f6..28319fc643 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -33,6 +33,7 @@ #include "core/crypto/crypto_core.h" #include "core/math/color.h" #include "core/math/math_funcs.h" +#include "core/object/object.h" #include "core/os/memory.h" #include "core/string/print_string.h" #include "core/string/string_name.h" @@ -4064,8 +4065,18 @@ String String::format(const Variant &values, const String &placeholder) const { for (const Variant &key : keys) { new_string = new_string.replace(placeholder.replace("_", key), d[key]); } + } else if (values.get_type() == Variant::OBJECT) { + Object *obj = values.get_validated_object(); + ERR_FAIL_NULL_V(obj, new_string); + + List props; + obj->get_property_list(&props); + + for (const PropertyInfo &E : props) { + new_string = new_string.replace(placeholder.replace("_", E.name), obj->get(E.name)); + } } else { - ERR_PRINT(String("Invalid type: use Array or Dictionary.").ascii().get_data()); + ERR_PRINT(String("Invalid type: use Array, Dictionary or Object.").ascii().get_data()); } return new_string; -- cgit v1.2.3