Pausing games and process mode

Introduction

In most games it is desirable to, at some point, interrupt thegame 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 inGodot.

How pausing works

To pause the game the pause state must be set. This is done by assigningtrue to theSceneTree.paused property:

get_tree().paused=true

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

Note

The physics servers can be made active while the game ispaused by using theirset_active methods.

Process Modes

Each node in Godot has a "Process Mode" that defines when it processes. It canbe found and changed under a node'sNode properties in the inspector.

../../_images/pausemode.webp

You can also alter the property with code:

func_ready():process_mode=Node.PROCESS_MODE_PAUSABLE

This is what each mode tells a node to do:

  • Inherit: Process depending on the state of the parent,grandparent, etc. The first parent that has a non-Inherit state.

  • Pausable: Process the node (and its children in Inheritmode) only when the game is not paused.

  • WhenPaused: Process the node (and its children in Inheritmode)only when the game is paused.

  • Always: Process the node (and its children in Inheritmode) no matter what. Paused or not, this node will process.

  • Disabled: The node (and its children in Inheritmode) will not process at all.

By default, all nodes have this property in the "Inherit" state. If theparent is set to "Inherit", then the grandparent will be checked and soon. If a state can't be found in any of the grandparents, the pause statein SceneTree is used. This means that, by default, when the game is pausedevery node will be paused. Several things happen when a node stops processing.

The_process,_physics_process,_input, and_input_event functionswill not be called. However signals still work and cause their connected function torun, even if that function's script is attached to a node that is not currently being processed.

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

It is important to note that even if a node is processing while the game ispaused physics willNOT work for it by default. As stated earlier this isbecause the physics servers are turned off. The physics servers can be madeactive while the game is paused by using theirset_active methods.

Pause menu example

Start by creating a button that will be used to pause the game.

Create a menu containing a close button, set theProcess Mode of the menu's root nodetoWhen Paused, then hide the menu. Since the process mode is set toWhen Pausedon the root node, all its children and grandchildren will inherit that process mode.This way, all the nodes in the menu will start processing when the game is paused.

Attach a script to the menu's root node, connect the pause button created earlier to a new method inthe script, and inside that method pause the game and show the pause menu.

func_on_pause_button_pressed():get_tree().paused=trueshow()

Finally, connect the menu's close button to a new method in the script. Inside that method,unpause the game and hide the pause menu.

func_on_close_button_pressed():hide()get_tree().paused=false

You should now have a working pause menu.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.