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.

HBGames

Fade In BGM
Version: 1.02


Introduction

Simple BGM Fade In

Features
  • Select the Maps u want the BGM to be faded in

Script

Code:
#==============================================================================
# Self Variables Script v. 1.02
# by Caldaron (16.09.2006)
#==============================================================================
module FadeIn
  #--------------------------------------------------------------------------
  MAPS = []        # Map IDs in which the BGM fades in
  FADE = 10        # Time in seconds of fade in
  #--------------------------------------------------------------------------
end
#==============================================================================
module RPG
  #--------------------------------------------------------------------------
  class AudioFile
    #--------------------------------------------------------------------------
    def initialize(name = "", volume = 100, pitch = 100, fade = 0)
      @name = name
      @volume = volume
      @pitch = pitch
      @fade = fade
    end
    #--------------------------------------------------------------------------
    attr_accessor :name
    attr_accessor :volume
    attr_accessor :pitch
    attr_accessor :fade
  end
  #--------------------------------------------------------------------------
end
#==============================================================================
class Game_System
  #--------------------------------------------------------------------------
  alias fade_init initialize
  #--------------------------------------------------------------------------
  def initialize
    fade_init
    @time = 0
    @wait = 10
    @fade = false
    @bgm_name = nil
    @bgm_volume = 0
    @bgm_pitch = 0
    @bgm_wait = 0
  end
  #--------------------------------------------------------------------------
  def bgm_play(bgm)
    @fade = false
    if bgm != nil and bgm.name != ""
      if bgm.fade != nil
        bgm_fade_in(bgm.name, bgm.volume, bgm.pitch, bgm.fade)
        @playing_bgm = RPG::AudioFile.new(bgm.name, bgm.volume, bgm.pitch, FadeIn::FADE)
      else
        Audio.bgm_play("Audio/BGM/" + bgm.name, bgm.volume, bgm.pitch)
        @playing_bgm = bgm
      end
      else
      Audio.bgm_stop
    end
    Graphics.frame_reset
  end
  #--------------------------------------------------------------------------
  def bgm_fade_in(name, volume, pitch, time)
    @playing_bgm = RPG::AudioFile.new(name, volume, pitch, time)
    @bgm_name = name
    @bgm_volume = volume
    @bgm_pitch = pitch
    @bgm_wait = time
    @fade = true
  end
  #--------------------------------------------------------------------------
  alias fade_update update
  #--------------------------------------------------------------------------
  def update
    if @fade
      @wait -= 1
      if @wait == 0
        @time += @bgm_volume/(@bgm_wait * 4.00)
        if @time >= @bgm_volume
          Audio.bgm_play("Audio/BGM/" + @bgm_name, @bgm_volume, @bgm_pitch)
          @fade = false
          @bgm_name = nil
          @bgm_volume = 0
          @bgm_pitch = 0
          @time = 10
        else
          Audio.bgm_play("Audio/BGM/" + @bgm_name, @time, @bgm_pitch)
        end
        @wait = 10
      end
    end
    fade_update
  end
  #--------------------------------------------------------------------------
end
#==============================================================================
class Game_Map
  #--------------------------------------------------------------------------
  def autoplay
    if @map.autoplay_bgm
      if FadeIn::MAPS.include?(@map_id)
        $game_system.bgm_fade_in(@map.bgm.name, @map.bgm.volume, @map.bgm.pitch, FadeIn::FADE)
      else
        $game_system.bgm_play(@map.bgm)
      end
    end
    if @map.autoplay_bgs
      $game_system.bgs_play(@map.bgs)
    end
  end
  #--------------------------------------------------------------------------
end
#==============================================================================
class Scene_Battle
  #--------------------------------------------------------------------------
  alias fade_phase5 update_phase5
  #--------------------------------------------------------------------------
  def update_phase5
    $game_system.bgm_stop
    fade_phase5
  end
  #--------------------------------------------------------------------------
  alias fade_end battle_end
  #--------------------------------------------------------------------------
  def battle_end(result)
    fade_end(result)
    $game_system.bgm_play($game_temp.map_bgm)
  end
  #--------------------------------------------------------------------------
end

Instructions

Put this Script above Main
to fade in BGM:
Code:
$game_system.bgm_fade_in("Name", Volume, Pitch, Time of Fade In (in secs))
Example:
Code:
$game_system.bgm_fade_in(
"020-Field03", 100, 100, 10)
or (for longer names):
Code:
$game_system.bgm_fade_in(
"020-Field03",
100,
100,
10)

FAQ

No Questions yet...

Incompatibility

Not known...

Credits and Thanks

Credits to me...

Author's Notes

damn! v. 1.02 was quiet hard to set up, but finally i got the clue ;)

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