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.
Umbilicus ps: When you press the button in 1 scene, the music should not work in all scenes
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?
Jovitajovitah At the moment I have AudioStreamPlayer 2D in 1 scene
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.
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 Iceland beat me to it while I made sure I was giving you the right info 😂 give them the credit please
Venesection Haha, I don't need the credit. I'm very amused by the fact that we both posted the exact same answer. 😂
Holman I've done an automatic upload, now all the scenes have music. BUT when you click on the on/off button, the window crashes.
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()
Holman Thanks everything works, 1 more question I would like to remove this sound in some scenes, how to do it?
Same way. ControlMusic.stop()
It is an Autoload, so it exists and is accessible from every scene.
Holman func _pressed() -> void:
if BackGroundMusic.playing:
BackGroundMusic.stop()
so?
Umbilicus 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"...
© 2022 - 2024 — McMap. All rights reserved.