Hey remember that time we made an options menu in our title screen? Hey remember that other time when we added a master volume slider to that options menu so we could have both easy and detailed control over the volumes in our game? Here’s the problem – the way we did it, that information isn’t saved. So when I start the game up again, too bad so sad, I need to reset the values again.

The past few weeks have been focused on how to save data that isn’t just a switch or variable, and also to save data outside of a save file (at the Game level). We’ve now got enough tools to save our volume settings so that they’re permanent no matter what we do. Unless of course “what we do” is intentionally change them.

A Recap

When we were building our master volume setting, we reflected on how Yanfly structured the volumes. He creates a new method in $game_system called “volume.” You can then access the various volume settings by calling volume(type), where type is a symbol. So, for example, if we want to get whatever’s currently set to the background music volume, we use:

$game_system.volume(:bgm)

Saving Data

Because we want to save this option across all save files (it is not something save-file specific) we’re going to write it to the ini. At the end of Yanfly’s option script, we’re going to write two new functions. Before we do that, scroll to around line 1031 and look for this:

class Scene_System < Scene_MenuBase

Remember last week we said that we need this stupid little string in any class which is going to attempt to write to an ini. So immediately after the class line, write:

WritePrivateProfileString = Win32API.new('kernel32', 'WritePrivateProfileString', 'pppp' , 'i')

This just means that now we get to write to the ini. Fantastic! Let’s write our first function, called save_volume_setting. This is what it looks like:

def save_volume_setting (key) value = $game_system.volume(key) section = 'Volume' filename = './Game.ini' WritePrivateProfileString.call(section, key.to_s, value.to_s, filename) end

Let’s break that down.

The first line uses “def” which defines the function. save_volume_setting is the name. It takes one “argument” called key. So if we want to make this script happen, you would use save_volume_setting(:bgm), for example.

The next line sets a variable called volume to the actual volume. So using the bgm example, if bgm is set to 100, value is now set to 100

Section and filename we talked about in our INI tutorial last week.

And then finally, we call our write to the INI. Note that everything has to be strings, and right now our key is a symbol and our value is an integer, so we need to add .to_s to convert them to a string.

Fabulous! This will now save any kind of volume setting we pass to it. If we do save_volume_setting(:bgs), we’ll save our Background Sound setting. If we do (:master) we’ll save our Master volume. We probably want to do all of them. And we do that in a second function.

def save_settings save_volume_setting(:master) save_volume_setting(:bgm) save_volume_setting(:bgs) save_volume_setting(:sfx) end

Just to be clear, this function is called save_settings. It takes no arguments. It calls the first function we made 4 times, and each time passes it a different setting to save. The first time it passes :master, the second time it passes :bgm, and so on. So we have one function that will save all of our volume settings. But nothing right now calls save_settings.

And let’s think about when that should happen. Remember we took out things like “To Title” and “Shutdown” when we made our title screen options menu, because they didn’t make sense here. So we should probably save on exiting the options screen. This is the “cancel” function.

Plugging Into Cancel

The end of our Yanfly script looks something like this:

Our command window here has three separate commands – cancel, title, and shutdown. Except in a prior tutorial, we disabled title and shutdown, meaning they won’t show up at all. All we have is cancel, which calls the function “return_scene.” That means “go back to the scene prior to this” – in this instance, our title screen, but if we came to this from elsewhere, then whatever that was.

We want it to return_scene when we cancel, but we want it to do more. We want it to call our save_settings function. So, first, replace :return_scene with :save_settings in the “create_command_window” function.

But if you do that alone and run it, you’ll notice that you can’t leave the options menu anymore. That’s because nothing is returning the scene! Head to the function we wrote above called save_settings and add a final line called “return_scene.” The whole function now looks like this:

def save_settings save_volume_setting(:master) save_volume_setting(:bgm) save_volume_setting(:bgs) save_volume_setting(:sfx) return_scene end

Et voila. Now, whenever you exit the options menu, it will call “save_volume_setting” for all of your settings.

This is Already Too Long – so we’ll save loading for next week 😉