pausing_games.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.png
  30. You can also alter the property with code:
  31. .. tabs::
  32. .. code-tab:: gdscript GDScript
  33. func _ready():
  34. pause_mode = Node.PAUSE_MODE_PROCESS
  35. .. code-tab:: csharp
  36. public override void _Ready()
  37. {
  38. PauseMode = Node.PauseModeEnum.Process;
  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**: Stop the node no matter what (and children in Inherit
  44. mode). When paused this node will not process.
  45. - **WhenPaused**:
  46. - **Always**: Process the node no matter what (and children in Inherit
  47. mode). Paused or not this node will process.
  48. - **Disabled**: The node will not process at all.
  49. By default, all nodes have this property in the "Inherit" state. If the
  50. parent is set to "Inherit", then the grandparent will be checked and so
  51. on. If a state can't be found in any of the grandparents, the pause state
  52. in SceneTree is used. This means that, by default, when the game is paused
  53. every node will be paused. Several things happen when a node stops processing.
  54. ``_process``, ``_physics_process``, ``_input``, and ``_input_event`` functions
  55. will not be called". However signals still work and cause their connected function to
  56. run, even if that function's script is attached to a node that has its pause
  57. mode set to "Stop".
  58. animation nodes will pause their current animation, audio nodes
  59. will pause their current audio stream, and particles will pause. These resume
  60. automatically when the game is no longer paused.
  61. It is important to note that even if a node is processing while the game is
  62. paused physics will **NOT** work for it by default. As stated earlier this is
  63. because the physics servers are turned off. The physics servers can be made
  64. active while the game is paused by using their ``set_active`` methods.
  65. Pause Menu Example
  66. ------------------
  67. Here is an example of a pause menu. create a popup or panel with controls
  68. inside, and set its pause mode to "Process" then hide it. By setting the
  69. root of the pause popup to "Process", all children and grandchildren will
  70. inherit that state. This way, this branch of the scene tree will continue
  71. working when paused.
  72. Finally, make it so when a pause button is pressed (any button will do),
  73. enable the pause and show the pause screen.
  74. .. tabs::
  75. .. code-tab:: gdscript GDScript
  76. func _on_pause_button_pressed():
  77. get_tree().paused = true
  78. $pause_popup.show()
  79. .. code-tab:: csharp
  80. public void _on_pause_button_pressed()
  81. {
  82. GetTree().Paused = true;
  83. GetNode<Control>("pause_popup").Show();
  84. }
  85. To remove the pause, do the opposite when the pause screen is
  86. closed:
  87. .. tabs::
  88. .. code-tab:: gdscript GDScript
  89. func _on_pause_popup_close_pressed():
  90. $pause_popup.hide()
  91. get_tree().paused = false
  92. .. code-tab:: csharp
  93. public void _on_pause_popup_close_pressed()
  94. {
  95. GetNode<Control>("pause_popup").Hide();
  96. GetTree().Paused = false;
  97. }