Issue with a pause menu

Godot Version

4.4

Question

I’m working on a pause menu. I want to be able to pause the game using a pause button and the escape key. The buttons work as intended and I have already mapped the escape key. When the menu is opened using the pause button I can use the escape key to close the menu however I cannot use the escape key to open the menu. Can someone please point me in the right direction? My script code is listed below:

extends Control

Called when the node enters the scene tree for the first time.

func _ready() → void:
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
pass

#function to toggle the pause menu/pause the game
func toggle_pause():
if get_tree().paused:
get_tree().paused = false
hide()
else:
get_tree().paused = true
show()

func _input(event: InputEvent) → void:
if Input.is_action_pressed(“pause”):
toggle_pause()

#Shows the pause menu when the pause button is pressed
func _on_pause_button_pressed() → void:
toggle_pause()

#hides the pause menu when the continue button is pressed
func _on_continue_pressed() → void:
toggle_pause()

#returns to start menu when leave game button is pressed
func _on_leave_game_pressed() → void:
get_tree().paused = false
get_tree().change_scene_to_file(“res://screens/ui/Menu.tscn”)

#exits the game
func _on_exit_pressed() → void:
get_tree().quit()

In the future please put your code in a code block using ```

Your code looks fine. Your issue is likely that you have another input event somewhere that is eating your “pause” action. When the game is paused, whatever is eating it is also paused, so the execution is working when the game is paused.

Press Ctrl + F in the code editor and look for “pause”. You’ll probably find the place pretty fast.

This can be a window focus problem. Game controller input goes to your game regardless of whether the game window has keyboard focus, so the game can appear to have focus as long as you use the controller, but key presses are actually going to some other program entirely (whatever has focus). The resulting behaviour can be counterintuitive.

1 Like