mirror of
https://github.com/godotengine/godot.git
synced 2024-11-24 13:12:42 +00:00
Merge pull request #65962 from 4d49/format-object
Add `Object` support for `String.format`
This commit is contained in:
commit
b8f626a0e1
@ -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;
|
||||
|
@ -248,7 +248,7 @@
|
||||
<param index="1" name="placeholder" type="String" default=""{_}"" />
|
||||
<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]
|
||||
|
Loading…
Reference in New Issue
Block a user