0 votes

when I play a music, and change and scene it restarts, how do I make it play continuously.

asked Dec 13, 2019 in Engine by RichardSilsa (12 points)

1 Answer

0 votes

The AudioStreamPlayer node needs to be separate from the scene, so the scene tab needs to look like this.

New Scene
   > The game scene
   > AudioStreamPlayer

and instead of using get_tree().reload_current_scene(), load the scene and instance it like this.

export var level : PackedScene
var levelInstance = load(level.get_path())

func _restart():
    your_current_level_var.queue_free()
    get_owner().add_child(levelInstance.instance())
answered Dec 13, 2019 by Magso (2,713 points)

but what are the information i need to put,and where i put it.
(im sorry im am new in gdscript)

You haven't said what you've got so far so I'm assuming you've created the scene and then used get_tree().reload_current_scene() which as it says, will reload everything in that scene including any music.
I'm saying remove the music from the game scene, create a new empty scene and load the game scene and music separately so the music doesn't restart. However now you can't use get_tree().reload_current_scene()because it will reload the empty scene. The code I gave you is how to load a scene resource as a child into the current scene.
As for your_current_level_var.queue_free() this removes the previously played game scene so the new one can spawn. your_current_level_var needs to reference the current game scene.
for e.g.

func _restart():
    your_current_level_var.queue_free() //deletes current scene
    your_current_level_var = levelInstance.instance() //sets new scene to variable
    get_owner().add_child(your_current_level_var) //creates the new current scene
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.