Explanation: I made a music on/off button in 1 scene of my project, but in other scenes the music is not heard. In fact, the question is how to make music play in all scenes, but the on/off button only worked in 1 scene.
button music
- Edited
I assume you're using an AudioStreamPlayer node. Is that node present in all the scenes? I.e., is it in a location in the scene tree that is always present?
DaveTheCoder At the moment I have AudioStreamPlayer 2D in 1 scene
- Edited
You could create a scene with AudioStreamPlayer Node and attach a script to it.
To have music in all scenes go to ProjectSettings->Autoload and in path select your scene (.tscn) file and add it.
This way the music player will be available from everywhere. You don't need to add it manually in a tree. It will be loaded automatically.
You can then access it by its name. MusicPlayer.play() for example.
- Edited
Make your AudioStreamPlayer2D its own scene without the button and add it as an Autoload. Make sure the AudioStreamPlayer is at the root of the scene.
Then from your button you can call it just like this:
extends Button
func _pressed() -> void:
AudioPlayer.play()
Oops @trizZzle beat me to it while I made sure I was giving you the right info give them the credit please
Put your AudioStreamPlayer at the root of its scene:
This is all that should be in the scene. No Control
above it.
Then, make sure you're calling it the Autoload way and also that you are using the correct variables and function names.
func _pressed() -> void:
if ControlMusic.playing: #no $, check "playing" not "play"
ControlMusic.stop()
else:
ControlMusic.play()
Same way. ControlMusic.stop()
It is an Autoload, so it exists and is accessible from every scene.
VladFD ps: When you press the button in 1 scene, the music should not work in all scenes
Now just for a moment imagine that during the runtime there is only one "scene"...