godot: Documentation is missing example how to format arrays with string interpolation

Godot version

ef025711a

System information

ArchLinux x11

Issue description

var array := ["a", "r", "r", "a", "y", "s"]
print("fstrings dont work with %s anymore" % array)

results in:

ERROR: not all arguments converted during string formatting
   at: validated_evaluate (core/variant/variant_op.h:947)

I expect it to result in fstrings dont work with ["a", "r", "r", "a", "y", "s"] anymore.

Steps to reproduce

run mrp or run example code

Minimal reproduction project

bug.zip

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 2
  • Comments: 15 (15 by maintainers)

Most upvoted comments

I think it should have a section:

Formatting arrays

To format a normal Array (non Packed*Array), encase the array in another array:

var array_to_format = [1, 2, 3]
print("array is: %s" % [array_to_format]) # note the array enclosing the array
# Output: "array is: [1, 2, 3]"

If you have a Packed*Array, simply format it like any other object: print("packed array: %s" % PackedStringArray(my_array)

I think this is correct behavior, in a string % array expression it is not generally possible to determine whether array is a single replacement value or a list containing some number of replacement values.

We could check how many replacement sequences string contains, but only if the string is a constant. However I think this is less consistent, I would prefer to always require [array].

But we could pay attention to this in the docs.

What do you think is missing from the documentation?

There are no examples where the right operand is an array variable, only literals. We can also write about errors when the number of array/dictionary elements and substitution sequences do not match, and recommend using the format() method instead of the % operator if the mismatch is expected.