2023-01-12 20:49:14 +01:00
|
|
|
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
Handling quit requests
|
|
|
|
======================
|
|
|
|
|
|
|
|
Quitting
|
|
|
|
--------
|
|
|
|
|
|
|
|
Most platforms have the option to request the application to quit. On
|
|
|
|
desktops, this is usually done with the "x" icon on the window title bar.
|
|
|
|
On Android, the back button is used to quit when on the main screen (and
|
|
|
|
to go back otherwise).
|
|
|
|
|
|
|
|
Handling the notification
|
|
|
|
-------------------------
|
|
|
|
|
2023-01-12 19:30:47 +01:00
|
|
|
On desktop platforms, the `MainLoop`
|
2023-01-12 19:43:03 +01:00
|
|
|
has a special `MainLoop.NOTIFICATION_WM_QUIT_REQUEST` notification that is
|
2022-03-18 17:46:08 +01:00
|
|
|
sent to all nodes when quitting is requested.
|
|
|
|
|
2023-01-12 19:43:03 +01:00
|
|
|
On Android, `MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST` is sent instead.
|
2022-03-18 17:46:08 +01:00
|
|
|
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).
|
|
|
|
|
2023-01-12 20:55:57 +01:00
|
|
|
Note:
|
|
|
|
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 19:43:03 +01:00
|
|
|
`MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST` isn't supported on iOS, as
|
2022-03-18 17:46:08 +01:00
|
|
|
iOS devices don't have a physical Back button.
|
|
|
|
|
|
|
|
Handling the notification is done as follows (on any node):
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
func _notification(what):
|
|
|
|
if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
|
|
|
|
get_tree().quit() # default behavior
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
When developing mobile apps, quitting is not desired unless the user is
|
|
|
|
on the main screen, so the behavior can be changed.
|
|
|
|
|
|
|
|
It is important to note that by default, Godot apps have the built-in
|
|
|
|
behavior to quit when quit is requested, this can be changed:
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
get_tree().set_auto_accept_quit(false)
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
Sending your own quit notification
|
|
|
|
----------------------------------
|
|
|
|
|
2023-01-12 19:30:47 +01:00
|
|
|
While forcing the application to close can be done by calling `SceneTree.quit`,
|
2022-03-18 17:46:08 +01:00
|
|
|
doing so will not send the quit *notification*. This means the function
|
|
|
|
described above won't be called. Quitting by calling
|
2023-01-12 19:30:47 +01:00
|
|
|
`SceneTree.quit` will not allow custom actions
|
2022-03-18 17:46:08 +01:00
|
|
|
to complete (such as saving, confirming the quit, or debugging), even if you try
|
|
|
|
to delay the line that forces the quit.
|
|
|
|
|
|
|
|
Instead, you should send a quit request:
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
get_tree().notification(MainLoop.NOTIFICATION_WM_QUIT_REQUEST)
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|