pause button stops working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tshrpl

hi all, i’m making pause menu for my asteroids game and i’ve encountered this issue

i have a button in my main scene called settings_button with the following script attached, also the pause mode is set to process

extends Button
var settings_scene = preload("res://ui/settings.tscn")
func _on_settings_button_pressed():
	var settings_instance = settings_scene.instance()
	get_parent().add_child(settings_instance)

the settings scene that is instanced in the above code looks like this

the pause mode in this scene is also set to process

this scene also has an exit button with the following script

extends Button
func _on_exit_pressed():
	get_tree().paused = false
	get_parent().queue_free()

when i click the settings button, the game pauses and the settings scene is instanced and added to the main scene, and when i click the exit button i successfully exit that scene, but when i click the settings button again, nothing happens

any idea?
thanks in advance

edit:
so i tracked down the issue to a bug in my exit.gd script
the last line should be like this

	# get_parent().queue_free()
	get_node('../..').queue_free()

bacause the settings node was two node up

Why are you instancing the settings scene? Why not add it to the main scene?

Ertain | 2021-12-31 20:44

can you give me an example of how to do this otherwise?

tshrpl | 2022-01-01 02:13

In the editor, add the pause screen as an instance to the main scene.

Ertain | 2022-01-01 06:06

i could do that, toggling the visibility, but the code looks a lot cleaner now than it looked earlier. my problem is that the button works first time but doesn’t work after that

tshrpl | 2022-01-01 07:55

Maybe have something like this in the functions?

# In the main scene.
func _on_settings_button_pressed():
    #  The "settings" scene that's instanced via the editor, instead of at runtime. The "settings" scene here is hidden at first.
    settings_scene.show()
    # This changes the current scene from running to paused (or to its opposite; from paused to running).
    get_tree().paused = not get_tree().is_paused()

# In the "settings" scene.
func _on_exit_pressed():
    get_tree().paused = not get_tree().is_paused()
    hide()

Ertain | 2022-01-01 09:02