pausing_games.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. .. _doc_pausing_games:
  2. Pausing games and process mode
  3. ==============================
  4. Introduction
  5. ------------
  6. In most games it is desirable to, at some point, interrupt the
  7. game to do something else, such as taking a break or changing options.
  8. Implementing a fine-grained control for what can be paused (and what cannot)
  9. is a lot of work, so a simple framework for pausing is provided in
  10. Godot.
  11. How pausing works
  12. -----------------
  13. To pause the game the pause state must be set. This is done by assigning
  14. ``true`` to the :ref:`SceneTree.paused <class_SceneTree_property_paused>` property:
  15. .. tabs::
  16. .. code-tab:: gdscript GDScript
  17. get_tree().paused = true
  18. .. code-tab:: csharp
  19. GetTree().Paused = true;
  20. Doing this will cause two things. First, 2D and 3D physics will be stopped
  21. for all nodes. Second, the behavior of certain nodes will stop or start
  22. depending on their process mode.
  23. .. note:: The physics servers can be made active while the game is
  24. paused by using their ``set_active`` methods.
  25. Process Modes
  26. -------------
  27. Each node in Godot has a "Process Mode" that defines when it processes. It can
  28. be found and changed under a node's :ref:`Node <class_Node>` properties in the inspector.
  29. .. image:: img/pausemode.webp
  30. You can also alter the property with code:
  31. .. tabs::
  32. .. code-tab:: gdscript GDScript
  33. func _ready():
  34. process_mode = Node.PROCESS_MODE_PAUSABLE
  35. .. code-tab:: csharp
  36. public override void _Ready()
  37. {
  38. ProcessMode = Node.ProcessModeEnum.Pausable;
  39. }
  40. This is what each mode tells a node to do:
  41. - **Inherit**: Process depending on the state of the parent,
  42. grandparent, etc. The first parent that has a non-Inherit state.
  43. - **Pausable**: Process the node (and its children in Inherit
  44. mode) only when the game is not paused.
  45. - **WhenPaused**: Process the node (and its children in Inherit
  46. mode) *only* when the game is paused.
  47. - **Always**: Process the node (and its children in Inherit
  48. mode) no matter what. Paused or not, this node will process.
  49. - **Disabled**: The node (and its children in Inherit
  50. mode) will not process at all.
  51. By default, all nodes have this property in the "Inherit" state. If the
  52. parent is set to "Inherit", then the grandparent will be checked and so
  53. on. If a state can't be found in any of the grandparents, the pause state
  54. in SceneTree is used. This means that, by default, when the game is paused
  55. every node will be paused. Several things happen when a node stops processing.
  56. The ``_process``, ``_physics_process``, ``_input``, and ``_input_event`` functions
  57. will not be called. However signals still work and cause their connected function to
  58. run, even if that function's script is attached to a node that is not currently being processed.
  59. Animation nodes will pause their current animation, audio nodes
  60. will pause their current audio stream, and particles will pause. These resume
  61. automatically when the game is no longer paused.
  62. It is important to note that even if a node is processing while the game is
  63. paused physics will **NOT** work for it by default. As stated earlier this is
  64. because the physics servers are turned off. The physics servers can be made
  65. active while the game is paused by using their ``set_active`` methods.
  66. Pause Menu Example
  67. ------------------
  68. Here is an example of a pause menu. Create a popup or panel with controls
  69. inside, and set its pause mode to "When Paused" then hide it. By setting the
  70. root of the pause popup to "When Paused", all children and grandchildren will
  71. inherit that state. This way, this branch of the scene tree will continue
  72. working when paused.
  73. Finally, make it so when a pause button is pressed (any button will do),
  74. enable the pause and show the pause screen.
  75. .. tabs::
  76. .. code-tab:: gdscript GDScript
  77. func _on_pause_button_pressed():
  78. get_tree().paused = true
  79. $pause_popup.show()
  80. .. code-tab:: csharp
  81. public void _on_pause_button_pressed()
  82. {
  83. GetTree().Paused = true;
  84. GetNode<Control>("pause_popup").Show();
  85. }
  86. To unpause, do the opposite when the pause screen is
  87. closed:
  88. .. tabs::
  89. .. code-tab:: gdscript GDScript
  90. func _on_pause_popup_close_pressed():
  91. $pause_popup.hide()
  92. get_tree().paused = false
  93. .. code-tab:: csharp
  94. public void _on_pause_popup_close_pressed()
  95. {
  96. GetNode<Control>("pause_popup").Hide();
  97. GetTree().Paused = false;
  98. }