+1 vote

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.

in Engine by (29 points)

just delete the second line of code:
onButtonpressed() == (MainLoop.NOTIFICATIONWMQUITREQUEST)

this has many mistakes in it:
onButton_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

Thanks for your answer!

Then when would I need to include the:

 (MainLoop.NOTIFICATION_WM_QUIT_REQUEST)

?

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

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 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 webmaster@godotengine.org with your username.