[SOLVED] On/off music button for every scene

Hello everyone!
I have a new problem, I’m completely renewing the main menu of my game, and now I’ve set up music that starts at the opening of the game, and you hear it in every scene. The problem, as it is title, is that in the menu I would put a button to turn this music on and off everywhere. I tried to use variables, and other methods found on the net, but failed to do so.
Currently I use 2 buttons, 1 puts the volume at 100, the other to 0. Is there a lightweight way (for hardware) to set up audio from a single button?

Thank you very much!

1 Like

Hi,
What I did was set up a global variable Volume and use that for volume in every sound action.
Then I set the variable to 0 or 100 anywhere.

1 Like

So basically from your current button, you just want to toggle the audio/music volume from 0 to 100 (or 100 to 0)?
The logic for that and most other “toggle button” is to check a certain variable to see if the button (or in this case music) already been pressed:
When button is pressed, just check for the current volume number, is it 100 or 0. If it is 100 set it to 0 to mute it, and other way around.
Make sure this only occurs once when player clicks the button - use Trigger Once or change it when the button is released.

1 Like

I did about the same, but it doesn’t work… I’m going to do something wrong…

Yes, that’s pretty much what I tried to do, without success… I made several attempts, and now I’m stuck at this point…

Oh yes, ironic that we’re having an extension jam around toggle system…
Anyway, for your events:
Note that “The cursor/touch is on Music” means the action will occur immediately when mouse over. If you want to avoid that, add a “mouse button pressed/released” or “touch ended” event.
Then, your button event run into error where it immediately unmutes and mutes itself, because the two events (“the” and "problem :smiley: ") both occur.
To avoid this, you can use variable or animation - I’ll use animation since it’s simpler.
For both “The global variable music = 0” and “The global variable music = 1”, add a condition to check if the number of the animation of Music is currently 1, and then add action to immediately set the animation number to 0, so the next event won’t trigger.

1 Like

thank you for the explanation! but I don’t think I understand…
I did this, but the same doesn’t work

Other than mouseclick and mouseclick not being same spelling, that’s nearly correct actually.
Instead of playing and stopping the music, can you try just changing the volume instead? (To 0 or 100)

1 Like

Yes, this is 1 of the problems,( thank you! )but also another. I’ve changed from “music on the channel” to “sound on the channel”, and now everything works, except the animation that I’m going to fix now.
But now I have a question, is it a Gdevelop bug?

Here’s what I’ve changed:

1 Like

What bug? Misspelling your variables is not a bug, and using the ‘stop music’ action to start music is not a bug either.
If you do find a bug, do let us know.

not for this. I thought it was a bug because I don’t think it makes sense to use the “sound” option for background music. Wouldn’t it be more logical to use the “music” option?
But I could be wrong with “my way of thinking”, I just approached Gdevelop :slight_smile:

1 Like