handling_quit_requests.rst 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 platforms, the :ref:`MainLoop <class_MainLoop>`
  13. has a special ``MainLoop.NOTIFICATION_WM_QUIT_REQUEST`` notification that is
  14. sent to all nodes when quitting is requested.
  15. On Android, ``MainLoop.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. ``MainLoop.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 == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
  27. get_tree().quit() # default behavior
  28. .. code-tab:: csharp
  29. public override void _Notification(int what)
  30. {
  31. if (what == MainLoop.NotificationWmQuitRequest)
  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, this can be changed:
  38. .. tabs::
  39. .. code-tab:: gdscript GDScript
  40. get_tree().set_auto_accept_quit(false)
  41. .. code-tab:: csharp
  42. GetTree().SetAutoAcceptQuit(false);
  43. Sending your own quit notification
  44. ----------------------------------
  45. While forcing the application to close can be done by calling :ref:`SceneTree.quit <class_SceneTree_method_quit>`,
  46. doing so will not send the quit *notification*. This means the function
  47. described above won't be called. Quitting by calling
  48. :ref:`SceneTree.quit <class_SceneTree_method_quit>` will not allow custom actions
  49. to complete (such as saving, confirming the quit, or debugging), even if you try
  50. to delay the line that forces the quit.
  51. Instead, you should send a quit request:
  52. .. tabs::
  53. .. code-tab:: gdscript GDScript
  54. get_tree().notification(MainLoop.NOTIFICATION_WM_QUIT_REQUEST)
  55. .. code-tab:: csharp
  56. GetTree().Notification(MainLoop.NotificationWmQuitRequest)