0 votes

getplaybackposition() 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:
gettree (). reloadcurrent_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
in Engine by (43 points)

2 Answers

+1 vote
Best answer

Here you should rather understand why getplaybackposition 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

by (61 points)
selected by

global - global singleton

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

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?

+2 votes

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

by (570 points)

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

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.