The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

Hi everyone!

Please, help me. How can I handle close(quit) button? ('X' button in Windows or MacOS)
For example, if I want to save the game before quit?

in Engine by (18 points)

2 Answers

+3 votes
Best answer

You may handling quit request by:

func _notification(what):
    if (what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST):
        print ("You are quit!")
        get_tree().quit() # default behavior

-j

by (1,488 points)
selected by

Not working.
When I close the game nothing happens in output console.

Strange. Have you insert this function in main script?

Sorry, what do you mean main script? I can't understand how can script can be not main in Godot. I insert this function in script of the scene where I want to save the game when it close

Check this my little test project.

https://drive.google.com/open?id=1toYNO4xvQ1Z9CbA3YSZ8PrNgatsOdYMl

It works.

-j

Thank you very much!

+2 votes

If I understood the documentation correctly, then:
1.) You have to override the default behavior (ie. in the _ready function):

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

2.)You can handle the quit request in the _notification function:

func _notification(what):
    if (what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST):
        #
        # code of saving
        #
        get_tree().quit()
by (685 points)
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.