Godot Custom UI Menus With Control Nodes
In this article
Yes, Godot custom UI menus are created using Control nodes. The entire UI system in Godot, from a simple button to a full inventory screen, is built from the Control class and its many specialized subclasses, which you arrange inside a scene tree and style through themes and scripts.
Why Control Nodes Are the Foundation
Godot has two main node families for anything you see on screen: Node2D (for game objects like sprites, characters, and particles) and Control (for user interface elements). Control nodes are not just a different kind of sprite; they are a complete framework designed for interactive, responsive layouts. They handle input events like mouse clicks and keyboard focus, they can be anchored to any corner or edge of the screen, and they automatically scale when the window size changes. If you try to build a menu with Node2D nodes, you will have to manually calculate positions, handle resizing, and manage input yourself—Control nodes do all of this for you.
Core Control Nodes You Will Use
Godot ships with a rich library of Control subclasses. These are the ones you will reach for most often when building a menu:
- Panel / PanelContainer: A simple background box. Use it as the root of your menu to give it a distinct visual area.
- Button: The standard clickable element. Has variants like CheckButton, CheckBox, and OptionButton (a dropdown).
- Label / RichTextLabel: Displays text. RichTextLabel supports BBCode for styled text, making it great for descriptions or dialogue.
- LineEdit / TextEdit: For text input. LineEdit is single-line; TextEdit is multi-line.
- HSlider / VSlider: For volume or other numeric settings.
- ItemList: A scrollable list of selectable items, useful for save-game lists or settings options.
- TextureRect: Displays a texture or image, useful for icons or portraits.
- MarginContainer / VBoxContainer / HBoxContainer / GridContainer: These are Container nodes. They do not draw anything themselves; they automatically position and resize their child Control nodes. They are the backbone of any responsive layout.
How to Build a Menu: Scene Structure and Scripting
Building a menu is a two-part process: designing the scene tree and wiring up the logic. Here is a typical workflow for a main menu with a title, a "Start" button, and a "Quit" button:
- Create the scene: Create a new scene and set its root node to
Control. Name itMainMenu. - Add a layout container: Add a
VBoxContaineras a child. In the Inspector, set its Anchors to "Full Rect" so it fills the screen. Set its Alignment to Center so its children are centered vertically. - Add your title: Add a
Labelchild to the VBoxContainer. Set its text to your game's title. You can adjust the font size in the Inspector under Theme Overrides. - Add buttons: Add two
Buttonnodes to the VBoxContainer. Set their text to "Start Game" and "Quit". They will automatically stack vertically and center horizontally. - Connect signals: Select the "Start" button, go to the Node tab next to the Inspector, find the
pressedsignal, and double-click it. This creates a script on the root node with an empty function. Do the same for the "Quit" button. - Write the logic: In the script, fill in the functions. For example, to start the game, you might use
get_tree().change_scene_to_file("res://scenes/game.tscn"). For quitting, useget_tree().quit().
Containers vs. Manual Positioning
A common mistake for beginners is to drag Control nodes around the viewport to position them. While this works, it breaks as soon as the window resizes. Instead, you should use Containers and Anchors.
- Anchors: Every Control node has anchor points (left, top, right, bottom). If you set all anchors to 1 (bottom-right corner), the node will stick to the bottom-right of its parent. If you set them to 0.5, it will stick to the center. This is ideal for a single element like a health bar.
- Containers: For multiple elements that need to be arranged in a row or column, containers are superior. A
VBoxContainerwill automatically space its children vertically. AGridContainerwill arrange them in a grid. Containers also handle spacing and margins, so you do not have to calculate pixel offsets manually.
For a menu, the standard approach is to use a full-rect MarginContainer as the root, then place a CenterContainer inside it, and finally a VBoxContainer inside that to hold the actual buttons and labels. This structure keeps the menu perfectly centered regardless of screen size.
Styling with Themes and StyleBoxes
Default Godot UI looks flat and gray. To make your menu match your game's art style, you use Themes and StyleBoxes. A Theme is a resource that stores colors, fonts, and style variations for all Control types. You can create a Theme resource, assign it to your root Control node, and then every child will inherit it.
To style a button, you edit its StyleBox properties. A StyleBox is a resource that defines the background, border, and corner radius of a control. You typically create four StyleBoxes for a button: Normal, Hover, Pressed, and Focus. You can set these in the Inspector under Theme Overrides > Styles. For a more complex look, you can create a Theme resource in the FileSystem dock and edit it in a dedicated Theme editor, which lets you set default styles for all buttons, labels, and panels at once.
Handling Input and Focus
Menus need to respond to mouse clicks, but they also need to work with a keyboard or gamepad. Control nodes handle this through the gui_input signal and the focus system.
- Mouse: The
pressedsignal on a Button handles clicks. For custom controls, you can override the_gui_input(event)function to catchInputEventMouseButtonevents. - Keyboard/Gamepad: Control nodes can receive focus. When a button has focus, pressing Enter or Space activates it. You can navigate between controls using the arrow keys if you set
focus_neighborproperties in the Inspector. For gamepads, you need to set thefocus_neighborproperties to allow the D-pad to move between buttons. Godot 4 also has a built-inControlproperty calledfocus_mode; setting it toAllallows the node to be focused.
A common pattern is to connect the pressed signal for mouse users, but also connect the focus_entered signal to play a sound or change a visual, and the pressed signal to trigger the action. For gamepad support, you must ensure that your buttons have focus_neighbor set correctly, otherwise the player cannot navigate the menu.
Common Pitfalls and How to Avoid Them
Even experienced developers run into a few recurring issues with Godot UI. Here are the most common ones and their solutions:
- UI not scaling: If your UI looks tiny on a 4K monitor and huge on a 720p monitor, you have not set a base resolution. Go to Project Settings > Display > Window and set the Viewport Width and Height (e.g., 1920x1080). Then set Stretch Mode to "canvas_items" and Aspect to "keep". This makes the UI scale proportionally.
- Buttons not clickable: This usually happens when another Control node is drawn on top of the button, intercepting the mouse clicks. Check the Z Index property of your nodes, or look for an invisible
PanelorColorRectcovering the button. Also, ensure the button'sMouse Filteris set to "Stop" (the default). - Containers not showing children: If you add a Control node to a Container but it does not appear, the Container might have a
Separationvalue that is too large, or the child has aCustom Minimum Sizeof zero and the container is shrinking it to nothing. Set aCustom Minimum Sizeon your buttons or labels to give them a non-zero size. - Text blurry: This is often caused by the UI being scaled by a non-integer factor. Using the
canvas_itemsstretch mode and setting the base resolution to a common target (like 1280x720) usually fixes this. Also, make sure your font size is not an odd number when scaling is involved.
Frequently Asked Questions
Can I use Control nodes inside a 3D world? Yes, but they are rendered on a flat layer on top of the 3D viewport. To have UI that exists in 3D space (like a health bar floating over a character), you use a SubViewport with a Control node inside it, and then display that viewport on a Sprite3D or QuadMesh. This is more advanced, but it is the standard way to do "world-space" UI.
What is the difference between a Panel and a PanelContainer? A Panel is a simple, static background. It does not resize its children. A PanelContainer is a Container that automatically resizes to fit its children, and it also draws a panel background behind them. Use PanelContainer when you want a background box that wraps around a group of controls, like a tooltip or a dialog box.
How do I make a popup menu or a dropdown? Godot has a built-in PopupMenu node. You can create one as a child of your main scene and call its popup() method to show it at the mouse position. For a dropdown that is attached to a button, use an OptionButton which is a button that displays a popup when clicked.
Is it better to build UI in code or in the editor? Both are valid. The editor is great for layout and visual feedback. Code is better for generating dynamic lists (like a list of save files) or for UI that is highly repetitive. Most projects use a hybrid: static parts in the editor, dynamic parts generated in code using preload() and instantiate().
How do I make a scrollable list of items? Use a ScrollContainer as the parent, and inside it place a VBoxContainer. Add your items (buttons, labels, etc.) as children of the VBoxContainer. The ScrollContainer will automatically show scrollbars when the content exceeds its size. For very large lists, consider using an ItemList node instead, which is more memory-efficient.
Why is my button text not centered? Buttons have a Text Alignment property (horizontal) and a Vertical Alignment property (vertical). By default, they are centered, but if you have changed them or if you are using a custom StyleBox with extra padding, the text might appear off-center. Check the Theme Overrides > Fonts and Colors in the Inspector, and ensure the Content Margins of your StyleBox are set to a small value or zero.