Multi-Resolution Game Development With GameBuilder Studio Check it out!
NEW Version 0.9.7 has arrived! New Blazing Fast Particle Engine & More Game Actions. Check it out
Toggle for Music/Sound On and Off
  • I set up a BackgroundSoundPlayer on a layer in my game. I have AutoStart checked to run when the game starts.

    Then I have a 'volume' button on the UI layer I am hoping to have as a toggle On or Off for all sounds in the game at once.

    Does anyone know how to do this?


    I thought some how setting up a Global called sound set to 0 might be involved with the process . . .
  • 13 Comments sorted by
  • Hi @BenFromOregon

    Currently there are two ways to do this. You can stop all sounds when you use a Pause Game action or you can use the Background Sound Player component and setup a global "soundsPlaying" property. When this is set to 0 use the Call Method action and point to the stop method on that component (i.e. "@BackgroundSound.stop" ). The input field is a property reference but the icon is missing. You do not need quotes around the reference. To play the sound again use another Call Method action and point to the "start" method. Sorry this is only documented in the underlying game engine code.

    In the future there will be a more robust way to manage sound but for now this works.
  • Does it matter what layer I place the Background Sound player on?

    I was able to get the music to turn off, just not back on again.
    Here is what I did.

    I set up a Background Sound Player on a Global Game State (generic object) and called it Music, Auto Start checked.
    I set up a global component "soundsPlaying" property and set that to 1..
    Then on my music button on the UI layer I have a Rules component condition When the "soundsPlaying" property is greater than 0 and when the Touch is released on the entity(music button), Trigger a change property to set the global "soundsPlaying" property to 0.

    Back on the Global Game State where the Background music is I added a Rules component condition When the "soundsPlaying" property is equal to 0, Trigger a Call method @Music.stop  and it worked great!

    Now where am I going wrong to get it back on with the same music button touched again?

    I set up an additional Rules component on the music button on the UI layer with the condition of When the "soundsPlaying" property is equal to 0 and the Touch is released on the entity(music button), Trigger a change property to set the global "soundsPlaying" property to 1.
    Back on the Global Game State where the Background music is I added a Rules component condition When the "soundsPlaying" property is equal to 1, Trigger a Call method @Music.start

    The music does not return. 

    I've even tried setting the "soundsPlaying" property to 2 from a touch release when the music was off(soundsPlaying set to 0) and on the Global Game State had the rules component listen for the touch release setting the Global property to 2 and then call Method @Music.start


    Any ideas?
  • @BenFromOregon looks like you have found a bug. The fix for this will be in the new build that is about to be released. There will also be a new pause method that you can use instead of stop.
  • Great!
    With this and the sprite frames per second running with the next build, things will really pick up here.
    Your hard work and assistance is greatly appreciated, thank you.
  • @BenFromOregon sure no problem. I sent you a new release candidate build to test the new fixes. Thanks for your patience!
  • So is there a simple method for toggling sound on/off?? It doesn't look that simple for what I've red in the forums
  • @kikubu There is a toggle property action that was mentioned HERE
    I haven't used it yet as I had already set up the background music toggling approach mentioned in that post with the expression (Globals.Data.soundsPlaying + 1) % 2

    soundsPlaying is just the name of a global property I created. With sound effects in the game I would have them play(trigger) on a collision for example if this global property was 1 .

    I used a button to toggle this global from 1 to 0 and 0 to 1.

    That is just how I did it, not saying it's the best or simplest.

  • @kikubu yes just create a property called "musicOn" or something make it a boolean and trigger music play and music pause method on a background music component based on if that property is turn on or off. You can now easily toggle the property using the toggle property action.
  • Then I should have:
    1- A Global property ex: 'musicOn' set to boolean and unchecked. 
    2- A button with a toggle property ex. "#Globals.Data.musicOn" on mouseDown.
    2- A Sound entity (autoplay on??) with 2 rules map:
         a) soundOn: when #Globals.Data.musicOn is set to "true", trigger -> call Method -> @Music.pause
         b) soundOn: when #Globals.Data.musicOn is set to "false", trigger -> call Method -> @Music.play

    is that right? 
  • @kikubu Yea that sounds right except when musicOn is true you should call play and when musicOn is set to false you should call pause. Yes you can have autoplay on if you want.
  • I can't make it work. 
    The action for the music to play should be: Trigger -> Call Method ->@Music.play, on the music entity ???
  • If you are just trying to have a button to turn off/on the games background music, this should work as it is essentially what I did:

    FIRST:
    Create a Global property and call it musicOn if you like and set it to integer 1.

    SECOND:
    Create a Sound entity (use one of those square shaped basic spatials from the GBs editor) and call it whatever, Sound, for example. Place it off stage.

    Next, on the Sound entity components list click the Add plus sign and add a Background Sound Player. Choose your audio file. leave Auto Start unchecked.
    Now also add a Rules Map that checks for a property of:
    #Globals.Data.musicOn = 0

    Trigger:(use the Call Method action here)
    @Music.pause

    Otherwise: (use the Call Method action here)
    @Music.play


    THIRD:
    On your button you created to turn on/off the background music add a Rules Map that checks for an Event Captured.
    When: 
    Touch is Released on Entity checked.

    Trigger:(use a Change Property)
    #Globals.Data.musicOn =  Type Expression    ( Globals.Data.musicOn + 1) % 2


    This will change that Global musicOn back and forth from 1 to 0 and 0 to 1 every time touch is released on the button.(mouse Up)

    This approach does work. Using the new Toggle Property action looks like it would only require that your musicOn global property be set to the true/false boolean rather that 1/0 integers and you would no longer need the expression ( Globals.Data.musicOn + 1) % 2

    Good Luck.
  • @kikubu in your case I believe it is because you are pointing to the wrong music component on the wrong entity. Check where your reference is pointing to.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Tagged