This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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
enter image description here

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

Godot version Godot_v3.4.2-stable_win64
in Engine by (63 points)
edited by

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

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

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

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

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()

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.