Exploring UI node anchors
- Resize and anchor a
Control
node. - Use the Anchor Presets to quickly set anchors.
- Experiment with different anchor settings.
Nathan
Founder and teacher at GDQuest
Anchors and containers
- The element places itself: this is when UI elements, like health bars, icons, or buttons, decide where they should position themselves relative to their parent. For example, a health bar might always stay at the top-left corner of the game window or at the bottom. We achieve this with anchors in Godot.
- The parent places children: this is when parents decide where their children go. For example, a grid component could place inventory items in a grid layout, and a horizontal box could place buttons next to each other horizontally. We achieve this with containers in Godot.
Some CSS parallels for the webdevs among youIf you've created websites before and used HTML and CSS: Note that UI frameworks, whether in Godot or other desktop and mobile applications, have some differences with respect to CSS. You won't be able to directly apply your knowledge of web development but you can still draw some parallels to help you understand concepts faster.
- Anchors are a bit like
position: absolute
. They allow elements to place themselves relative to their parent. - Containers are like
display: flex
ordisplay: grid
. They allow parent elements to place their children in a specific layout.
- A container to place the children in a row. The parent would be the outline of the navigation bar and the children would be the logo and the three buttons.
- Anchors to position the notification circles relative to the buttons.
HBoxContainer
node to place the children in a row (HBox stands for horizontal box). It's one of the most useful Godot containers, along with VBoxContainer
, which arranges children vertically, and GridContainer
, which arranges children in a grid.VBoxContainer
node to create dialogue choices for the player and align them vertically. For now, in this module, we'll work exclusively with anchors to place elements in the scene.Control
node proportionally to its parentIf a Control node has no parent, then the anchors are relative to the game window.
Control
node. There are four anchors: top left, top right, bottom left, and bottom right. This image illustrates a few anchor positions:Create and resize a TextureRect node
Control
node as the root that covers the entire game window. You can see this thanks to the large bounding box and the four green pins at the four corners of the node.My Control node is small and at the top left of the viewportWhen creating a To do this, select the
Control
node with the shortcut the add child node shortcut, the node is created at the top left of the viewport with a small size. In that case, you need to change its anchor layout to make it cover the entire game window.Control
node in the Scene dock. In the toolbar above the viewport, click the Anchor Presets button and select the Full Rect preset. This will resize the node to cover the entire game window. We'll talk more about anchor layouts in a moment.dialogue.tscn
.TextureRect
. A TextureRect
is a type of control node that displays a texture image.TextureRect
inherits from Control
nodes, so it can use anchors, while sprites are regular 2D nodes and can't use them. You can think of TextureRect
as a Sprite2D
that can be used in the engine's UI system.TextureRect
node as a child of the Dialogue node. You can press ctrla (on Mac: ⌘a) to open the Create New Node dialog. A small empty orange box appears in the viewport.TextureRect
node.TextureRect
node in the Scene dock to apply it to the node. This is equivalent to dragging and dropping the file to the Texture property in the Inspector. This resizes the node in the viewport to match the texture size.TextureRect
node determines how the texture stretches. By default, it's set to Keep Size, which forces the node to match the texture's size. Instead, let's allow our TextureRect
node to dictate the size of the texture.1920
by 1080
(these are the dimensions of our game window).TextureRect
node is anchored to the top left of the game window by default, so it doesn't resize with the window. It just stays positioned at the top left.I cannot resize the window!On Windows, if your screen resolution is smaller or equal to the game's resolution (and window size). If you're experiencing this issue, you can try the following steps to reduce the window size while preserving the ability to follow along the lessons: The Window Width Override and Window Height Override settings change the size of the game window without affecting the editor viewport. You can use these settings to run your game in different resolutions.
- Open the ProjectProject Settings... window.
- Turn on the Advanced Settings radio button at the top right of the window.
- Navigate to the DisplayWindow category.
- Find the Window Width Override and Window Height Override settings.
- Set Window Width Override to 1280 and Window Height Override to 720.
Change the anchors
TextureRect
node selected, click the Anchor Presets button in the toolbar above the viewport and select the Full Rect preset.My TextureRect node becomes small, it does not resize to cover the screenAn anchor layout is relative to the parent To address this: The parent node should now cover the entire game window. The
Control
node. If the parent node is small, the child node will also be small. In this case, the parent node is the Control
node at the root of the scene.- Select the parent
Control
node in the Scene dock. - Click the Anchor Presets button in the toolbar above the viewport
- Select the Full Rect preset.
TextureRect
node should automatically resize to cover the screen as well. If not, you can select the TextureRect
node again and apply the Full Rect preset to it too.TextureRect
node. This is the top right anchor. Drag it to the left until it reaches the center of the top edge of the node.TextureRect
node. Locate the LayoutTransformSize property in the Inspector and halve the width and height of the node. You can run math in property fieldsYou can do math in any number field in Godot
You can actually write math expressions in all numerical fields in the editor. Divide, multiply, or add numbers right there! The engine will do the math for you.
Size
property and typing /2
after the number, then pressing ↵.Try Answering this
Control
node, and that they can be moved manually or placed with presets.Let's explore
TextureRect
nodes to the scene and experiment with them.Tip 1: Anchor values
0
to 1
, where 0
is the top left corner and 1
is the bottom right corner.How come I can drag the anchors beyond 0 and 1 then?Technically, you can drag your anchors outside the limits of your viewport (delineated by the red, green and blue lines), but that's not useful for most applications. It would resize your texture proportionally to a distance outside your game screen. Feel free to try it and run the scene! That's what this lesson's for.
Tip 2: Anchor presets
Control
node, you can apply anchor presets from two places. One is the Anchor Presets button in the toolbar above the viewport. (That's what we used earlier in the lesson to select the Full Rect preset). You can also select anchor presets in the Inspector with the property LayoutAnchor Presets.Changing the anchor presets moves and resizes the nodeWhen you change the anchors presets, your node's size will also change. The anchor presets are a quick way to set the anchors, position, and size of UI elements at the same time.
Tip 3: Duplicate nodes
Tip 4: Move anchors
Control
node with the Select Mode or the Move Mode active, you change its offset. The offset is the distance between the node's anchors and its position. You can also move the anchors with the node without changing the offset by clicking the anchor button in the toolbar above the viewport.Nathan
Founder and teacher at GDQuest
Challenge:
click to reveal Hint
click to reveal Hint
click to reveal Hint
click to reveal Hint
Challenge:
click to reveal Hint
click to reveal Hint
Challenge:
TextureRect
nodes that stay in the top left and bottom right corners of the screen, respectively. They shouldn't resize with the window.click to reveal Hint
Recap
Control
nodes relative to their parent, while containers place their children in a specific layout. We mostly use anchors in this module, but we'll also use containers in the next module to create a branching dialogue system.- There are two ways to position UI elements: anchors and containers.
- Anchors affect the position and size of a
Control
node relative to its parent (or the game window). - You can use anchor presets to quickly set anchors.
- You can also fine-tune how a node resizes with the window by moving its anchors manually.
- Experimentation is key to understanding new concepts and breaking out of over-reliance on tutorials.
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.