How to Make a Main Menu in Godot 4
Originally published on CodingCush.
A main menu is the first screen your player sees, and in Godot 4 it is a scene like any other: a Control that fills the window, a stack of buttons in the middle, and a short script that loads the game when Start is pressed.
This post builds one for a small game called Space Rescue: a title, Start, How to Play and Quit buttons, plus a small panel that opens and closes. Every line of code here was run in Godot 4.3. If you prefer watching, the video version is in the Godot 4 Full Course in Hindi, free on YouTube.
The menu we are building
The project has two scenes: menu.tscn is the menu, and game.tscn is the game it loads. Our game scene is a placeholder with one label that says "You are in the game now."
In a real project, point Start at your own level, for example the player from how to move a character in Godot 4 with GDScript.
The menu itself is built from six nodes: a Control root, a CenterContainer, a VBoxContainer, a Label for the title, three Buttons, and a PanelContainer that stays hidden until the player asks for help. Buttons in Godot respond to mouse, keyboard and touch without extra code.
The scene tree, piece by piece
Start with a new scene whose root is a Control, the base node for every user interface element in Godot. Select it, open the Layout menu at the top of the 2D view, and pick Anchors Preset, then Full Rect, so the menu fills the screen at any window size. Skip this step and the menu fills only part of the window.
Add a CenterContainer as a child of the root, with the same Full Rect anchors. It puts its children in the middle of the space it gets, so the title and buttons stay centred on any screen size, with no numbers to recalculate.
Inside the CenterContainer, add a VBoxContainer. It stacks its children top to bottom and decides their positions for you. Set its Separation to 16 so the rows breathe.
Add a Label first: text SPACE RESCUE, font size 64, alignment centre. Then add three Buttons, Start Game, How to Play and Quit, each 260 by 56 with a font size of 24, big enough to tap on a phone screen.
The last node is the How to Play panel. Add a PanelContainer as a child of the root, not of the VBoxContainer, because it should float over the menu instead of joining the stack. Set its Visible property off, so the player never sees it until the button is pressed. Inside it, put a VBoxContainer with a Label that explains the controls and a Button that says Got it. The panel draws its own background box for the text to sit on.
One rule runs through all of this. Once a Control sits inside a container, the container places it: children give up their own positioning, and dragging them by hand is ignored or undone the next time the parent resizes. So do not drag the buttons into place. Set the separation and the sizes, and let the containers do the arranging.
Godot's guide to using containers lists every container type, and most menus need only the two used here.
The Godot 4 editor showing the Space Rescue main menu scene: the scene tree on the left and the title with three buttons in the 2D view.
The script that makes the buttons work
Buttons that do nothing are just rectangles. Attach this script to the menu root:
extends Control
@onready var start_button: Button = $Center/MenuBox/StartButton
@onready var howto_button: Button = $Center/MenuBox/HowToButton
@onready var quit_button: Button = $Center/MenuBox/QuitButton
@onready var howto_panel: PanelContainer = $HowToPanel
@onready var close_button: Button = $HowToPanel/PanelBox/CloseButton
func _ready() -> void:
start_button.pressed.connect(_on_start_button_pressed)
howto_button.pressed.connect(_on_howto_button_pressed)
quit_button.pressed.connect(_on_quit_button_pressed)
close_button.pressed.connect(_on_close_button_pressed)
func _on_start_button_pressed() -> void:
get_tree().change_scene_to_file("res://game.tscn")
func _on_howto_button_pressed() -> void:
howto_panel.visible = true
func _on_close_button_pressed() -> void:
howto_panel.visible = false
func _on_quit_button_pressed() -> void:
get_tree().quit()
The @onready lines grab the nodes when the scene loads, using the same paths you see in the Scene dock. The dollar sign is shorthand for get_node. Rename a node in the dock and the path in the script must change to match, or the game reports the node was not found.
The four connect lines in _ready are where the buttons come alive. pressed is a signal a Button emits when the player clicks or taps it, and connect tells Godot which function to run when it fires. How Godot signals work explains the same pattern in full, but for a menu this code version is the shortest path.
The Start handler calls get_tree().change_scene_to_file with the path of the game scene. The path must match the file exactly, starting with res://, spelling and .tscn ending included. The call returns an error code, 0 when it worked, and the switch happens at the end of the current frame. The SceneTree reference lists the other ways to change scenes.
I ran this exact script in Godot 4.3 and pressed Start in a headless test: the game scene loaded with its label showing.
The How to Play handler sets the panel visible to true, and Got it sets it back to false. The Quit handler calls get_tree().quit(), which closes the running game. It does nothing inside the editor, because the editor is not the running game, so test it by running the project.
Anchors or containers
New Godot users mix these two up, because both move controls around. They solve different problems:
- Anchors: Pin a control to its parent's edges, so it stretches or stays put as the window resizes. Reach for it when a panel must fill the screen, or a button must stay in one corner.
- Containers: Arrange children in a row, column or grid, and redo the layout on every resize. Reach for it when several buttons or labels must sit in a neat stack.
A menu uses both: anchors so it always fills the window, containers so the buttons stack neatly in the middle.
Where menus usually break
Most menu bugs have the same shape: the screen looks right, and nothing happens when you click.
- First, the button does nothing. The
pressedsignal was never connected, which usually means the connect line is missing from_ready, or the script sits on the wrong node. Check the Node dock: select a button, open the Node tab, and look for pressed. - Second, the menu fills only part of the window. The root Control never got its Full Rect anchors. Select the root, open the Layout menu, pick Full Rect, then do the same for the CenterContainer.
- Third, Start shows an error instead of the game. The path in
change_scene_to_fileis wrong, and the error names the path it could not find. Copy the path from the FileSystem dock instead of typing it. - Fourth, Quit seems broken. It only closes a running game, so pressing it while the editor is open does nothing visible. Run the project, press Quit, and the game window closes. On a web export the same button often does nothing, because a web page cannot close its own tab, so hide the Quit button on web builds or swap it for a link back to your site.
Questions beginners actually ask
Can I build the whole menu in code instead of the editor? Yes. Button.new() creates a button: set its text, connect its pressed signal and add it as a child, and the docs show the same pattern. The editor is faster for a first menu, because you see the layout as you build it. Code is the right choice when buttons are generated at run time, like a list of saved games.
Why do my buttons look plain and grey? They use Godot's default theme, which is plain on purpose. A theme is a separate job: it sets colours, fonts and borders for every control at once. Learn the layout first, then dress it up.
How do I go back to the menu from the game? The same call, in the other direction: from the game, call get_tree().change_scene_to_file("res://menu.tscn") on a pause menu button or a key press. The menu scene loads fresh each time, so the How to Play panel starts hidden again.
Does the menu need its own script file? One script on the menu root covers every button. Splitting the handlers across several scripts only makes the connections harder to find later.
Your turn
You now have a menu with a title, three working buttons and a panel, built from six nodes and one short script.
Try two small changes before you move on: add a Settings button that opens a second panel, and give the title screen a living character with how to animate a sprite in Godot 4.
When the menu feels like yours, the next lesson is waiting in the Godot 4 Full Course in Hindi, free on YouTube.
Comments
No comments yet. Start the discussion.