pausing_games.rst 4.3 KB

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