handling_quit_requests.rst 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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>`
  13. receives a special ``NOTIFICATION_WM_CLOSE_REQUEST`` notification when quitting is requested.
  14. On Android, ``MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST`` is sent instead.
  15. Pressing the Back button will exit the application if
  16. **Application > Config > Quit On Go Back** is checked in the Project Settings
  17. (which is the default).
  18. .. note::
  19. ``MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST`` isn't supported on iOS, as
  20. iOS devices don't have a physical Back button.
  21. Handling the notification is done as follows (on any node):
  22. .. tabs::
  23. .. code-tab:: gdscript GDScript
  24. func _notification(what):
  25. if what == NOTIFICATION_WM_CLOSE_REQUEST:
  26. get_tree().quit() # default behavior
  27. .. code-tab:: csharp
  28. public override void _Notification(int what)
  29. {
  30. if (what == NotificationWmCloseRequest)
  31. GetTree().Quit(); // default behavior
  32. }
  33. When developing mobile apps, quitting is not desired unless the user is
  34. on the main screen, so the behavior can be changed.
  35. It is important to note that by default, Godot apps have the built-in
  36. behavior to quit when quit is requested, this can be changed:
  37. .. tabs::
  38. .. code-tab:: gdscript GDScript
  39. get_tree().set_auto_accept_quit(false)
  40. .. code-tab:: csharp
  41. GetTree().SetAutoAcceptQuit(false);
  42. Sending your own quit notification
  43. ----------------------------------
  44. While forcing the application to close can be done by calling :ref:`SceneTree.quit <class_SceneTree_method_quit>`,
  45. doing so will not send the quit *notification*. This means the function
  46. described above won't be called. Quitting by calling
  47. :ref:`SceneTree.quit <class_SceneTree_method_quit>` will not allow custom actions
  48. to complete (such as saving, confirming the quit, or debugging), even if you try
  49. to delay the line that forces the quit.
  50. Instead, you should send a quit request:
  51. .. tabs::
  52. .. code-tab:: gdscript GDScript
  53. get_tree().notification(NOTIFICATION_WM_CLOSE_REQUEST)
  54. .. code-tab:: csharp
  55. GetTree().Notification(NotificationWmCloseRequest)