• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

BGM Memory (Resume After Battle) [v17.2-v18]

Boonzeet

Pokémon Secrets of the Ages Developer
188
Posts
15
Years
  • Simple script that allows remembering a BGM's position so the next time it's played it plays from that spot.

    This REQUIRES FMod and will not work without it. FMod can be set up by following these instructions.

    Unfortunately this means this mod is not yet compatible with MKXP, due to a bug within MKXP itself. This is being worked on. This script is compatible with both v17.2 and v18.

    Place this script after FMod & RGSS Linker and above Main:

    Code:
    #==============================================================================
    # BGM Pause and Resume
    #------------------------------------------------------------------------------
    # v1.0 by Boonzeet
    # Requires FModEx to work. Place BELOW your FMod Audio & RGSS Linker scripts
    #
    # Please credit if used
    #==============================================================================
    
    module Audio
      module_function
      # modified bgm play function
      def bgm_play(file_name, volume = 100, pitch = 100, position = 0)
        volume = volume * @master_volume / 100
        return @bgm_play.call(file_name, volume, pitch) if(@library != ::FmodEx)
        filename = check_file(file_name)
        bgm = ::FmodEx.bgm_play(filename, volume, pitch)
        if position > 0
          bgm.set_position(position)
        end
        @current_bgm = bgm
        loop_audio(bgm, file_name)
      end
      # gets the play position of the current track
      def bgm_pos
        return @current_bgm ? @current_bgm.get_position : 0
      end
    end
    
    #------------------------------------------------------------------------------
    # Game System edits
    #------------------------------------------------------------------------------
    
    class Game_System
      attr_accessor :bgm_positions
    
      alias initialize_bgmplayback initialize
      def initialize(*args)
        initialize_bgmplayback(*args)
        @bgm_positions = Hash.new(0)
      end
    
      # BGM Position setter
      def bgm_setpos(name, position=0)
        return if !name
          Kernel.echo("Setting BGM #{name} pos to #{position}\n")
        @bgm_positions = Hash.new(0) if !@bgm_positions
        if @bgm_positions[name] && position == 0
          @bgm_positions.delete(name)
        else
          @bgm_positions[name] = position
        end
      end
     
      # BGM Position getter
        def bgm_getpos(name)
        @bgm_positions = Hash.new(0) if !@bgm_positions
            if @bgm_positions.key?(name)
          result = @bgm_positions[name]
          Kernel.echo("Got BGM #{name} pos as #{result}\n")
                return result
            else
                return 0
            end
      end
     
      # Clear all positions
      def bgm_clearpos
        @bgm_positions = Hash.new(0)
      end
    
      # Get the position of the current playing track
      def bgm_current_getpos
        return 0 if !@playing_bgm
        pos = Audio.bgm_pos
        Kernel.echo("Got current BGM pos as #{pos}\n")
        return pos
      end
    
      # Remembers the position of the current playing track
      def bgm_current_rememberpos
        return if !@playing_bgm
        self.bgm_setpos(@playing_bgm.name, bgm_current_getpos)
      end
    
      def bgm_pause(fadetime=0.0) # :nodoc:
        self.bgm_current_rememberpos
        self.bgm_fade(fadetime) if fadetime>0.0
        @bgm_paused   = true
      end
    
      def bgm_unpause  # :nodoc:
        self.bgm_setpos(@playing_bgm.name, 0) if @playing_bgm
        @bgm_paused   = false
      end
    
      def bgm_resume(bgm) # :nodoc:
        if @bgm_paused
          pos = self.bgm_getpos(bgm.name)
          self.bgm_play_internal(bgm,pos)
          self.bgm_setpos(bgm.name, 0)
          @bgm_paused   = false
        end
      end
    
      def bgm_stop # :nodoc:
        self.bgm_setpos(@playing_bgm.name, 0) if !@bgm_paused
        @playing_bgm  = nil
        Audio.bgm_stop if !@defaultBGM
      end
    
      def bgm_fade(time) # :nodoc:
        self.bgm_setpos(@playing_bgm.name, 0) if !@bgm_paused
        @playing_bgm = nil
        Audio.bgm_fade((time*1000).floor) if !@defaultBGM
      end
     
      def bgm_play_internal(bgm,position=0) # :nodoc:
        Kernel.echo("!!! Play internal #{bgm.name} at pos #{position}\n")
        @playing_bgm = (bgm==nil) ? nil : bgm.clone
        if bgm!=nil and bgm.name!=""
          if FileTest.audio_exist?("Audio/BGM/"+bgm.name)
            bgm_play_internal2("Audio/BGM/"+bgm.name,bgm.volume,bgm.pitch,position) if !@defaultBGM
          end
        else
          @bgm_position = position if !@bgm_paused
          @playing_bgm = nil
          Audio.bgm_stop if !@defaultBGM
        end
        if @defaultBGM
          bgm_play_internal2("Audio/BGM/"[email protected],@defaultBGM.volume,@defaultBGM.pitch,position)
        end
        Graphics.frame_reset
      end
    end
    
    #------------------------------------------------------------------------------
    # Custom functions
    #------------------------------------------------------------------------------
    
    def pbRememberBGM
      $game_system.bgm_current_rememberpos
    end
    
    def pbRestoreBGM(bgm)
      $game_system.bgm_resume(bgm)
    end
     
    alias pbPrepareBattle_rememberbgm pbPrepareBattle
    def pbPrepareBattle(*args)
      pbRememberBGM
      pbPrepareBattle_rememberbgm(*args)
    end

    To remember a piece of music before another piece plays, for example if playing Low HP music in battle, simply use pbRememberBGM.

    Enjoy and please credit if used!
     
    286
    Posts
    5
    Years
    • Seen May 9, 2024
    Thanks so much for this, it works perfectly! I've been trying to implement something like this myself, but have had no luck.
     

    thepsynergist

    Vinemon: Sauce Edition Programmer and Composer
    795
    Posts
    15
    Years
  • I was trying to get this working with the 'LowHP BGM' of EBS, but I wasn't having much luck. Would you happen to know how they might be able to integrate? It works perfectly, otherwise!
     

    Boonzeet

    Pokémon Secrets of the Ages Developer
    188
    Posts
    15
    Years
  • I was trying to get this working with the 'LowHP BGM' of EBS, but I wasn't having much luck. Would you happen to know how they might be able to integrate? It works perfectly, otherwise!

    Before these two lines:
    Code:
    $game_system.bgm_memorize
            pbBGMPlay("lowhpbattle")

    in both occurences, add in a line $game_system.bgm_current_rememberpos
     
    Back
    Top