Exit button in godot

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

Hi! I’m trying to make an exit button in godot, as the title says. I tried to put get_tree().quit() in a script but not worked. Any solutions please? I’ll post the button’s script and the tittlescreen script.

Button.gd:

extends Button

func _ready():
    pass

func _on_Button_pressed():
    get_tree().quit()

TittleScreen.gd:

extends Control

var scene_path_to_load

func _ready():
	$Menu/CenterRow/Buttons/NewGameButton.grab_focus()
	for button in $Menu/CenterRow/Buttons.get_children():
		button.connect("pressed", self, "_on_Button_pressed", [button.scene_to_load])


func _on_Button_pressed(scene_to_load):
	$FadeIn.show()
	$FadeIn.fade_in()
	scene_path_to_load = scene_to_load


func _on_FadeIn_fade_finished():
	$FadeIn.hide()
	get_tree().change_scene(scene_path_to_load)
func _on_OptionsButton_pressed():
	pass # Replace with function body.

Did you connect the signal? Alternatively, you can override _press without connecting it:

func _pressed():
    get_tree().quit()

Dlean Jeans | 2019-07-16 02:40

I’ve connected the signal, but didn’t work. However, I found the answer. I just changed the script to

extends Button

func _physics_process(delta: float) -> void:
    if is_pressed():
        get_tree().quit()

and now it works!

Mort | 2019-07-16 16:48