handling_quit_requests.rst 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. .. _doc_handling_quit_requests:
  2. Handling quit requests
  3. ======================
  4. Quitting
  5. --------
  6. Most platforms have the option to request the application to quit. On
  7. desktops, this is usually done with the "x" icon on the window title bar.
  8. On Android, the back button is used to quit when on the main screen (and
  9. to go back otherwise).
  10. Handling the notification
  11. -------------------------
  12. On desktop and web platforms, :ref:`Node <class_Node>` receives a special
  13. ``NOTIFICATION_WM_CLOSE_REQUEST`` notification when quitting is requested from
  14. the window manager.
  15. On Android, ``NOTIFICATION_WM_GO_BACK_REQUEST`` is sent instead.
  16. Pressing the Back button will exit the application if
  17. **Application > Config > Quit On Go Back** is checked in the Project Settings
  18. (which is the default).
  19. .. note::
  20. ``NOTIFICATION_WM_GO_BACK_REQUEST`` isn't supported on iOS, as
  21. iOS devices don't have a physical Back button.
  22. Handling the notification is done as follows (on any node):
  23. .. tabs::
  24. .. code-tab:: gdscript GDScript
  25. func _notification(what):
  26. if what == NOTIFICATION_WM_CLOSE_REQUEST:
  27. get_tree().quit() # default behavior
  28. .. code-tab:: csharp
  29. public override void _Notification(int what)
  30. {
  31. if (what == NotificationWMCloseRequest)
  32. GetTree().Quit(); // default behavior
  33. }
  34. When developing mobile apps, quitting is not desired unless the user is
  35. on the main screen, so the behavior can be changed.
  36. It is important to note that by default, Godot apps have the built-in
  37. behavior to quit when quit is requested from the window manager. This
  38. can be changed, so that the user can take care of the complete quitting
  39. procedure:
  40. .. tabs::
  41. .. code-tab:: gdscript GDScript
  42. get_tree().set_auto_accept_quit(false)
  43. .. code-tab:: csharp
  44. GetTree().AutoAcceptQuit = false;
  45. Sending your own quit notification
  46. ----------------------------------
  47. While forcing the application to close can be done by calling
  48. :ref:`SceneTree.quit <class_SceneTree_method_quit>`, doing so will not send
  49. the ``NOTIFICATION_WM_CLOSE_REQUEST`` to the nodes in the scene tree.
  50. Quitting by calling :ref:`SceneTree.quit <class_SceneTree_method_quit>` will
  51. not allow custom actions to complete (such as saving, confirming the quit,
  52. or debugging), even if you try to delay the line that forces the quit.
  53. Instead, if you want to notify the nodes in the scene tree about the upcoming
  54. program termination, you should send the notification yourself:
  55. .. tabs::
  56. .. code-tab:: gdscript GDScript
  57. get_tree().root.propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
  58. .. code-tab:: csharp
  59. GetTree().Root.PropagateNotification((int)NotificationWMCloseRequest);
  60. Sending this notification will inform all nodes about the program termination,
  61. but will not terminate the program itself *unlike in 3.X*. In order to achieve
  62. the previous behavior, :ref:`SceneTree.quit <class_SceneTree_method_quit>` should
  63. be called after the notification.