Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Need Help with my BGM Volume script

Akin

Member

I need advice on one of my scripts. I'm pretty new to scripting, and I'm trying to make an options screen that allows the player to adjust the volume of the background music. I wrote this script to adjust the volume of the game by an amount defined by the "vol_adjust" variable. It seems to work fine at adjusting the volume. What I'm having trouble with is figuring out how to call the script to adjust the volume whenever a new BGM plays.

What I'm currently doing is running a parallel process common event with
Code:
bgmchange = Bgm_Adjust.new
bgmchange.vsetting(0.5)
*note right now I'm just setting the vol_adjust value to 0.5 to test the script I'll reset this to something the player defines later using the options screen.

I'm worried that my script is really inefficient, and I don't like having to use the parallel process common event. I was wondering if there was a seasoned scripter who could give me any advice that could help me improve my work?


My BGM Adjust Script
Code:
# BGMVolume Adjust Script
# Adjusts BGM volume according to the value of vol_adjust

class Bgm_Adjust
  #iniitialize the class
  def initialize
    if vol_adjust = nil
      vol_adjust = 1
    end
  end
	
  # This method gets information about the current BGM being played
  def vsetting(vol_adjust)
    bgm_playing = RPG::BGM.last
    current_bgm_n = bgm_playing.name
    current_bgm_v = 100
    current_bgm_p = 100
    if current_bgm_n != ""
      current_bgm_p = bgm_playing.pitch
      current_bgm_v = bgm_playing.volume
    end
    #This line resets the volume by the vol_adjust value (should be from 0-1)
    current_bgm_v = vol_adjust * current_bgm_v
    #This line plays the BGM with the adjusted volume
    if current_bgm_n != ""
      Audio.bgm_play("Audio/BGM/" + current_bgm_n.to_s, current_bgm_v, bgm_playing.pitch)
    end
  end
end
 
Don't use a class for this, but instead, I suggest just adding a method to the Audio module.
Code:
module Audio
  def self.adjust_bgm_volume(percent = 1.0)
    last = RPG::BGM.last
    last.volume = Integer(last.volume * percent)
    last.play
  end
end

So now, to adjust the volume, you could just use:
Code:
Audio.adjust_bgm_volume(0.5)

I haven't messed with RMVX that much, so let me know if this doesn't work.
   
 

Akin

Member

Ya that works. Thanks for the help I was really feeling like I needed a module method and not a class since I had no need to create objects. I originally tried to create my own module but I could never get it to function.

I was tearing through the RPGVX help file all day today trying to learn how the game audio functioned and learned about the Audio module and BGM Class (thats how I found the RPG::BGM.last method to get information about the last file played), but it never occured to me that I could add another method to the Audio module.

Hmmm after using it a bit it seems the volume is only set for the BGM as long as the map or the BGM doesn't change. I think the way you suggested is definately better, I'm just not able to get the result I want. I guess what I can't figure out is how to call the Audio.adjust_bgm_volume method whenever a new BGM is played. Its the same problem I had with my original that caused me to use a parallel process. If I parallel process the Audio.adjust_bgm_volume method though it will just continually reduce the current volume by the amount until you can't hear the bgm anymore.
 

Akin

Member

Ah, Took me awhile but I finally got it working, now all I have to do is add this into my options interface. It was pretty frustrating but I think I finally understand how RPGMaker handles sound.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top