Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: SilverSpook on Wed 04/01/2017 05:35:59

Title: Restore SFX, Music, Speech volume when player restarts?
Post by: SilverSpook on Wed 04/01/2017 05:35:59
In the default game I see that the game sets the default speech and sfx volume in the 'initialize_control_panel' function.

Code (ags) Select
  //Sound FX Volume
  Game.SetAudioTypeVolume(eAudioTypeSound, defaultSoundFXVol, eVolExistingAndFuture);
  Game.SetAudioTypeVolume(eAudioTypeAmbientSound, defaultSoundFXVol, eVolExistingAndFuture);
  sldAudio.Value = defaultSoundFXVol;
 
  //Music Volume
  Game.SetAudioTypeVolume(eAudioTypeMusic, defaultMusicVol, eVolExistingAndFuture);
  sldMusic.Value = defaultMusicVol;


Now if the player quits the game and restarts, the volume seems to go back to the default values.

Is there a way to make so if the player makes some changes to the volume sliders, these changes are still there the next time they start the game up?

Thanks
Title: Re: Restore SFX, Music, Speech volume when player restarts?
Post by: Gurok on Wed 04/01/2017 07:17:58
I would create two functions, one to save settings to a file and one to load them:

bool save_settings()
{
  File *config;
  bool result;

  config = File.Open("config.dat", eFileWrite);
  result = config != null;
  if(result)
  {
    config.WriteInt(sldAudio.Volume);
    config.WriteInt(sldMusic.Volume);
    config.Close();
  }

  return(result);
}

bool load_settings()
{
  File *config;
  bool result;
  int volume;

  config = File.Open("config.dat", eFileRead);
  result = config != null;
  if(result)
  {
    volume = config.ReadInt();
    Game.SetAudioTypeVolume(eAudioTypeSound, volume, eVolExistingAndFuture);
    Game.SetAudioTypeVolume(eAudioTypeAmbientSound, volume, eVolExistingAndFuture);
    sldAudio.Value = volume;
    volume = config.ReadInt();
    Game.SetAudioTypeVolume(eAudioTypeMusic, volume, eVolExistingAndFuture);
    sldMusic.Value = volume;
    config.Close();
  }

  return(result);
}


The load_settings() function returns true on success and false on failure. When you first run the game, load_settings() will fail because there won't be a config file, and the return value provides a nice way to deal with that.

In the part of initialize_control_panel where audio is set up, I would wrap the existing code like so:

if(!load_settings()) // Try to load the settings from the config file
{
    // If the file couldn't be opened, load the defaults instead:

    //Sound FX Volume
    Game.SetAudioTypeVolume(eAudioTypeSound, defaultSoundFXVol, eVolExistingAndFuture);
    Game.SetAudioTypeVolume(eAudioTypeAmbientSound, defaultSoundFXVol, eVolExistingAndFuture);
    sldAudio.Value = defaultSoundFXVol;

    //Music Volume
    Game.SetAudioTypeVolume(eAudioTypeMusic, defaultMusicVol, eVolExistingAndFuture);
    sldMusic.Value = defaultMusicVol;
}


In the OK button of the game's control panel, or whatever you're using to dismiss this dialogue box, use:

save_settings();

Again, the save_settings function returns true/false, and there are lots of reasons why you might not be able to save stuff to disk (disk full, no permission, locked file, etc.), so you might want to use something like:

if(!save_settings())
    Display("There was an error writing the configuration file.");


The last thing you'll want to do (presumably) is restore these settings when a game is restored. You can use the eEventRestoreGame event for this in your on_event function:

void on_event(EventType event, int data)
{
  if(event == eEventRestoreGame)
    load_settings(); // If there's an error reading the config, ignore it -- the previous settings will remain
}