pausing_games.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. Start by creating a button that will be used to pause the game.
  69. Create a menu containing a close button, set the **Process Mode** of the menu's root node
  70. to **When Paused**, then hide the menu. Since the process mode is set to **When Paused**
  71. on the root node, all its children and grandchildren will inherit that process mode.
  72. This way, all the nodes in the menu will start processing when the game is paused.
  73. Attach a script to the menu's root node, connect the pause button created earlier to a new method in
  74. the script, and inside that method pause the game and show the pause menu.
  75. .. tabs::
  76. .. code-tab:: gdscript GDScript
  77. func _on_pause_button_pressed():
  78. get_tree().paused = true
  79. show()
  80. .. code-tab:: csharp
  81. private void OnPauseButtonPressed()
  82. {
  83. GetTree().Paused = true;
  84. Show();
  85. }
  86. Finally, connect the menu's close button to a new method in the script. Inside that method,
  87. unpause the game and hide the pause menu.
  88. .. tabs::
  89. .. code-tab:: gdscript GDScript
  90. func _on_close_button_pressed():
  91. hide()
  92. get_tree().paused = false
  93. .. code-tab:: csharp
  94. private void OnCloseButtonPressed()
  95. {
  96. Hide();
  97. GetTree().Paused = false;
  98. }
  99. You should now have a working pause menu.