handling_quit_requests.rst 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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);