We’ve been talking about this Options Menu for a while, if only because there’s so much we can do with it. This will be the last post for a while, but rest assured we’ll be revisiting the topic.
RPG Maker doesn’t come with volume control right off the bat. In the F1 menu you can turn on or off certain sounds, but that’s it. You can’t, for example, make the music half volume and the sound effects full. In his script, Yanfly has given you complete control over that, which is awesome. But what is sorely missing is a Master Volume slider, something which allows you to adjust this all at once. Let’s build it.
Start At The Top
We’ve gotten pretty familiar with two parts of Yanfly’s script when adding and removing commands. The first section, COMMANDS, deals with the order of things. We need to add :master_volume to this list, wherever you’d like it to go. (I like it right at the top).
If we scroll down a little, we’ll see where the display text goes. We want to add a new section for master volume, and provide our text.
If you’re uncomfortable with these sections, or confused, or just want a quick refresher, head over to last week’s post where we dealt with these two sections in depth.
Make a Slider
Rather than build up a menu option from scratch, we’re going to plug into Yanfly’s way of doing things to save a ton of time and code. (Don’t worry, we’ll build an option from scratch soon!). Yanfly already has the methods in place to display a slider; to control it; to use shift to rapidly change values; and to take the value set and store it somewhere. Let’s just borrow all of that for our new Master Volume Slider.
- Go to around line 651 and find the function “make_command_list.” This function (you guessed it” builds all our commands. It does different things based on what the command is – if it’s volume or a switch or a variable or whatever. The bulk of the function is a “case statement” – it looks at the command and says “if you’re autodash, do X. if you’re battle animations, do Y.” Since we’re plugging into some pre-fabs, find the line with all the volume options and add your :master_volume command.
- Head further down to around line 697, “draw_item.” We’ve got another case statement. Do the same thing – find the line that has the volume options, and add :master_volume.
- One more time. Scroll down to line 898 to find “cursor_change”. Another case statement, another addition.
We’re now generally “plugged-in” to Yanfly’s system to build a slider. Unfortunately, it doesn’t do anything right now.
Before we figure out what Master Volume does, exactly, we should look at what Yanfly has done to the volumes of Background Music (BGM), Background Sound Effects (BGS), and Music Effects (ME) and Sound Effects (SE). If you think about it, RPG Maker actually does support volume for each of these items – but it doesn’t give the player any control. Whenever you set background music or a sound effect in the game, you can control the volume. So, Yanfly didn’t have to build an entire volume system.
Instead, what Yanfly does is take the number from the slider and multiply the volume of anything by that percent. So, if your BGM slider is set to 100, any music that gets played has its volume multiplied by 100% (or 1) before the music is played. If BGM is lower, say at 75, then we multiple the volume by .75 before playing it. Easy, right?
What we want Master Volume to do is the same thing – to all sound. So when we play a BGS in game, for example, the volume we want is:
Volume set by creator when making game X BGS volume slider in options menu X Master Volume slider in options menu
The first two parts of that are already what Yanfly is doing. We just want to add the last little bit. Let’s do it!
Head to around line 675 and look for a function called “draw_volume.” You’ll see – shocker – another case statement. This one, though, breaks up our volume types into different functions. Yanfly has created a variable called volume in $game_system which stores volume data for us. So we’ll add another line right before the end that says:
when :master_volume rate = $game_system.volume(:master)
The whole section looks like this:
All that did was change the display graphic (a big hint is that the function is called “draw_volume” not “change_volume”). We still want moving the slider to actually set the Master Volume, so scroll down to around line 935 for “change_volume” – which I gave away in the sentence before.
Another case statement, we’ll add master volume again here.
when :master_volume $game_system.volume_change(:master, value) RPG::BGM::last.play RPG::BGS::last.play
Those last two lines, what’s happening here? Why did I use two when the others have either 1 or none. While your menu is open, you could have BGM or BGS playing (in fact, you should! Title screen music is important!). We want to immediately change the volume, but not restart the music. We call RPG::BGM::last.play to “update” the BGM (and similar to update the BGS). Notice that in Yanfly’s script, he only updates BGM when we change BGM volume, and BGS when we change BGS volume. Since master volume impacts both of them, we need to update both.
Back to the Equation
Finally, we have the thing we need to add to our equation. In case you didn’t catch it, it’s $game_system.volume(:master) – that’s where we store our master volume setting. Scroll up to around line 298 where Yanfly builds out his equations. You’ll see four separate sections – BGM, BGS, SE, ME. In each, look for the line that has an equation which modifies the volume, and add our master volume. All in, it looks like:
volume *= $game_system.volume(:bgm) * 0.01 * $game_system.volume(:master) * 0.01 unless $game_system.nil?
With the :bgm part changed in each section to accurately refer to the correct volume setting. Et voila! Our master volume system is now in place!