Issue loading "music off" attribute
Hi,
I am trying to create a button that turns the music on/off and saves the settings when the app is closed and re-opened.
I currently have a button that works for the sound effects, because I have set all sound effects within my game to use an attribute called "sound effects volume" to determine their volume. So I was able to change and save that particular attribute.
The music, however, doesn't allow you to determine the volume when played. So I have tried using the exact same code but with the device's music volume attribute. However, it doesn't seem to load the saved attribute correctly - If I turn it off, then exit the game and restart, it is back on again for the first time I play, but then mysteriously turns off when I restart the game or scene. It's as if the code hasn't run for the first game attempt?
I have tried creating a separate attribute for music volume (as you will see below) and then using this to change the device volume, but it still has the same issue. I have tried with boolean and integer attributes. I have also tried all settings on same button, but that caused a glitch where it would change itself multiple times on touch.
Here's a bit more detail about how I've set up the code:
NEW ATTRIBUTE (integer) - game.music
In my "settings" scene:
ACTOR 1: Music OFF button
-Rule 1: when game.music = 0, change image (so OFF button is highlighted)
-Rule 2: When game.music = 1 AND actor receives event: touch is pressed, change attribute: game.music to 0, save attribute: game.music (Key:music)
ACTOR 2: Music ON button
-Rule 1: when game.music = 0, change image (so ON button is no longer highlighted)
-Rule 2: When game.music = 0 AND actor receives event: touch is pressed, change attribute: game.music to 1, save attribute: game.music (Key:music)
Then in my "main menu" scene (the first scene to open in the app):
ACTOR: Loader
- Load attribute: key: music, attribute: game.music
- Change attribute: game.audio.music to game.music (This is supposed to set the device's volume to 0 when the player has toggled the music OFF button above, based on the game.music attribute being 0)
Note that even though the music off doesn't seem to work when the user presses "Play game" (a button on the main menu scene), the music then turns off when they press "retry", even without returning to the main menu.
Thank you for your help
Comments
Pretty sure you need to include a save/load attribute actor in there...at least a save where your on/off switch is and a load on first scene to "remember" attribute and act accordingly
Complete Guide to iOS Publishing {} Complete Guide to Mac Publishing
Hi @Thunder_Child, thanks for the input. If you check the code above again you will notice I have done exactly as you have suggested. The actors on the button both save as the last step, and the "loader" actor loads the attribute on the first scene
Hmmm. I didn't know you could put a word for a key....try using just "m" for your key ?
Complete Guide to iOS Publishing {} Complete Guide to Mac Publishing
I don't see any problem here try to change this but i think its not the right answer
Load attribute: key: music, attribute: game.music
to
or maybe the problem is at the game.audio.music or at the on off buttons.
try this create 2 integer like game.music on game.music off
when you push button change attribute to music off and put audio a rule when music off be off same for on too. I don't remember the codes after two years i opened game salad today.
Good news... my problem has been fixed.
There was obviously nothing wrong with the code, because the new GS update mysteriously fixed the problem. We've tested it multiple times and all good!
Thanks everyone for the suggestions.