Get_tree().pause

Godot Version 4.2.1

Question

Context
I’m trying to create a simple pong game, after any player score 10 points, it will stop and then will display the message "player " win, or whatever. I’m using the “get.tree().pause” funciton to stop the game, showing some text and then after that the player will input anything and the game restart. I’m using a node as a Game manager.

Problem
The game pause, but it doesnt unpause after any action is pressed. Using a break point, the code doesn’t compile or just ignore the “_process” function. I wich is the function that handle the input.

Any advice is helpful, i’ve been stuck with this method, i haven’t tried any other method to pause again yet. But if anybody know if i’m doing it wrong or maybe another kind of method to accomplish what i’m trying to do…

extends Node

@onready var score_player = $ScorePlayer
@onready var score_rival = $ScoreRival
@onready var audio = $AudioStreamPlayer2D


var pl_score = 0
var ri_score = 0

func _ready():
	set_process(false)  # Disable processing initially

func _on_ball_ball_out_of_bounds(side):
	if side == "right":
		pl_score += 1
		score_player.text = str(pl_score)
	if side == "left":
		ri_score += 1
		score_rival.text = str(ri_score)
	audio.play()
	_check_winner()

func _check_winner():
	if pl_score >= 10:
		_pause_game("Player")
	elif ri_score >= 10:
		_pause_game("Rival")

func _pause_game(winner: String):
	get_tree().paused = true
	print("Winner is: " + winner)
	await_input_to_reset()

func await_input_to_reset() -> void:
	await get_tree().create_timer(1.0).timeout  # Wait a moment before allowing input
	set_process(true)  # Enable processing to detect input events

func _process(_delta: float):
	if get_tree().paused and _is_any_key_pressed():
		reset_game()

func _is_any_key_pressed() -> bool:
	return Input.is_anything_pressed()

func reset_game():
	get_tree().paused = false
	set_process(false)  # Disable processing after reset
	pl_score = 0
	ri_score = 0
	score_player.text = str(pl_score)
	score_rival.text = str(ri_score)

Hey

indeed the paused = true will stop the various event and process:

If true, the SceneTree is paused. Doing so will have the following behavior:

  • 2D and 3D physics will be stopped. This includes signals and collision detection.
  • Node._process, Node._physics_process and Node._input will not be called anymore in nodes.

To achieve what you want I would suggest using an enum that keep the state of the game

enum GameState{
	PREPARING,
	PLAYING,,
	OVER,
}

This way you could set the state to OVER in _check_winner and check for this state to reset the game in _process

Hope that helped, good luck!

1 Like

Make sure to set some nodes’ “Process Mode” to Always. Otherwise they will be paused and not run.

1 Like

When you call get_tree().paused = true , all nodes that still have the Process > Mode property set to the default value Inherit stop receiving input events. There are several ways to resolve that. Check out this tutorial that demonstrates one of them.

2 Likes

Thank you very much, you have earned a subscriber. Well my problem was very simple, in the inspector, i just need to change the process mode to When paused. But i didn’t know it. Kudos!!

1 Like