godot icon indicating copy to clipboard operation
godot copied to clipboard

4.0 STRING.format bug

Open kkoang opened this issue 3 years ago • 1 comments

Godot version

4.0.beta6.official (7f8ecffa5)

System information

macOS, Mac mini, Intel UHD Graphics 630

Issue description

Xnip2022-11-27_20-54-02

Steps to reproduce

var arr = [1, 2, 3]
#arr = [1, 2] # this line is correct
var template = "[Variant:{}] [type:{}] [value:{}]"
print(template.format([TYPE_ARRAY, typeof(arr), arr],"{}"))

Minimal reproduction project

20221127STRING_format_Error.zip

kkoang avatar Nov 27 '22 13:11 kkoang

I believe this is intentional, and it doesn't seem exclusive to 4.0.

Some additional handling is performed when values is an array. If placeholder does not contain an underscore, the elements of the array will be used to replace one occurrence of the placeholder in turn; If an array element is another 2-element array, it'll be interpreted as a key-value pair.

# Prints: User 42 is Godot.
print("User {} is {}.".format([42, "Godot"], "{}"))
print("User {id} is {name}.".format([["id", 42], ["name", "Godot"]]))

https://github.com/godotengine/godot/pull/68838 may make it slightly clearer this is how it works. See also the GDScript Format String tutorial.

Mickeon avatar Nov 27 '22 14:11 Mickeon

There are two usages with passing Array:

# Prints: User 42 is Godot.
print("User {} is {}.".format([42, "Godot"], "{}")) # A
print("User {id} is {name}.".format([["id", 42], ["name", "Godot"]])) # B

If your array contains array, it will use usage B to format the string. You would get an error STRING.format Inner Array size != 2.

To format strings with usage A, try template.format([TYPE_ARRAY, typeof(arr), str(arr)],"{}").

zaevi avatar Nov 30 '22 07:11 zaevi

I just realised that part of the original issue was not just the documentation. Rather, the error message that comes with an invalid array, which is needlessly vague. Still, the documentation part has been solved, so this could be closed?

Mickeon avatar Dec 30 '23 19:12 Mickeon

Could you identify the specific PR fixing this?

AThousandShips avatar Dec 30 '23 19:12 AThousandShips

I suppose that embellishing the description in https://github.com/godotengine/godot/pull/68838 is not enough. This obscure usage of the method may really need to be more verbose.

Mickeon avatar Dec 30 '23 19:12 Mickeon