查看: 92|回复: 2

[有事请教] 用豆包写了个控制音量的脚本,按f12出错

[复制链接]
梦石
0
星屑
1397
在线时间
1079 小时
注册时间
2011-3-26
回帖
121
发表于 2026-11-4 19:27:54 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
#==============================================================================
# ■ 音量系统 RGSS1(RMXP) 修复版(自建记录BGM/BGS名称,无Audio.bgm_name报错)
# 调用:事件脚本 $scene = Scene_Volume.new
#==============================================================================
class Game_System
  attr_accessor :bgm_vol
  attr_accessor :bgs_vol
  attr_accessor :se_vol
  attr_accessor :me_vol
  attr_accessor :current_bgm_name
  attr_accessor :current_bgs_name
  alias volume_initialize initialize
  def initialize
    volume_initialize
    @bgm_vol = 100
    @bgs_vol = 100
    @se_vol  = 100
    @me_vol  = 100
    @current_bgm_name = ""
    @current_bgs_name = ""
  end
end

#==============================================================================
# ■ 全局挂钩Audio,自动套用音量 + 记录当前BGM/BGS名称
#==============================================================================
module Audio
  class << self
    alias vol_bgm_play bgm_play
    def bgm_play(filename, volume = 100, pitch = 100)
      $game_system.current_bgm_name = filename
      vol_bgm_play(filename, $game_system.bgm_vol, pitch)
    end

    alias vol_bgs_play bgs_play
    def bgs_play(filename, volume = 100, pitch = 100)
      $game_system.current_bgs_name = filename
      vol_bgs_play(filename, $game_system.bgs_vol, pitch)
    end

    alias vol_se_play se_play
    def se_play(filename, volume = 100, pitch = 100)
      vol_se_play(filename, $game_system.se_vol, pitch)
    end

    alias vol_me_play me_play
    def me_play(filename, volume = 100, pitch = 100)
      vol_me_play(filename, $game_system.me_vol, pitch)
    end
  end
end

#==============================================================================
# ■ Scene_Volume 音量调节画面
#==============================================================================
class Scene_Volume
  def main
    items = [
      "BGM背景音乐:#{$game_system.bgm_vol}",
      "BGS环境音效:#{$game_system.bgs_vol}",
      "SE特效音效:#{$game_system.se_vol}",
      "ME提示音乐:#{$game_system.me_vol}"
    ]
    @command_window = Window_Command.new(360, items)
    @command_window.x = 140
    @command_window.y = 80
    @index = 0

    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze

    @command_window.dispose
  end

  def refresh
    items = [
      "BGM背景音乐:#{$game_system.bgm_vol}",
      "BGS环境音效:#{$game_system.bgs_vol}",
      "SE特效音效:#{$game_system.se_vol}",
      "ME提示音乐:#{$game_system.me_vol}"
    ]
    @command_window.dispose
    @command_window = Window_Command.new(360, items)
    @command_window.x = 140
    @command_window.y = 80
    @command_window.index = @index
  end

  def update
    @command_window.update

    # ↑ 上一项
    if Input.trigger?(Input::UP)
      $game_system.se_play($data_system.cursor_se)
      @index -= 1
      @index = 3 if @index < 0
      @command_window.index = @index
    end
    # ↓ 下一项
    if Input.trigger?(Input::DOWN)
      $game_system.se_play($data_system.cursor_se)
      @index += 1
      @index = 0 if @index > 3
      @command_window.index = @index
    end

    # ← 减小音量
    if Input.repeat?(Input::LEFT)
      $game_system.se_play($data_system.cursor_se)
      case @index
      when 0
        $game_system.bgm_vol = [0, $game_system.bgm_vol - 5].max
        # 如果存在BGM,重新播放,实时生效
        if $game_system.current_bgm_name != ""
          Audio.bgm_play($game_system.current_bgm_name, $game_system.bgm_vol, 100)
        end
      when 1
        $game_system.bgs_vol = [0, $game_system.bgs_vol - 5].max
        if $game_system.current_bgs_name != ""
          Audio.bgs_play($game_system.current_bgs_name, $game_system.bgs_vol, 100)
        end
      when 2
        $game_system.se_vol = [0, $game_system.se_vol - 5].max
      when 3
        $game_system.me_vol = [0, $game_system.me_vol - 5].max
      end
      refresh
    end

    # → 增大音量
    if Input.repeat?(Input::RIGHT)
      $game_system.se_play($data_system.cursor_se)
      case @index
      when 0
        $game_system.bgm_vol = [100, $game_system.bgm_vol + 5].min
        if $game_system.current_bgm_name != ""
          Audio.bgm_play($game_system.current_bgm_name, $game_system.bgm_vol, 100)
        end
      when 1
        $game_system.bgs_vol = [100, $game_system.bgs_vol + 5].min
        if $game_system.current_bgs_name != ""
          Audio.bgs_play($game_system.current_bgs_name, $game_system.bgs_vol, 100)
        end
      when 2
        $game_system.se_vol = [100, $game_system.se_vol + 5].min
      when 3
        $game_system.me_vol = [100, $game_system.me_vol + 5].min
      end
      refresh
    end

    # ESC 返回地图
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end
脚本在此,按下f12第31行会出错,豆包写的脚本,我也不知道怎么改……
人活着就是为了梦想而活,没有梦想和咸鱼没什么区别,我也不想等我老了才后悔为什么不年轻时实现梦想。
梦石
0
星屑
14629
在线时间
2704 小时
注册时间
2013-6-10
回帖
1718
发表于 2026-11-5 11:53:34 | 显示全部楼层
来自deepseek
问题分析

第31行是这一句:

```ruby
vol_bgm_play(filename, $game_system.bgm_vol, pitch)
```

出错原因:按 F12 重置时,$game_system 这个全局变量会先被清空(RGSS 重置时 $game_system 变成 nil),但 Audio.bgm_play 可能仍会被调用(比如重置过程中 BGM 恢复播放),此时 $game_system.bgm_vol 就会报 NoMethodError: undefined method 'bgm_vol' for nil。

另外还有一个隐患:Audio 模块在 RGSS 中是内置模块,用 alias 修改它,在 F12 重置时可能因为内置模块被重新初始化导致 alias 链断裂,但最常见、最先触发的还是 $game_system 为 nil 的问题。

修复方法

给所有 Audio 挂钩加上空值保护,$game_system 不存在时就用默认值 100。同时把 Audio 挂钩改成用 instance_variable 判断,避免 F12 时 alias 重复定义报错。

请把整个脚本替换成下面的版本:

```ruby
#==============================================================================
# ■ 音量系统 RGSS1(RMXP) 修复版
# 调用:事件脚本 $scene = Scene_Volume.new
#==============================================================================
class Game_System
  attr_accessor :bgm_vol
  attr_accessor :bgs_vol
  attr_accessor :se_vol
  attr_accessor :me_vol
  attr_accessor :current_bgm_name
  attr_accessor :current_bgs_name

  alias volume_initialize initialize
  def initialize
    volume_initialize
    @bgm_vol = 100
    @bgs_vol = 100
    @se_vol  = 100
    @me_vol  = 100
    @current_bgm_name = ""
    @current_bgs_name = ""
  end
end

#==============================================================================
# ■ 全局挂钩 Audio,自动套用音量 + 记录当前 BGM/BGS 名称
# (所有取值都做了 $game_system 空值保护,避免 F12 重置时崩溃)
#==============================================================================
module Audio
  class << self
    # 只在第一次加载时做 alias,防止 F12 重复定义
    unless method_defined?(:vol_bgm_play)
      alias vol_bgm_play bgm_play
      def bgm_play(filename, volume = 100, pitch = 100)
        sys = $game_system
        if sys
          sys.current_bgm_name = filename
          volume = sys.bgm_vol
        end
        vol_bgm_play(filename, volume, pitch)
      end
    end

    unless method_defined?(:vol_bgs_play)
      alias vol_bgs_play bgs_play
      def bgs_play(filename, volume = 100, pitch = 100)
        sys = $game_system
        if sys
          sys.current_bgs_name = filename
          volume = sys.bgs_vol
        end
        vol_bgs_play(filename, volume, pitch)
      end
    end

    unless method_defined?(:vol_se_play)
      alias vol_se_play se_play
      def se_play(filename, volume = 100, pitch = 100)
        sys = $game_system
        volume = sys.se_vol if sys
        vol_se_play(filename, volume, pitch)
      end
    end

    unless method_defined?(:vol_me_play)
      alias vol_me_play me_play
      def me_play(filename, volume = 100, pitch = 100)
        sys = $game_system
        volume = sys.me_vol if sys
        vol_me_play(filename, volume, pitch)
      end
    end
  end
end

#==============================================================================
# ■ Scene_Volume 音量调节画面
#==============================================================================
class Scene_Volume
  def main
    @index = 0
    create_command_window

    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze

    @command_window.dispose
  end

  def create_command_window
    items = volume_items
    @command_window = Window_Command.new(360, items)
    @command_window.x = 140
    @command_window.y = 80
    @command_window.index = @index
  end

  def volume_items
    [
      "BGM背景音乐:#{$game_system.bgm_vol}",
      "BGS环境音效:#{$game_system.bgs_vol}",
      "SE特效音效:#{$game_system.se_vol}",
      "ME提示音乐:#{$game_system.me_vol}"
    ]
  end

  def refresh
    @command_window.dispose
    create_command_window
  end

  def update
    @command_window.update

    # ↑ 上一项
    if Input.trigger?(Input::UP)
      $game_system.se_play($data_system.cursor_se)
      @index -= 1
      @index = 3 if @index < 0
      @command_window.index = @index
    end
    # ↓ 下一项
    if Input.trigger?(Input::DOWN)
      $game_system.se_play($data_system.cursor_se)
      @index += 1
      @index = 0 if @index > 3
      @command_window.index = @index
    end

    # ← 减小音量
    if Input.repeat?(Input::LEFT)
      $game_system.se_play($data_system.cursor_se)
      change_volume(-5)
      refresh
    end

    # → 增大音量
    if Input.repeat?(Input::RIGHT)
      $game_system.se_play($data_system.cursor_se)
      change_volume(5)
      refresh
    end

    # ESC 返回地图
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end

  def change_volume(delta)
    case @index
    when 0
      $game_system.bgm_vol = [[0, $game_system.bgm_vol + delta].max, 100].min
      if $game_system.current_bgm_name != ""
        Audio.bgm_play($game_system.current_bgm_name, $game_system.bgm_vol, 100)
      end
    when 1
      $game_system.bgs_vol = [[0, $game_system.bgs_vol + delta].max, 100].min
      if $game_system.current_bgs_name != ""
        Audio.bgs_play($game_system.current_bgs_name, $game_system.bgs_vol, 100)
      end
    when 2
      $game_system.se_vol = [[0, $game_system.se_vol + delta].max, 100].min
    when 3
      $game_system.me_vol = [[0, $game_system.me_vol + delta].max, 100].min
    end
  end
end
```

主要改动说明

1. Audio 挂钩加了 $game_system 空值判断(这是第31行崩溃的直接修复):
   · 重置过程中如果 $game_system 还是 nil,就直接用默认音量 100 播放,不再报错。
2. 用 unless method_defined?(:vol_xxx) 包裹 alias:
   · 防止 F12 重置后脚本被重新执行时,alias 一个已经不存在的旧方法导致 NameError。
3. 顺手整理了 Scene_Volume:
   · 把 main 和 refresh 里重复的建窗口代码合并成 create_command_window;
   · 左右音量变化合并成 change_volume(delta),逻辑一致但更短。

还有一个要注意的点

F12 重置后,Scene_Volume 如果还停留在当前场景,@command_window 等实例变量会丢失,但因为 $scene 会被重置回 Scene_Map,一般不会出问题。如果你发现 F12 后画面残留,可以在 Scene_Volume#main 开头加一句 @command_window.dispose if @command_window 的防御(上面版本已通过 create_command_window 重新创建来规避)。

替换后按 F12 应该不再报错了。如果还有报错,把完整报错信息(含行号和 message)发出来,我再帮你精确定位。

点评

谢谢大神,修复了,你真厉害!  发表于 2026-11-5 14:42

评分

参与人数 1赞 +1 收起 理由
习惯就好 + 1 认可答案

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部