handling_quit_requests.rst 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 mobile devices, the app can quit at any time while it is suspended
  9. to the background.
  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. Handling the notification is done as follows (on any node):
  16. .. tabs::
  17. .. code-tab:: gdscript GDScript
  18. func _notification(what):
  19. if what == NOTIFICATION_WM_CLOSE_REQUEST:
  20. get_tree().quit() # default behavior
  21. .. code-tab:: csharp
  22. public override void _Notification(int what)
  23. {
  24. if (what == NotificationWMCloseRequest)
  25. {
  26. GetTree().Quit(); // default behavior
  27. }
  28. }
  29. It is important to note that by default, Godot apps have the built-in
  30. behavior to quit when quit is requested from the window manager. This
  31. can be changed, so that the user can take care of the complete quitting
  32. procedure:
  33. .. tabs::
  34. .. code-tab:: gdscript GDScript
  35. get_tree().set_auto_accept_quit(false)
  36. .. code-tab:: csharp
  37. GetTree().AutoAcceptQuit = false;
  38. On mobile devices
  39. -----------------
  40. There is no direct equivalent to ``NOTIFICATION_WM_CLOSE_REQUEST`` on mobile
  41. platforms. Due to the nature of mobile operating systems, the only place
  42. that you can run code prior to quitting is when the app is being suspended to
  43. the background. On both Android and iOS, the app can be killed while suspended
  44. at any time by either the user or the OS. A way to plan ahead for this
  45. possibility is to utilize ``NOTIFICATION_APPLICATION_PAUSED`` in order to
  46. perform any needed actions as the app is being suspended.
  47. .. note:: On iOS, you only have approximately 5 seconds to finish a task started by this signal. If you go over this allotment, iOS will kill the app instead of pausing it.
  48. On Android, pressing the Back button will exit the application if
  49. **Application > Config > Quit On Go Back** is checked in the Project Settings
  50. (which is the default). This will fire ``NOTIFICATION_WM_GO_BACK_REQUEST``.
  51. Sending your own quit notification
  52. ----------------------------------
  53. While forcing the application to close can be done by calling
  54. :ref:`SceneTree.quit <class_SceneTree_method_quit>`, doing so will not send
  55. the ``NOTIFICATION_WM_CLOSE_REQUEST`` to the nodes in the scene tree.
  56. Quitting by calling :ref:`SceneTree.quit <class_SceneTree_method_quit>` will
  57. not allow custom actions to complete (such as saving, confirming the quit,
  58. or debugging), even if you try to delay the line that forces the quit.
  59. Instead, if you want to notify the nodes in the scene tree about the upcoming
  60. program termination, you should send the notification yourself:
  61. .. tabs::
  62. .. code-tab:: gdscript GDScript
  63. get_tree().root.propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
  64. .. code-tab:: csharp
  65. GetTree().Root.PropagateNotification((int)NotificationWMCloseRequest);
  66. Sending this notification will inform all nodes about the program termination,
  67. but will not terminate the program itself *unlike in 3.X*. In order to achieve
  68. the previous behavior, :ref:`SceneTree.quit <class_SceneTree_method_quit>` should
  69. be called after the notification.