handling_quit_requests.rst 3.2 KB

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