Pausing games and process mode

前言

In most games it is desirable to, at some point, interrupt the game to do something else, such as taking a break or changing options. Implementing a fine-grained control for what can be paused (and what cannot) is a lot of work, so a simple framework for pausing is provided in Godot.

如何暂停工作

To pause the game the pause state must be set. This is done by assigning true to the SceneTree.paused property:

GDScriptC#

  1. get_tree().paused = true
  1. GetTree().Paused = true;

Doing this will cause two things. First, 2D and 3D physics will be stopped for all nodes. Second, the behavior of certain nodes will stop or start depending on their process mode.

备注

The physics servers can be made active while the game is paused by using their set_active methods.

Process Modes

Each node in Godot has a “Pause Mode” that defines when it processes. It can be found and changed under a node’s Node properties in the inspector.

../../_images/pausemode.png

You can also alter the property with code:

GDScriptC#

  1. func _ready():
  2. pause_mode = Node.PAUSE_MODE_PROCESS
  1. public override void _Ready()
  2. {
  3. PauseMode = Node.PauseModeEnum.Process;
  4. }

This is what each mode tells a node to do:

  • Inherit(继承) :处理与否取决于父、祖父等等节点中第一个非 Inherit 状态的节点。

  • Stop(停止) :无条件停止节点(以及 Inherit 模式的子节点)。暂停时该节点不会进行处理。

  • Process(处理) :无条件处理节点(以及 Inherit 模式的子节点)。无论暂停与否,该节点都会进行处理。

By default, all nodes have this property in the “Inherit” state. If the parent is set to “Inherit”, then the grandparent will be checked and so on. If a state can’t be found in any of the grandparents, the pause state in SceneTree is used. This means that, by default, when the game is paused every node will be paused. Several things happen when a node stops processing.

The _process, _physics_process, _input, and _input_event functions will not be called. However signals still work and cause their connected function to run, even if that function’s script is attached to a node that has its pause mode set to “Stop”.

Animation nodes will pause their current animation, audio nodes will pause their current audio stream, and particles will pause. These resume automatically when the game is no longer paused.

It is important to note that even if a node is processing while the game is paused physics will NOT work for it by default. As stated earlier this is because the physics servers are turned off. The physics servers can be made active while the game is paused by using their set_active methods.

Pause Menu Example

Here is an example of a pause menu. Create a popup or panel with controls inside, and set its pause mode to “Process” then hide it. By setting the root of the pause popup to “Process”, all children and grandchildren will inherit that state. This way, this branch of the scene tree will continue working when paused.

最后, 当按下暂停按钮(任何按钮都可以)时, 启用暂停并显示暂停屏幕.

GDScriptC#

  1. func _on_pause_button_pressed():
  2. get_tree().paused = true
  3. $pause_popup.show()
  1. public void _on_pause_button_pressed()
  2. {
  3. GetTree().Paused = true;
  4. GetNode<Control>("pause_popup").Show();
  5. }

To unpause, do the opposite when the pause screen is closed:

GDScriptC#

  1. func _on_pause_popup_close_pressed():
  2. $pause_popup.hide()
  3. get_tree().paused = false
  1. public void _on_pause_popup_close_pressed()
  2. {
  3. GetNode<Control>("pause_popup").Hide();
  4. GetTree().Paused = false;
  5. }