How to handle close('x') button?

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

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?

:bust_in_silhouette: Reply From: jospic

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


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

Gleb | 2017-11-16 11:10

Strange. Have you insert this function in main script?

jospic | 2017-11-16 13:32

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

Gleb | 2017-11-17 10:21

Check this my little test project.

It works.


jospic | 2017-11-17 12:26

Thank you very much!

Gleb | 2017-11-28 10:42

:bust_in_silhouette: Reply From: bruteforce

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

func _ready():

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

func _notification(what):
		# code of saving