Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Gestion des demandes de quitter l'application

Fermeture

La plupart des plateformes ont la possibilité de demander de quitter l'application. Sur les ordinateurs de bureau, cela se fait généralement à l'aide de l'icône "x" dans la barre de titre de la fenêtre. Sur Android, le bouton retour est utilisé pour quitter l'application lorsqu'elle est sur l'écran principal (et pour revenir en arrière dans le cas contraire).

Traitement de la notification

On desktop and web platforms, Node receives a special NOTIFICATION_WM_CLOSE_REQUEST notification when quitting is requested from the window manager.

On Android, NOTIFICATION_WM_GO_BACK_REQUEST is sent instead. Pressing the Back button will exit the application if Application > Config > Quit On Go Back is checked in the Project Settings (which is the default).

Note

NOTIFICATION_WM_GO_BACK_REQUEST isn't supported on iOS, as iOS devices don't have a physical Back button.

Le traitement de la notification se fait comme suit (sur n'importe quel nœud) :

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

Lors du développement d'applications mobiles, il n'est pas souhaitable de quitter à moins que l'utilisateur se trouve sur l'écran principal, ce qui permet de modifier le comportement.

It is important to note that by default, Godot apps have the built-in behavior to quit when quit is requested from the window manager. This can be changed, so that the user can take care of the complete quitting procedure:

get_tree().set_auto_accept_quit(false)

Envoi de votre propre notification d'arrêt

While forcing the application to close can be done by calling SceneTree.quit, doing so will not send the NOTIFICATION_WM_CLOSE_REQUEST to the nodes in the scene tree. Quitting by calling SceneTree.quit will not allow custom actions to complete (such as saving, confirming the quit, or debugging), even if you try to delay the line that forces the quit.

Instead, if you want to notify the nodes in the scene tree about the upcoming program termination, you should send the notification yourself:

get_tree().root.propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)

Sending this notification will inform all nodes about the program termination, but will not terminate the program itself unlike in 3.X. In order to achieve the previous behavior, SceneTree.quit should be called after the notification.