Hello! I'm currently encountering an issue with a game project I'm making. When I start a new game and pause the game, the game seems to unpause and start running after a few seconds. I've posted a video showing what this is like:
Below is the code for my main game scene:
extends Node
var wave : int
var difficulty : float
const DIFF_MULTIPLIER : float = 1.2
var lives : int
var max_enemies : int
var isHurt : bool = false
func _ready():
new_game()
$GameOver/Button.pressed.connect(new_game)
func new_game():
lives = 3
wave = 1
difficulty = 10.0
$EnemySpawner/Timer.wait_time = 1.0
reset()
func reset():
max_enemies = int(difficulty)
$Player.reset()
get_tree().call_group("enemies", "queue_free")
get_tree().call_group("bullets", "queue_free")
get_tree().call_group("items", "queue_free")
$HUD/LivesLabel.text = "X " + str(lives)
$HUD/WaveLabel.text = "WAVE: " + str(wave)
$HUD/EnemiesLabel.text = "X " + str(max_enemies)
$GameOver.hide()
$NextWave.hide()
$RestartTimer.start()
func process(delta):
if is_wave_completed():
wave += 1
difficulty *= DIFF_MULTIPLIER
if $EnemySpawner/Timer.wait_time > 0.25:
$EnemySpawner/Timer.wait_time -= 0.05
get_tree().paused = true
$NextWave.visible = true
$WaveOverTimer.start()
func _on_enemy_spawner_hit_p():
lives -= 1
isHurt = true
$LifeLost.play()
$Player.on_hit()
$HUD/LivesLabel.text = "X " + str(lives)
isHurt = false
if lives <= 0:
get_tree().paused = true
$GameOver.show()
$GameOver/WavesSurvivedLabel.text = "WAVES SURVIVED: " + str(wave - 1)
func _on_wave_over_timer_timeout():
reset()
func _on_restart_timer_timeout():
get_tree().paused = false
func is_wave_completed():
var all_dead = true
var enemies = get_tree().get_nodes_in_group("enemies")
if enemies.size() == max_enemies:
for e in enemies:
if e.alive:
all_dead = false
return all_dead
else:
return false
If it helps, here's the code for my Player scene since that's where the pause menu is attached:
extends CharacterBody2D
signal shoot
@onready var hurtblink = $HurtBlink
@onready var hurttimer = $HurtTimer
const START_SPEED : int = 200
const BOOST_SPEED : int = 400
const NORMAL_SHOT : float = 0.5
const FAST_SHOT : float = 0.1
var speed : int
var screen_size : Vector2
var can_shoot : bool
func _ready():
screen_size = get_viewport_rect().size
reset()
hurtblink.play("RESET")
func reset():
can_shoot = true
position = screen_size/2
speed = START_SPEED
$ShotTimer.wait_time = NORMAL_SHOT
func get_input():
var input_dir = Input.get_vector("left", "right", "up", "down")
velocity = input_dir.normalized() * speed
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and can_shoot:
var dir = get_global_mouse_position() - position
shoot.emit(position, dir)
$Shoot.play()
can_shoot = false
$ShotTimer.start()
if Input.is_action_pressed("pause"):
get_tree().paused = true
$PauseMenu.visible = true
func physics_process(delta):
get_input()
move_and_slide()
#limit movement to window size
position = position.clamp(Vector2.ZERO, screen_size)
var mouse = get_local_mouse_position()
var angle = snappedf(mouse.angle(), PI / 4) / (PI / 4)
angle = wrapi(int(angle), 0, 8)
$AnimatedSprite2D.animation = "walk" + str(angle)
if velocity.length() != 0:
$AnimatedSprite2D.play()
else:
$AnimatedSprite2D.stop()
$AnimatedSprite2D.frame = 1
func boost():
$BoostTimer.start()
$CoffeePickup.play()
speed = BOOST_SPEED
func quick_fire():
$FastFireTimer.start()
$GunPickup.play()
$ShotTimer.wait_time = FAST_SHOT
func extralife():
$LifePickup.play()
func on_hit():
hurtblink.play("hurtblink")
hurttimer.start()
await hurttimer.timeout
hurtblink.play("RESET")
func _on_shot_timer_timeout():
can_shoot = true
func _on_boost_timer_timeout():
speed = START_SPEED
func _on_fast_fire_timer_timeout():
$ShotTimer.wait_time = NORMAL_SHOT
func _on_resume_button_pressed():
get_tree().paused = false
$PauseMenu.visible = false
func _on_quit_button_pressed():
get_tree().change_scene_to_file("res://scenes/title_screen.tscn")
If anyone knows a solution to this issue, please let me know as soon as possible. Thank you!