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)