button music
Asked Answered
U

12

0

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 answered 16/11, 2023 at 20:36 Comment(0)
U
0

Umbilicus ps: When you press the button in 1 scene, the music should not work in all scenes

Umbilicus answered 16/11, 2023 at 20:39 Comment(0)
J
0

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 answered 16/11, 2023 at 20:53 Comment(0)
U
0

Jovitajovitah At the moment I have AudioStreamPlayer 2D in 1 scene

Umbilicus answered 16/11, 2023 at 21:1 Comment(0)
A
0

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.

Asper answered 16/11, 2023 at 21:40 Comment(0)
H
0

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

Holman answered 16/11, 2023 at 21:43 Comment(0)
A
0

Venesection Haha, I don't need the credit. I'm very amused by the fact that we both posted the exact same answer. 😂

Asper answered 16/11, 2023 at 21:52 Comment(0)
U
0

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.

Umbilicus answered 16/11, 2023 at 22:11 Comment(0)
H
0

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 answered 16/11, 2023 at 22:32 Comment(0)
U
0

Holman Thanks everything works, 1 more question I would like to remove this sound in some scenes, how to do it?

Umbilicus answered 16/11, 2023 at 22:46 Comment(0)
H
0

Same way. ControlMusic.stop() It is an Autoload, so it exists and is accessible from every scene.

Holman answered 16/11, 2023 at 23:2 Comment(0)
U
0

Holman func _pressed() -> void:
if BackGroundMusic.playing:
BackGroundMusic.stop()
so?

Umbilicus answered 16/11, 2023 at 23:11 Comment(0)
M
0

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"...

Marvamarve answered 16/11, 2023 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.