Adding Game Over Sound effect

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rayu

When my Scene hit it’s time to 0 (Game Over) the ConvasLayer pops up (it becomes visible)
on that CanvasLayer is the Score and Highscore.
and when the time hits 0 and the CanvasLayer pops up i want to add a soundeffect

Game Scene Script:

    func _ready() -> void:
    	$ControlScore/ScoreCount.text = str(Globals.Score) 
    	var actor_id = randi() % 4 + 1
    	if actor_id == 1:
    		for _i in range(4 + Globals.Score):
    	elif actor_id == 2:
    		for _i in range(4 + Globals.Score):
    	elif actor_id == 3:
    		for _i in range(4 + Globals.Score):
    		for _i in range(4 + Globals.Score):

    # 1 of the player and enemy scripts, all are basically the same

func spawn_player1(num):
	for _i in range(num):
		var w = Player1.instance()
		w.connect("player1_pressed", self, "_on_player1_pressed") 

func _on_player1_pressed():
	if Globals.timeLeft > 0:
		Globals.timeLeft += 5
		if Globals.timeLeft > Globals.maxTime:
			Globals.timeLeft = Globals.maxTime
	Globals.Score += 1
	get_tree().call_group("Players", "next_level")
	get_tree().call_group("Enemies", "hide_for_time", 0.5)
	yield(get_tree().create_timer(0.5), "timeout")
func spawn_enemy1():
	var rand = floor(rand_range(0, Enemy1.size()))
	var piece = Enemy1[rand].instance()

ControlTimer Script: (Where the time hits 0 and sends signals )

func _process(delta):
	if Globals.timeLeft > 0:
		Globals.timeLeft -= delta
		$TimerCount.text = str(Globals.timeLeft)
		$TimerCount.text = "%d" % Globals.timeLeft
		Globals.timeLeft = 0
		get_tree().call_group("Player", "game_over")
		get_tree().call_group("Enemies", "game_over")
		get_tree().call_group("GameOverGroup", "game_over")  <---- Game Over Signal to CanvasLayer

CanvasLayer (inherited Scene of Game Scene)
Script of a child node

extends Control

func game_over():
	visible = true    <---- here the canvas Layer gets Visible and at that  point i want to add the Game Over sfx

	if Globals.Score > Globals.highscore:
		Globals.highscore = Globals.Score
	$VBoxContainer/Best.text = "Best  " + str(Globals.highscore)
	$VBoxContainer/Score.text = "Score  " + str(Globals.Score)

But the signal i coming from a _process delta function it either giving a bug sound or i doesn’t play at all. I’ve tried to put the Sound effect on all functions where the Game Over is happening but it doesn’t play at all.

I don’t know where to put it
oh and btw the Sound effect is on a Autoload

:bust_in_silhouette: Reply From: jgodfrey

Hmmm… You have a strange habit of asking a question, getting an answer, ignoring it (or, at least not responding to it), and then asking the same or similar question again.

In this case, you already asked this (or something similar) here:

And I provided an answer, to the best of my ability, based on the limited amount of code you provided (and, you never followed up).

While you’ve provided more code here, any traction we gained in the other thread is completely lost when you “start over” in a new thread.

Please try to keep this stuff consolidated for the sake of those trying to help as well as those trying to use this forum as a resource for solving their problems in the future…

And, know that the people who assist here are volunteers who can certainly find other things to do with their time. Don’t make it harder than necessary for them to help you.

Now, to your question(s)…

I’d say the underlying problem is the same as in the other question (linked above). You’re going to process that else side of your _ready() function in every frame once your timer reaches zero. That’ll cause your game_over function to be called in every frame, which I assume isn’t what you want.

While there are lots of things that could/should be cleaned up in the code you provided, you can get that GameOver stuff to be called only once by making this adjustment…

var game_over = false # <--- flag to track game_over status

func _process(delta):
    if Globals.timeLeft > 0:
        Globals.timeLeft -= delta
        $TimerCount.text = str(Globals.timeLeft)
        $TimerCount.text = "%d" % Globals.timeLeft
        if !game_over: # <--- if game isn't over...
            game_over = true # <--- now the game is over
            Globals.timeLeft = 0
            get_tree().call_group("Player", "game_over")
            get_tree().call_group("Enemies", "game_over")
            get_tree().call_group("GameOverGroup", "game_over")

Also, you mention that your soundeffect is an autoload. While I don’t know exactly what that means to you, if you’re saying the sound can be played by calling a function in an autoload script, then you should be able to just call it from your game_over() function.