How to correctly implement QUIT_REQUEST on button_pressed.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 4K4deusz
:warning: Old Version Published before Godot 3 was released.

Hi,

I am learning scripting gd scripting and trying to get me head around it. I have made a button that I want to exit the program. I hooked up the button and it worked when just printing (“button is pressed”)

func _on_Button_pressed():
	print ("pressed the button")

Now i wanted to make use of the example given in the godot docs. https://godot.readthedocs.io/en/latest/tutorials/engine/handling_quit_requests.html

func _notification(what):
    if (what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST):
        get_tree().quit() # default behavior

And so after fiddling around I wrote this:

 func _on_Button_pressed():
	_on_Button_pressed() == (MainLoop.NOTIFICATION_WM_QUIT_REQUEST)
	get_tree().quit() # default behavior

Which quits the program all right but it give me a windows dialog saying: Godot Engine Editor has stopped working.

Appreciate any answers or pointers.

just delete the second line of code:
_on_Button_pressed() == (MainLoop.NOTIFICATION_WM_QUIT_REQUEST)

this has many mistakes in it:
_on_Button_pressed() is the name of the function and if it would return some value should be on the right side of the ‘=’
the ‘==’ should check if the left and right values are equal or not, but there’s nothing to asses the result of that check
left and right of ‘==’ should be the same type of variable

MrMonk | 2016-10-27 14:12

Thanks for your answer!

Then when would I need to include the:

 (MainLoop.NOTIFICATION_WM_QUIT_REQUEST)

?

4K4deusz | 2016-10-27 14:58

Unless you want to do something before your game closes (close files, save the state of the game, or just display a confirmation dialog), you don’t need to do anything that’ts mentioned in the tutorial, just do:

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

If you want to do something before your game closes you need to write a script like this:

func _ready():
    get_tree().set_auto_accept_quit(false)

func _notification(what):
    if (what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST):
        # do whatever you want here
        get_tree().quit()

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

atze | 2016-10-27 15:55