0 votes

I have a "Notification" scene consisting of a Container node and two children: a Sprite node and a Label node.

There is a manual init() method for this scene that I call when I instance the scene. This init() method takes a string as argument and passes it into the Label.

After I call the init() method, I add the scene as a child of my "HUD" node, which is itself just a Container.

What I want is for the Sprite to always appear directly to the left of the Label, and for them both to be centered on my screen. If the text of the label is long, I want the label to stretch but still be centered. This will push the Sprite left.

For example, I want to be able to accommodate both of these:

enter image description here
enter image description here

(The red square is the Sprite, the gray rectangle is the Label.)

I have tried about every container hierarchy and anchor combination that I can think of, but just can't seem to get it to work.

Any ideas?

in Engine by (1,602 points)

1 Answer

+1 vote
Best answer

Try this.

- HBoxContainer (alignment : center)
    - TextureRect
    - Label

It's better to use only Control nodes to consist UI instead of mixing Control & Node2D nodes.

enter image description here

by (9,782 points)
selected by

I wasn't familiar with TextureRect. Works like a charm!

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.