Need help with singleton!

Godot Version

4.3

Question

How can I make it so that if the music is playing, it will not be played from the beginning, otherwise, if it is not playing, it will be played? I will say that I tried to make this code:
image

func play_music():
	if not bg_music.is_playing():
		bg_music.play()
	elif bg_music.is_playing():
		return

Please help!

Hi,

I use the following sort of function in my sound manager code, this is assuming you’re using an Audiostream player created in code or part of your scene.

func play_music() -> void:
	if not bg_music.playing:
		bg_music.play()

Ryn

1 Like

Doesn’t work ( But regarding AudioStreamPlayer, I have it as part of the scene

Can you show your scene and errors. If bg_music is the name then prefix it with a $ symbol to locate in your scene or control drag it into your script to get an @onready name in the script.

func play_music() -> void:
	if not $bg_music.playing:
		$bg_music.play()
1 Like

If you just want to toggle music on and off, this should be enough:

func play_music():
	bg_music.playing = !bg_music.playing
1 Like

Script of AudioManager:

extends Node

@onready var bg_music: AudioStreamPlayer = $background_music
@export_range(-80, 15) var volume : int = 0

func play_music() -> void:
	if not bg_music.is_playing():
		bg_music.play()

func stream_music(stream_file: String):
	bg_music.stream = load(stream_file)

func stop_music():
	bg_music.stop()

func set_volume():
	bg_music.volume_db = volume

func fade_in(duration: float):
	bg_music.volume_db = -80
	var tween1 = get_tree().create_tween()
	tween1.tween_property(bg_music, "volume_db", 0, duration)

func fade_out(duration: float):
	bg_music.volume_db = 0
	var tween1 = get_tree().create_tween()
	tween1.tween_property(bg_music, "volume_db", -80, duration)

Scene Tree:
image
Regarding errors, the music plays from the very beginning after reloading the scene, and this error is most likely in the play_music() function

Change the line

if not bg_music.is_playing():

to

if not bg_music.playing:
1 Like

Doesn’t work again(

What is happening are any errors displayed, Have you assigned a sound file to the audiostream player.

Is this a global script or part of the actual active scene, if the scene is not active then the sound will not play.

Where are you calling Play_music() from. I’m assuming the _ready() method?

1 Like

Yes, the scene is global so that the music plays not in the scene but in the game, and also the play_music() function is played in the _ready() function in the level script

Can you please post the code where you call the play_music() function… Also a screen shot of your globals would be helpful

Ryn

1 Like

Global script :

extends Node

var keys : int = 0
var room : int = 1
var deaths : int = 0

var player = self

func _process(delta: float) -> void:
	if Input.is_action_just_pressed("f11"):
		if DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_WINDOWED:
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
		else:
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)

Script with play_music():
image

extends Node2D

@export var stream_music : String

func _ready() -> void:
	AudioManager.stream_music(stream_music)
	AudioManager.play_music()

Okay, thanks,

You’re reloading stream each time you enter the scene which would restart the audio so try this change:

func stream_music(stream_file: String) -> void:
	if not bg_music.playing:
		bg_music.stream = load(stream_file)
1 Like

It worked! Thank you very much!!!

No problem, please mark the thread as solved… Have a good day.

Ryn

1 Like