スクリプトのヘルプ:選択スクリプトを無効にする

皆さん、こんにちは。RPGMaker XP用の「選択肢無効化」スクリプトを作成しようとしています。

HimesworkさんはVXAce用のものを作られていましたが、今はリンクが切れており、さらにRGSS3(RGSS1ではなく)を使用しているため、私には使えません(現在リンクが有効なものは、コードが何をするものなのかの説明がなく、すべて不明な状態です…)。

Google Search AIの助けを借りてここまで進めましたが、AIが整理してくれたコードは意図した通りに動作していません。

#==============================================================================

# ■ RPG Maker XP (RGSS1) 用 個別選択肢無効化

#==============================================================================

class Window_Command < Window_Selectable

  

  #--- テキストタグを動的にクリーンアップするための初期化オーバーライト ---

  alias xp_tag_choice_initialize initialize

  def initialize(width, commands)

    @raw_commands = commands.clone # タグを含む元のテキストを保存

    clean_commands = commands.map { |text| text.gsub(/\\[Ss]\[\d+\]/, "") }

    xp_tag_choice_initialize(width, commands)

  end

  

  #--- アイテムを描画。タグ付きスイッチがONの場合は色を変更 ---

  def draw_item(index, color)

    raw_text = @raw_commands[index]

    

    if raw_text =~ /\\[Ss]\[(\d+)\]/

      switch_id = $1.to_i

      if $game_switches[switch_id] == true

        self.contents.font.color = disabled_color

      else

        self.contents.font.color = color

      end

    else

      self.contents.font.color = color

    end

    

    rect = Rect.new(4, 32 * index, self.contents.width - 8, 32)

    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))

    self.contents.draw_text(rect, @raw_commands[index])

  end

  

  #--- 選択プロセスが生のタグを参照できるようにするアクセサ ---

  def raw_commands

    return @raw_commands

  end
end




#--- プレイヤーが無効なオプションを選択しようとした場合、入力をブロック ---

class Window_Selectable < Window_Base

  alias xp_tag_choice_update update

  def update

    if self.active && @item_max > 0 && Input.trigger?(Input::C)

      if self.is_a?(Window_Command)

        raw_text = self.raw_commands[self.index]

        if raw_text =~ /\\[Ss]\[(\d+)\]/

          switch_id = $1.to_i

          if $game_switches[switch_id] == true

            $game_system.se_play($data_system.buzzer_se)

            return

          end

        end

      end

    end

    xp_tag_choice_update

  end
end


このコードには重大な問題があるようです。実行すると、スイッチのテキスト(\s[1])が削除されるどころか、色の変更も行われません。おそらく、.cloneまたは.mapの処理で失敗していると思われます。

まず第一に、この部分について話します…

    @raw_commands = commands.clone # タグを含む元のテキストを保存
    clean_commands = commands.map { |text| text.gsub(/\\[Ss]\[\d+\]/, "") }
    xp_tag_choice_initialize(width, commands)

ここで「raw commands」を定義していますが、これは問題ありません。次に、commands.map を使って clean_commands という新しいコマンドリストを作成していますが、その後に第3行で依然として元の commands をパラメータとして渡しています。これが、正しく描画されない理由でしょう。この行を変更するだけで解決するはずです。

    # <--- commands の代わりに clean_commands を渡す
    xp_tag_choice_initialize(width, clean_commands)

描画の部分については、@raw_commands の代わりに @commands(または @raw_commands の内容)を使用すべきだと考えます。

self.contents.draw_text(rect, @raw_commands[index])

最後の部分は、単に機能しません。RMXP はウィンドウの管理方法が異なります。VXAce ではウィンドウ自体に「コールバックの追加」のロジックがあり、アクションはウィンドウ内部から呼び出されます。これにより、ウィンドウ自体に対してコードを実行しないよう指示したり、オプションを無効にしたりすることができます。一方、RMXP では、選択ボタンを押したときに何が起こるかを通常はシーンが決定します。そのため、特定の条件下で C キーを押した後に選択可能なウィンドウが戻るようにしても、ブザー音が鳴るだけで、無効化されるはずの処理は依然として実行されてしまいます。:\

これはどこに表示されるべきですか?

編集: コードの最初の部分を少し書き直しました。ご自由にお使いください。ただし、グレーアウトしたオプションを選択しようとしたときにアクションの実行を「スキップ」する部分がまだ欠けています。

class Window_Command < Window_Selectable
  
  alias ozwcd_initialize initialize unless $@
  def initialize(width, commands)
    @raw_commands = commands
    @switch_req = []
    commands = []
    @raw_commands.each_with_index {|c,i|
      cc = c.gsub(/\\[Ss]\[(\d+)\]/, "")
      commands.push(cc)
      @switch_req.push($1.to_i)
    }
    ozwcd_initialize(width,commands)
  end
  
  alias ozwcd_draw_item draw_item unless $@
  def draw_item(index, color)
    color = disabled_color if !check_req(index)
    ozwcd_draw_item(index, color)
  end
  
  def check_req(index)
    sw_id = @switch_req[index]
    return true if sw_id == 0
    v = $game_switches[sw_id]
    return v
  end
end