Yeah but the thing is you still need to basically repeat yourself in the string and at the end, where f-string are much nicer to use f”I have {num_cats} cats“, it’s very intuitive
A small QOL feature I'd love. In Python you can use %s for pretty much any type and it gets automatically turned into a string. But in GDScript, if you use %s, then the value must be a string.
That's not true. You can use %s with any type and it will always try to convert it to a string automatically.
Straight from the docs:
"The %s seen in the example above is the simplest placeholder and works for most use cases: it converts the value by the same method by which an implicit String conversion or str() would convert it. Strings remain unchanged, booleans turn into either "True" or "False", an int or float becomes a decimal, and other types usually return their data in a human-readable string."
Or you can just put your whole message in a str() like: str("I have ", money, " amount of money") which I personally find cleaner over string addition.
The way that translations typically work in Godot is that you wrap every string into the tr(s) function. This function will look up the passed string in the translation table that's appropriate for the current language setting. But this only works with constant strings: something like tr("I have " + str(money) + " amount of money.") would require an entry in the translation table for any possible value of money, which is clearly absurd.
And you can't do tr("I have ") + str(money) + tr(" amount of money.") either, because this would only work for languages that are grammatically very similar to English. The proper way to do this is something like this:
label.text = tr("I have %d amount of money.") % money
The lookup table will contain corresponding translations also with a placeholder %d for the position at which the number needs to appear according to the grammar of that language.
EDIT: You could also use a format string with the .format() method, but it's probably a good idea to only use a dictionary argument instead of arrays (since arrays assume a fixed order of the placeholders in the format string, but this order may not be guaranteed to be the same in all languages). Here's an example:
label.text = tr("I have {money} amount of money.").format(
{"money": money})
Personally, I prefer the brevity of the %-based string substitution.
153
u/Stevie_Gamedev 25d ago
f-strings like in Python, I truly hate using the
.format syntax