summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThaddeus Crews <repiteo@outlook.com>2024-10-29 19:25:45 -0500
committerThaddeus Crews <repiteo@outlook.com>2024-10-29 19:25:45 -0500
commitb8f626a0e12525024e1676b611fcf95c86ae93c5 (patch)
treed0970620adb14ad57ae913166513573fec595aee
parent6b70a6197fd1e4efe30f0659dd62d6f6b4c037de (diff)
parent98c89f17c4ba223a87c20e6fbe2149fd85ea2dde (diff)
downloadredot-engine-b8f626a0e12525024e1676b611fcf95c86ae93c5.tar.gz
Merge pull request #65962 from 4d49/format-object
Add `Object` support for `String.format`
-rw-r--r--core/string/ustring.cpp13
-rw-r--r--doc/classes/String.xml8
2 files changed, 19 insertions, 2 deletions
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<PropertyInfo> 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;
diff --git a/doc/classes/String.xml b/doc/classes/String.xml
index 588d0c73f9..44795af473 100644
--- a/doc/classes/String.xml
+++ b/doc/classes/String.xml
@@ -248,7 +248,7 @@
<param index="1" name="placeholder" type="String" default="&quot;{_}&quot;" />
<description>
Formats the string by replacing all occurrences of [param placeholder] with the elements of [param values].
- [param values] can be a [Dictionary] or an [Array]. Any underscores in [param placeholder] will be replaced with the corresponding keys in advance. Array elements use their index as keys.
+ [param values] can be a [Dictionary], an [Array] or an [Object]. Any underscores in [param placeholder] will be replaced with the corresponding keys in advance. Array elements use their index as keys.
[codeblock]
# Prints "Waiting for Godot is a play by Samuel Beckett, and Godot Engine is named after it."
var use_array_values = "Waiting for {0} is a play by {1}, and {0} Engine is named after it."
@@ -263,6 +263,12 @@
print("User {} is {}.".format([42, "Godot"], "{}"))
print("User {id} is {name}.".format([["id", 42], ["name", "Godot"]]))
[/codeblock]
+ When passing an [Object], the property names from [method Object.get_property_list] are used as keys.
+ [codeblock]
+ # Prints: Visible true, position (0, 0).
+ var node = Node2D.new()
+ print("Visible {visible}, position {position}".format(node))
+ [/codeblock]
See also the [url=$DOCS_URL/tutorials/scripting/gdscript/gdscript_format_string.html]GDScript format string[/url] tutorial.
[b]Note:[/b] The replacement of placeholders is not done all at once, instead each placeholder is replaced in the order they are passed, this means that if one of the replacement strings contains a key it will also be replaced. This can be very powerful, but can also cause unexpected results if you are not careful. If you do not need to perform replacement in the replacement strings, make sure your replacements do not contain placeholders to ensure reliable results.
[codeblock]