Trying to get a basic pause menu to work

Godot 4.3

Hi,

I’m still very new to Godot and I know to how to set up a basic input event to turn the mouse mode from captured to uncaptured. But I was trying to make an actual pause menu to go along with it using some of my own code and some stuff from tutorials.

But for some reason my game is paused 100% of the time, the buttons don’t work, and the freeing of the mouse isn’t tied to whether or not the game is paused.

(What I mean by that is that even though the pause menu is up no matter what you do, in order to get the mouse visible you still have to press escape.)

My code and scene tree are attached, any help would be great, because I’m not a programmer.


You must set your pause menu to also process when paused. In the pausemenu’s Node properties set process mode to ‘Always’ or ‘When Paused’

I’ve seen this @onready var my_script = $"." thing often, where did you get that? It’s equivalent and slower than using self or the functions directly, for example these three lines are the same in your script, and you should prefer the last two

pause_menu.hide()

self.hide()
hide()
1 Like

that… makes sense lol, I’ll fix that.

as for the onready variables, like I said I’m not a programmer I’m learning by watching tutorials and just trying stuff and just about every video I’ve seen that needs to control a node with code uses an onready variable, and one of them even made it sound like you have to do that to work with a node directly, and as I haven’t gotten deep into the documentation, I just assumed that was the truth.

@onready isn’t a problem I was talking about the path $"." specifically is equal to self, but slower.

Other paths are necessary like $ColorRect will access the ColorRect child, then you could change it’s color from the same script.

$ColorRect.color = Color.RED

Are saying it’s slower computationally or coding-wise? Because I just dragged the path in from the scene tree, so it was pretty fast.

Also, I tried both “always process” and “process when paused”, and “when paused” is the closest to working as intended. It got the quit button to work. And the game starts off with the player able to move the camera around, only behind the pause menu because it’s still there 100% of the time. Freeing the mouse works as expected but it doesn’t ever recapture, and the resume button still doesn’t work. (Probably because it’s trying to do the same thing the escape key is set to do, which also doesn’t work.)

And before you say, “Well if you’re learning from tutorials, why don’t you follow one?”. The videos that show you how to free the mouse don’t actually have it bring up a menu. And the pause menu tutorials I saw, one was for a game where the mouse was never captured so that wouldn’t work, and one just didn’t work when I followed it. So I just figured I’d use the small gdscript course I took and just figure it out, which led me here.

Aha! It is slower computationally (by a little bit) and coding wise I suppose, it’s much faster to simply not. I see why it keeps popping up, you do not have to drag the node into the editor if it’s the same node the script is attached to.


You will need to re-capture the mouse in code, when the pause menu is hidden also set the mouse mode to captured.

hide()
get_tree().paused = false
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

The resume button needs to call the “pause_game” function, not set the “game_paused” variable.

func _on_resumebtn_pressed() -> void:
    pause_game()

Though I would recommend changing this pause_game function to accept a true/false argument.

func pause_game(new_paused: bool = true) -> void:
    visible = new_paused
    get_tree().paused = new_paused
    Input.mouse_mode = Input.MOUSE_MODE_VISIBLE if new_paused else Input.MOUSE_MODE_CAPTURED

func _on_resumebtn_pressed() -> void:
    pause_game(false)

thank you for all your help, I don’t know how I never noticed I didn’t ever tell it to recapture the mouse or the fact that I was just calling a true/false vriable

1 Like

I got it working properly, partly with your help and partly some other jank fixes, I’m sure it’s barely held together with duct tape but everything works.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.