The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hello everybody !

I have Labels that show informations about the player:

acceleration_label.text = "acceleration........." + str(player_acc)
velocity_label.text = "velocity................." + str(player_vel)

the output is for instance:
acceleration.........3.4
velocity.................209.7

Is there an easy way to have this instead ?:
acceleration.........3.4
velocity.............209.7

Or do I have to code it myself ?

Thanks for your time

in Engine by (92 points)

1 Answer

+2 votes
Best answer

A few thoughts...

If you want both the text label and the value to be rendered as part of a single string in a single Label component, you'll need to use a fixed-width font. In that case, each character in the font will take the same amount of space. With that, you could easily calculate the amount of space needed between a given text label and its value to properly align the columns in multiple rows. Without a fixed-width font, it's probably not possible.

Alternatively, if you could represent the two pieces of data in each row in 2 separate Label components (1 for the text and 1 for the number), it'd be easier to handle the alignment. In that case, you'd just need to separate the two "columns" of labels appropriately, and then format your values to properly align. The advantage here is that you don't have to deal with the variable-length text portions also...

Though, no matter what, you'll have to do some work yourself.

One more thought, though I'm not overly familiar... Perhaps the RichTextLabel node supports some formatting options that might be useful here?

by (22,674 points)
selected by

Ok thanks,
The fixed-width font worked with a bit of code on the strings

I multiplied the length of the value string ("209.7".length() = 5) with the number of spaces and subtracted the result from a constant number of spaces
so:
".........." - (5 * ".") = "....."

then you can add "209.7" and you have "....." + str(209.7) = ".....209.7"

for those interested, here is the code

var spaces = subTxt("     ", multTxt(" ", str(velocity).length()))
velocity_label.text = "VELOCITY:" + spaces + str(speed)

func multTxt(txt, nb):
    var newText = ""
    for _i in range(nb):
        newText += txt
    return newText

func subTxt(txt1, txt2):
    var newSize = txt1.length()-txt2.length()
    return txt1.substr(0, newSize)

That way, the total string is always the same length.
I have a bit of flickering when the values changes fast but it's ok, all my players informations are aligned.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.