How do I center text on a RichTextLabel? (beginner)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cookieKing_

In my game, I want to make it so a rich text labels shows the score, but I want the text to be centered.

:bust_in_silhouette: Reply From: GameSchool

Try enabling bbcode and write [center] before the text. This should do the job.

It says Identifier “center” not declared in current scope

cookieKing_ | 2023-05-27 17:49

If you enabled in the UI the bbcode, the string inside the script should like like

self.text = "[center]Your text"

GameSchool | 2023-05-27 17:54

Now it says "expected end of statement after expression, found “Identifier” instead.

self.text = [center]score.score

cookieKing_ | 2023-05-27 17:59

Because you’re using a variable, it will look like this:

self.text = "[center]" + String(score.score)

I don’t remember if it’s String()or str()

GameSchool | 2023-05-27 18:02

It’s not showing any text when I press play and I know I turned BBcode on

cookieKing_ | 2023-05-27 18:11

You had a version that showed the score?
If you had, just add "[center]" + to it.

You can always test possible strings in the UI before running the game by writing the same text ([center]1 for example) in the text property in the UI.

GameSchool | 2023-05-27 18:29

There could be many different reasons why your label isn’t showing. Check this answer for possibilities:

RichTextLabel broken after upgrade to Godot 4? - Archive - Godot Forum

Also to center it vertically you can put it in a VBox and set the mode to Center

aidave | 2023-05-27 22:09