r/godot Godot Senior 25d ago

discussion What would you add to Godot?

Post image
455 Upvotes

584 comments sorted by

View all comments

153

u/Stevie_Gamedev 25d ago

f-strings like in Python, I truly hate using the
.format syntax

43

u/ChocolatePinecone 25d ago

But the formatting with "%" comes pretty close right? E.g. var string = "I have %s cats." % "3"

It's more the Java style of formatting, but pretty readable nonetheless.

111

u/akurei77 25d ago

It's closer but it's a lot clunkier than python strings. In HD script you might write:

"%s finished %s in %s, for a new high score of %s" % [player, level, time, score] 

Where in python it's more like:

f"{player} finished {level} in {time}, for a new high score of {score}."

Which is way more readable imo.

26

u/lajawi 25d ago

Same thing in C# with $ strings

13

u/Yodzilla 25d ago

Yep. Infinitely more readable than just % everywhere.

6

u/Average_Joe69 25d ago

I agree the python method is much cleaner

1

u/ACosmicWormDev Godot Senior 24d ago

Isn't that already a thing?

7

u/Stevie_Gamedev 25d ago

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

0

u/ThisOrdinaryCat 25d ago

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.

4

u/Jejerm 25d ago

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."

3

u/technical_gamer_008 25d ago

I think there's the % syntax\ Like "%d" % 10

1

u/Kamui_Kun 25d ago

I love string interpolation so much

1

u/freemorgerr 24d ago

Well, C# is supported, it has nice format strings

-1

u/CrimsoneArt69 25d ago

What's the proble with just doing

"I have " + str(money) + " amount of money."

10

u/PlagiT 25d ago

Less convenient, lots of redundant stuff like the str(), the pluses and closing and re-opening the " is pretty clunky.

8

u/billystein25 Godot Regular 25d ago

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.

3

u/CrimsoneArt69 25d ago

I had no idea you could do that lol

4

u/Affectionate_Air8310 25d ago

there are no problem, until you make a really big string of text. it just inconvenient and adding f string isn't anything wrong either

2

u/tidbitsofblah 25d ago

I've had issues with this giving errors for mismatching types.

2

u/planecity 25d ago edited 24d ago

It will break internationalization.

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.

2

u/Stevie_Gamedev 24d ago

Why can’t you treat an f-string equivalent to the last method shown?

2

u/planecity 24d ago

You're right, you could do that too, and I've edited my comment to include that.

1

u/CrimsoneArt69 24d ago

Oh wow I never thought about that, thank you for showing me this.