I want the music not to start again when the player dies, but to continue. Could you help?

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

get_playback_position() doesn’t work.
I want the music not to start again when the player dies, but to continue. Could you help?
bores when the music starts again
Maybe code is working but the music restarts because I wrote the code:
get_tree (). reload_current_scene ()

I couldn’t find a solution.

extends Node
onready var music = $"../../AudioStreamPlayer"
onready var scene_tree: SceneTree = get_tree()
onready var score_label: Label = $Score
onready var pause_overlay: ColorRect = $PauseOverlay
onready var title_label: Label = $PauseOverlay/Title
onready var lab: Label = $PauseOverlay/PauseMenu/pause/pauseretry
const MESSAGE_DIED: = "Game Over"
var paused: = false setget set_paused
onready var gui = get_parent().get_node("/root/Level/gui/Control")
onready var joystick = get_parent().get_node("/root/Level/gui/joystick")
const SAVE_DIR = "user://user/"
var sound = 0
var music_position = 0
onready var list = list_files_in_directory("user://user/")
func list_files_in_directory(SAVE_DIRS):
	var files = []
	var dir = Directory.new()
	dir.open(SAVE_DIRS)
	dir.list_dir_begin()

	while true:
		var file = dir.get_next()
		if file == "":
			break
		elif not file.begins_with("."):
			files.append(file)
	dir.list_dir_end()
	return files

func _ready() -> void:
	#get_node("PauseOverlay/PauseMenu").visible = true
	get_node("loading").visible = false
	if PlayerData.connect("updated", self, "update_interface") != OK:
		print("x")
	if PlayerData.connect("died", self, "_on_Player_died") != OK:
		print("x")
	update_interface()
	get_tree().paused = false
	pause_overlay.visible = false
	gui.visible = true
	joystick.visible = true
	if not music.playing:
		music.play()

func _physics_process(_delta):
	if Input.is_action_pressed("pause") and title_label.text != MESSAGE_DIED:
		if get_tree().paused == false:
			if admanager.reklamkaldir == false:
				admanager.showBanner()
			get_tree().paused = true
			pause_overlay.visible = true
			gui.visible = false
			joystick.visible = false
		else:
			if admanager.reklamkaldir == false:
				admanager.hideBanner()
			get_tree().paused = false
			pause_overlay.visible = false
			gui.visible = true
			joystick.visible = true
	if Input.is_action_pressed("pause") and title_label.text == MESSAGE_DIED:
		get_tree().paused = false
		pause_overlay.visible = true
		gui.visible = false
		joystick.visible = false
		#PlayerData.score = 0
		var save_path = SAVE_DIR + str(PlayerData.sonlevel) + "save.dat"
		var file = File.new()
		if file.file_exists(save_path):
			var error = file.open_encrypted_with_pass(save_path, File.READ, OS.get_unique_id())
			if error == OK:
				ayarlar.levelden=1
				var _player_data = file.get_var()
				PlayerData.score = _player_data.get('coin')
				file.close()
			else:
				PlayerData.score = 0
		if list.size()==0:
			PlayerData.score = 0
		get_node("Score").visible = false
		get_node("PauseOverlay/PauseMenu/Main Menu").visible = false
		get_node("PauseOverlay/PauseMenu/pause").visible = false
		get_node("PauseOverlay/PauseMenu/quit").visible = false
		get_node("loading").visible = true
		$AnimatedSprite.play("load")
		get_node("AnimatedSprite").visible = true
		yield($AnimatedSprite, "animation_finished")
		if get_tree().reload_current_scene() != OK:
			print ("An unexpected error")
		if not music.playing:
			music.play(music_position)
		 #get_tree().reload_current_scene()
	if Input.is_action_pressed("quit"):
		get_tree().quit()
	if Input.is_action_pressed("main"):
		get_tree().paused = false
		get_node("loading").visible = true
		AudioServer.set_bus_volume_db(AudioServer.get_bus_index("Master"), -80)
		if scene_tree.change_scene("res://src/Screens/MainScreen.tscn") != OK:
			print ("An unexpected")
		 #scene_tree.change_scene("res://src/Screens/MainScreen.tscn")
	
func _on_Player_died() -> void:
	self.paused = true
	title_label.text = MESSAGE_DIED
	gui.visible = false
	joystick.visible = false
	lab.text ="Restart Game"
	if music.playing:
		music_position = music.get_playback_position()
		AudioServer.set_bus_volume_db(AudioServer.get_bus_index("Master"), -80)
		music.stop()
	var save_path = SAVE_DIR + str(PlayerData.sonlevel) + "save.dat"
	var file = File.new()
	if file.file_exists(save_path):
		var error = file.open_encrypted_with_pass(save_path, File.READ, OS.get_unique_id())
		if error == OK:
			ayarlar.levelden=1
			var _player_data = file.get_var()
			PlayerData.score = _player_data.get('coin')
			file.close()		
		else:
			PlayerData.score = 0
		if list.size()==0:
			PlayerData.score = 0
	if admanager.reklamkaldir == false:
		admanager.showBanner()
		admanager.showInterstitial()


func update_interface() -> void:
	score_label.text = "%s" % PlayerData.score

func set_paused(value: bool) -> void:
	paused = value
	scene_tree.paused = value
	pause_overlay.visible = value
:bust_in_silhouette: Reply From: professorik

Here you should rather understand why get_playback_position does not work. This option works for me:

global.audio_position = $AudioStreamPlayer.get_playback_position()

In the first scene

 $AudioStreamPlayer.play(global.audio_position)

In the second scene
You may have set the sound to zero or not using the global class

global - global singleton

professorik | 2020-11-09 15:50

Alright. Playing music with the global. I understand. Thank you for your help.

maxiproduksiyon | 2020-11-10 02:18

Hello. music continues to play from where it stopped.
There is no problem trying the game in Godot. But on mobile, the game causes a music problem. When the game is restarted, the music plays 2 times in a second. then a music plays. I’m using the “if not playing” code. And although the game loads with loading bar at startup, sometimes it loads a lot when the game restarts. the opening godot logo stays too long.
Is there a solution?

maxiproduksiyon | 2020-11-12 05:11

:bust_in_silhouette: Reply From: djmick

Make a global singleton script that plays the music. Here’s a good video about it: https://youtu.be/xT51BO8KrIg

. Thank you for your help. I will watch the video.

maxiproduksiyon | 2020-11-10 02:20

Hello. music continues to play from where it stopped.
There is no problem trying the game in Godot. But on mobile, the game causes a music problem. When the game is restarted, the music plays 2 times in a second. then a music plays. I’m using the “if not playing” code. And although the game loads with loading bar at startup, sometimes it loads a lot when the game restarts. the opening godot logo stays too long.
Is there a solution?

maxiproduksiyon | 2020-11-12 05:12