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:
func play_music():
if not bg_music.is_playing():
bg_music.play()
elif bg_music.is_playing():
return
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()
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()
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:
Regarding errors, the music plays from the very beginning after reloading the scene, and this error is most likely in the play_music() function
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
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)