VXAce Help Needed: RPG Maker VX Ace Game Crashes with Custom Menu Scripts

Hey, I have a big problem with my scripts for RPG Maker VX Ace. I have 4 custom scripts for the item menu, status menu, equipment menu, and input handler.


With these scripts, my game sometimes hard crashes when opening or closing menus, and I can’t figure out why. I’ve tried fixing it on my own but couldn’t.


If someone can help, I’d be really grateful. I’m going to paste all the scripts below so anyone who knows how can take a look.


Thank you in advance!

Spoiler: Item Menu
Ruby:
#==============================================================================
# ** Scene_Item
#==============================================================================

class Scene_Item < Scene_ItemBase
  def start
    super
    @closing = false
    create_help_window
    create_category_window
    create_item_window
    create_info_window
    HotkeyManager.menu_opened if defined?(HotkeyManager)
  end

  def create_category_window
    @category_window = Window_ItemCategory.new
    @category_window.viewport = @viewport
    @category_window.help_window = @help_window
    @category_window.y = @help_window.height
    @category_window.set_handler(:ok, method(:on_category_ok))
    @category_window.set_handler(:cancel, method(:return_scene))
  end

  def create_item_window
    wy = @category_window.y + @category_window.height
    wh = Graphics.height - wy
    @item_window = Window_SkyrimItemList.new(0, wy, Graphics.width - 200, wh)
    @item_window.viewport = @viewport
    @item_window.help_window = @help_window
    @item_window.set_handler(:ok, method(:on_item_ok))
    @item_window.set_handler(:cancel, method(:on_item_cancel))
    @category_window.item_window = @item_window
  end

  def create_info_window
    wy = @category_window.y + @category_window.height
    wh = Graphics.height - wy
    @info_window = Window_ItemInfo.new(Graphics.width - 200, wy, 200, wh)
    @info_window.viewport = @viewport
    @item_window.info_window = @info_window if @item_window
  end

  def on_category_ok
    return if @closing
    @item_window.activate if @item_window
    @item_window.select_last if @item_window
  end

  def on_item_ok
    return if @closing
    item = @item_window.item if @item_window
    return unless item
    $game_party.last_item.object = item
    determine_item
  end

  def on_item_cancel
    return if @closing
    @item_window.unselect if @item_window
    @category_window.activate if @category_window
    @info_window.clear if @info_window
  end

  def use_item
    super
    @item_window.redraw_current_item if @item_window
  end

  def return_scene
    @closing = true
    super
  end

  def terminate
    super
    @item_window.dispose if @item_window && !@item_window.disposed?
    @category_window.dispose if @category_window && !@category_window.disposed?
    @info_window.dispose if @info_window && !@info_window.disposed?
    @item_window = nil
    @category_window = nil
    @info_window = nil
    @closing = false
    HotkeyManager.menu_closed if defined?(HotkeyManager)
  end
end

#==============================================================================
# ** Windows
#==============================================================================

class Window_ItemCategory < Window_HorzCommand
  def initialize; super(0, 0); end
  def window_width; Graphics.width; end
  def col_max; 5; end
  def make_command_list
    add_command("All", :all)
    add_command("Weapons", :weapon)
    add_command("Armor", :armor)
    add_command("Items", :item)
    add_command("Quest Items", :key_item)
  end
end

class Window_SkyrimItemList < Window_ItemList
  attr_accessor :info_window

  def initialize(x, y, width, height)
    super(x, y, width, height)
    @info_window = nil
  end

  def info_window=(info_window)
    @info_window = info_window
    update_info_window if @info_window && item
  end

  def include?(item)
    return false unless item
    case @category
    when :all then true
    when :item then item.is_a?(RPG::Item) && !item.key_item?
    when :weapon then item.is_a?(RPG::Weapon)
    when :armor then item.is_a?(RPG::Armor)
    when :key_item then item.is_a?(RPG::Item) && item.key_item?
    else false
    end
  end

  def draw_item(index)
    return unless @data && index >= 0 && index < @data.size
    item = @data[index]
    return unless item
    rect = item_rect(index)
    rect.width -= 4
    draw_icon(item.icon_index, rect.x, rect.y, enable?(item))
    draw_text(rect.x + 24, rect.y, rect.width - 24, line_height, item.name, 0)
  end

  def update_help
    @help_window.set_item(item) if @help_window
    update_info_window
  end

  def update_info_window
    return unless @info_window
    @info_window.item = item rescue nil
  end

  def refresh
    make_item_list
    create_contents
    draw_all_items
  end

  def redraw_current_item
    draw_item(index) if index && index >= 0 && index < item_max
  end
end

class Window_ItemInfo < Window_Base
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @item = nil
  end

  def item=(item)
    return if @item == item
    @item = item
    refresh
  end

  def clear
    @item = nil
    contents.clear if contents
  end

  def refresh
    return unless contents
    contents.clear
    return unless @item

    y = 0
    change_color(normal_color)
    draw_text(0, y, contents.width, line_height, @item.name, 1)
    y += line_height + 10

    change_color(system_color)
    draw_text(0, y, contents.width, line_height, get_item_type(@item), 1)
    y += line_height + 10

    if @item.is_a?(RPG::Weapon)
      draw_weapon_stats(y)
      y += line_height * 4
    elsif @item.is_a?(RPG::Armor)
      draw_armor_stats(y)
      y += line_height * 4
    end

    change_color(system_color)
    draw_text(0, y, contents.width, line_height, "Value: #{@item.price} Gold")
    y += line_height + 5

    if $game_party && $game_party.item_number(@item) > 1
      change_color(normal_color)
      draw_text(0, y, contents.width, line_height, "Quantity: #{$game_party.item_number(@item)}")
    end
  end

  def get_item_type(item)
    return "Unknown" unless item
    if item.is_a?(RPG::Weapon)
      $data_system.weapon_types[item.wtype_id] || "Weapon"
    elsif item.is_a?(RPG::Armor)
      $data_system.armor_types[item.atype_id] || "Armor"
    elsif item.is_a?(RPG::Item)
      item.key_item? ? "Quest Item" : "Item"
    else
      "Unknown"
    end
  end

  def draw_weapon_stats(y)
    return unless @item && @item.params
    change_color(power_up_color)
    draw_text(0, y, contents.width, line_height, "Damage: #{@item.params[2] || 0}")
    draw_text(0, y + line_height, contents.width, line_height, "Speed: #{@item.params[6] || 0}")
    draw_text(0, y + line_height * 2, contents.width, line_height, "Critical: #{@item.params[7] || 0}%")
  end

  def draw_armor_stats(y)
    return unless @item && @item.params
    change_color(power_up_color)
    draw_text(0, y, contents.width, line_height, "Armor: #{@item.params[3] || 0}")
    draw_text(0, y + line_height, contents.width, line_height, "Magic Resist: #{@item.params[5] || 0}")
  end
end

Spoiler: Status Menu
Ruby:
#==============================================================================
# ** Status Menu
#==============================================================================

# Add the missing exp_rate method to Game_Actor
class Game_Actor < Game_Battler
  def exp_rate
    return 1 if max_level?
    current_exp = @exp - current_level_exp
    next_exp = next_level_exp - current_level_exp
    return 0 if next_exp == 0
    current_exp.to_f / next_exp.to_f
  end
end

#==============================================================================
# ** Scene_Status
#==============================================================================

class Scene_Status < Scene_MenuBase
  def initialize(actor_index = 0)
    super()
    @actor_index = actor_index
    @actor = $game_party.all_members[@actor_index] rescue nil
    @current_tab = 0
  end

  def start
    super
    return SceneManager.return unless @actor
    create_status_window
    create_tab_window
    create_content_window
    update_content_window
  end

  def terminate
    super
    dispose_status_windows
    HotkeyManager.menu_closed if defined?(HotkeyManager)
  end

  def dispose_status_windows
    [@status_window, @tab_window, @stats_window, @skills_window, @skill_desc_window].each do |w|
      w.dispose if w && w.respond_to?(:dispose) && !w.disposed?
    end
  end

  #--------------------------------------------------------------------------
  # * Create Windows
  #--------------------------------------------------------------------------
  def create_status_window
    @status_window = Window_SkyrimStatus.new(0, 0, Graphics.width, 160)
    @status_window.actor = @actor
  end

  def create_tab_window
    @tab_window = Window_SkyrimTabs.new(0, 160, Graphics.width, 50)
    @tab_window.set_handler(:stats, method(:select_stats))
    @tab_window.set_handler(:skills, method(:select_skills))
    @tab_window.select(0)
    @tab_window.activate
  end

  def create_content_window
    @stats_window = Window_SkyrimStats.new(0, 210, Graphics.width, Graphics.height - 210)
    @stats_window.actor = @actor

    @skills_window = Window_SkyrimSkills.new(0, 210, Graphics.width / 2, Graphics.height - 210)
    @skills_window.actor = @actor
    @skills_window.set_handler(:ok, method(:on_skill_ok))
    @skills_window.set_handler(:cancel, method(:on_skill_cancel))
    @skills_window.help_window = @skill_desc_window
    @skills_window.hide
    @skills_window.deactivate

    @skill_desc_window = Window_SkyrimSkillDesc.new(Graphics.width / 2, 210, Graphics.width / 2, Graphics.height - 210)
    @skill_desc_window.hide
  end

  #--------------------------------------------------------------------------
  # * Tab logic
  #--------------------------------------------------------------------------
  def update_content_window
    case @current_tab
    when 0
      @stats_window.show
      @skills_window.hide
      @skills_window.deactivate
      @skill_desc_window.hide
      @tab_window.activate
    when 1
      @stats_window.hide
      @skills_window.show
      @skill_desc_window.show
      @skills_window.activate
      @skills_window.select(0) if @skills_window.item_max > 0
    end
  end

  def select_stats
    @current_tab = 0
    update_content_window
  end

  def select_skills
    @current_tab = 1
    update_content_window
  end

  def on_skill_ok
    skill = @skills_window.current_skill
    @skill_desc_window.skill = skill if skill
  end

  def on_skill_cancel
    @tab_window.activate
    @skills_window.deactivate
  end

  #--------------------------------------------------------------------------
  # * Update
  #--------------------------------------------------------------------------
  def update
    super
    # ESC closes menu
    if Input.trigger?(:B) || Input.trigger?(:X)
      Sound.play_cancel
      return_scene
      return
    end

    # Tab switching logic
    if @tab_window.active
      if Input.trigger?(:LEFT)
        Sound.play_cursor
        @current_tab = 0
        @tab_window.select(0)
        update_content_window
      elsif Input.trigger?(:RIGHT)
        Sound.play_cursor
        @current_tab = 1
        @tab_window.select(1)
        update_content_window
      end
    end
  end
end

#==============================================================================
# ** Windows
#==============================================================================

class Window_SkyrimTabs < Window_HorzCommand
  def initialize(x, y, width, height)
    super(x, y)
    self.width = width
    self.height = height
  end

  def window_width; Graphics.width; end
  def col_max; 2; end
  def make_command_list
    add_command("Attributes", :stats)
    add_command("Skills", :skills)
  end
end

class Window_SkyrimStatus < Window_Base
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @actor = nil
  end

  def actor=(actor)
    return if @actor == actor
    @actor = actor
    refresh
  end

  def refresh
    contents.clear
    return unless @actor

    draw_section(20, 30, "Level", @actor.level.to_s)
    draw_gauge_section(200, 60, "Health", @actor.hp, @actor.mhp, @actor.hp_rate, hp_gauge_color1, hp_gauge_color2)
    draw_gauge_section(200, 100, "Magicka", @actor.mp, @actor.mmp, @actor.mp_rate, mp_gauge_color1, mp_gauge_color2)

    # Experience
    if @actor.max_level?
      exp_text = "MAX"
      exp_rate = 1
    else
      current_exp = @actor.exp - @actor.current_level_exp
      next_exp = @actor.next_level_exp - @actor.current_level_exp
      exp_text = "#{current_exp}/#{next_exp}"
      exp_rate = next_exp > 0 ? current_exp.to_f / next_exp.to_f : 0
    end
    draw_gauge_section(480, 60, "Experience", nil, nil, exp_rate, skyrim_gold, skyrim_blue, exp_text)
  end

  def draw_section(x, y, label, value)
    change_color(system_color)
    draw_text(x, y, 80, line_height, label)
    change_color(normal_color)
    draw_text(x + 80, y, 50, line_height, value)
  end

  def draw_gauge_section(x, y, label, val, max_val, rate, color1, color2, text = nil)
    change_color(system_color)
    draw_text(x, y - 15, 80, line_height, label)
    draw_gauge(x, y, 180, rate, color1, color2)
    change_color(normal_color)
    draw_text(x + 185, y, 100, line_height, text || "#{val}/#{max_val}")
  end
end

class Window_SkyrimStats < Window_Base
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @actor = nil
  end

  def actor=(actor)
    return if @actor == actor
    @actor = actor
    refresh
  end

  def refresh
    contents.clear
    return unless @actor

    draw_column(50, 20, [
      ["Attack Power", @actor.atk],
      ["Defense", @actor.def],
      ["Magic Attack", @actor.mat],
      ["Magic Defense", @actor.mdf],
      ["Agility", @actor.agi],
      ["Luck", @actor.luk]
    ], "Primary Attributes")

    draw_column(Graphics.width / 2 + 50, 20, [
      ["Hit Rate", "#{(@actor.hit*100).to_i}%"],
      ["Evasion Rate", "#{(@actor.eva*100).to_i}%"],
      ["Critical Rate", "#{(@actor.cri*100).to_i}%"],
      ["HP Recovery", "#{(@actor.hrg*100).to_i}%"],
      ["MP Recovery", "#{(@actor.mrg*100).to_i}%"],
      ["TP Recovery", "#{(@actor.trg*100).to_i}%"]
    ], "Combat Statistics")

    draw_status_effects
  end

  def draw_column(x, y, stats, header)
    change_color(skyrim_gold)
    draw_text(x, y, 200, line_height, header, 1)
    y += line_height + 20
    stats.each do |name, value|
      change_color(system_color)
      draw_text(x, y, 140, line_height, name)
      change_color(skyrim_blue)
      draw_text(x + 140, y, 60, line_height, value.to_s, 2)
      y += line_height + 8
    end
  end

  def draw_status_effects
    y = 20 + 6*line_height + 50
    change_color(skyrim_gold)
    draw_text(0, y, contents.width, line_height, "Status Effects", 1)
    y += line_height + 15
    if @actor.states.empty?
      change_color(normal_color)
      draw_text(0, y, contents.width, line_height, "No active effects", 1)
    else
      col_width = contents.width / 3
      col = 0
      @actor.states.reject{|s| s.id == @actor.death_state_id}.each do |state|
        x_pos = (col % 3) * col_width + 20
        y_offset = (col / 3) * (line_height + 5)
        draw_icon(state.icon_index, x_pos, y + y_offset)
        change_color(normal_color)
        draw_text(x_pos + 30, y + y_offset, col_width - 50, line_height, state.name)
        col += 1
        break if y + y_offset >= contents.height - line_height*2
      end
    end
  end
end

class Window_SkyrimSkills < Window_Selectable
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @actor = nil
    @data = []
  end

  def actor=(actor)
    return if @actor == actor
    @actor = actor
    make_item_list
    refresh
    select(0) if item_max > 0
  end

  def item_max
    @data ? @data.size : 0
  end

  def current_skill
    @data && index >= 0 && index < @data.size ? @data[index] : nil
  end

  def make_item_list
    @data = @actor ? @actor.skills.select{|s| s && s.is_a?(RPG::Skill) && !s.name.empty?} : []
    @data.sort_by!(&:name) rescue @data
  end

  def refresh
    make_item_list
    create_contents
    draw_all_items
  end

  def draw_item(index)
    return unless @data && @data[index]
    skill = @data[index]
    rect = item_rect(index)
    rect.width -= 4
    enabled = @actor && @actor.usable?(skill)
    draw_icon(skill.icon_index, rect.x, rect.y, enabled)
    change_color(normal_color, enabled)
    draw_text(rect.x+24, rect.y, rect.width-84, line_height, skill.name)
    if skill.mp_cost > 0
      change_color(mp_cost_color, enabled)
      draw_text(rect.x+rect.width-60, rect.y, 60, line_height, "#{skill.mp_cost}MP", 2)
    elsif skill.tp_cost > 0
      change_color(tp_cost_color, enabled)
      draw_text(rect.x+rect.width-60, rect.y, 60, line_height, "#{skill.tp_cost}TP", 2)
    end
  end

  def update_help
    @help_window.set_item(current_skill) if @help_window
  end
end

class Window_SkyrimSkillDesc < Window_Base
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @skill = nil
  end

  def skill=(skill)
    return if @skill == skill
    @skill = skill
    refresh
  end

  def refresh
    contents.clear
    return unless @skill && @skill.is_a?(RPG::Skill)
    y = 10
    change_color(skyrim_gold)
    draw_text(10, y, contents.width-20, line_height, @skill.name)
    y += line_height + 10
    change_color(skyrim_blue)
    stype = case @skill.stype_id
      when 1 then "Magic Skill"
      when 2 then "Special Skill"
      else "Skill"
    end
    draw_text(10, y, contents.width-20, line_height, stype)
    y += line_height + 15
    if @skill.mp_cost > 0
      change_color(system_color)
      draw_text(10, y, 100, line_height, "MP Cost:")
      change_color(mp_cost_color)
      draw_text(110, y, 50, line_height, @skill.mp_cost.to_s)
      y += line_height + 5
    end
    if @skill.tp_cost > 0
      change_color(system_color)
      draw_text(10, y, 100, line_height, "TP Cost:")
      change_color(tp_cost_color)
      draw_text(110, y, 50, line_height, @skill.tp_cost.to_s)
      y += line_height + 5
    end
    y += 10
    change_color(skyrim_gold)
    draw_text(10, y, contents.width-20, line_height, "Description:")
    y += line_height + 5
    change_color(normal_color)
    desc = @skill.description.empty? ? "No description available." : @skill.description
    draw_text_ex(10, y, desc)
  end

  def draw_text_ex(x, y, text)
    text = convert_escape_characters(text)
    pos = {x:x, y:y, new_x:x, height:calc_line_height(text)}
    process_character(text.slice!(0,1), text, pos) until text.empty?
  end
end

#==============================================================================
# ** Window_Base color enhancements
#==============================================================================

class Window_Base
  def skyrim_blue; Color.new(100,149,237); end
  def skyrim_gold; Color.new(255,215,0); end
  def skyrim_red; Color.new(220,20,60); end
  def skyrim_green; Color.new(34,139,34); end
  def skyrim_purple; Color.new(138,43,226); end

  alias_method :original_text_color, :text_color
  def text_color(n)
    case n
    when 30 then skyrim_blue
    when 31 then skyrim_gold
    when 32 then skyrim_red
    when 33 then skyrim_green
    when 34 then skyrim_purple
    else original_text_color(n)
    end
  end

  def convert_escape_characters(text)
    result = text.to_s.clone
    result.gsub!(/\\/) { "\\" }
    result.gsub!(/\e\e/) { "\\" }
    result.gsub!(/\eC\[(\d+)\]/) { "\eC[#{$1}]" }
    result.gsub!(/\eSB/) { "\eC[30]" }
    result.gsub!(/\eSG/) { "\eC[31]" }
    result.gsub!(/\eSR/) { "\eC[32]" }
    result.gsub!(/\eSGR/) { "\eC[33]" }
    result.gsub!(/\eSP/) { "\eC[34]" }
    result
  end
end

Spoiler: Equipment Menu
Ruby:
#==============================================================================
# ** Equipment Menu 
#==============================================================================

#==============================================================================
# ** Window_EquipStatus_New
#==============================================================================
class Window_EquipStatus_New < Window_Base
  def initialize(x, y)
    super(x, y, window_width, window_height)
    self.opacity = 240
    self.back_opacity = 220
    @actor = nil
    @temp_actor = nil
    refresh
  end

  def window_width; 340; end
  def window_height; Graphics.height; end

  def actor=(actor)
    return if @actor == actor
    @actor = actor
    refresh
  end

  def set_temp_actor(temp_actor)
    @temp_actor = temp_actor
    refresh
  end

  def refresh
    contents.clear
    return unless @actor

    draw_gradient_rect(0, 0, contents.width, contents.height, Color.new(0, 0, 50, 30), Color.new(0, 0, 0, 10))

    # Draw actor face with border
    contents.fill_rect(15, 15, 96, 96, Color.new(255, 255, 255, 60))
    contents.fill_rect(16, 16, 94, 94, Color.new(0, 0, 0, 0))
    draw_actor_face(@actor, 15, 15, 96)

    # Name
    change_color(Color.new(255, 215, 0))
    contents.font.size = 24
    draw_text(0, 130, contents.width, 30, @actor.name, 1)
    contents.font.size = Font.default_size

    # Level & Class
    change_color(Color.new(200, 200, 255))
    draw_text(0, 165, contents.width, line_height, "Level #{@actor.level} #{@actor.class.name}", 1)

    draw_enhanced_gauge(15, 200, contents.width - 30, @actor.hp, @actor.mhp, hp_gauge_color1, hp_gauge_color2, "HP")
    draw_enhanced_gauge(15, 230, contents.width - 30, @actor.mp, @actor.mmp, mp_gauge_color1, mp_gauge_color2, "MP")

    draw_parameters_enhanced(15, 280)
  end

  def draw_gradient_rect(x, y, width, height, color1, color2)
    for i in 0...height
      rate = i.to_f / height
      color = Color.new(
        color1.red + (color2.red - color1.red) * rate,
        color1.green + (color2.green - color1.green) * rate,
        color1.blue + (color2.blue - color1.blue) * rate,
        color1.alpha + (color2.alpha - color1.alpha) * rate
      )
      contents.fill_rect(x, y + i, width, 1, color)
    end
  end

  def draw_enhanced_gauge(x, y, width, value, max_value, color1, color2, label)
    contents.fill_rect(x, y, width, 20, Color.new(0, 0, 0, 120))
    contents.fill_rect(x + 1, y + 1, width - 2, 18, Color.new(50, 50, 50, 180))

    rate = max_value > 0 ? value.to_f / max_value : 0
    fill_width = ((width - 4) * rate).to_i

    if fill_width > 0
      for i in 0...fill_width
        color_rate = i.to_f / (width - 4)
        color = Color.new(
          color1.red + (color2.red - color1.red) * color_rate,
          color1.green + (color2.green - color1.green) * color_rate,
          color1.blue + (color2.blue - color1.blue) * color_rate,
          255
        )
        contents.fill_rect(x + 2 + i, y + 2, 1, 16, color)
      end
    end

    change_color(Color.new(255, 255, 255))
    contents.font.size = 18
    draw_text(x + 5, y - 2, 60, 24, label)
    draw_text(x + width - 80, y - 2, 75, 24, "#{value}/#{max_value}", 2)
    contents.font.size = Font.default_size
  end

  def draw_parameters_enhanced(x, y)
    6.times do |i|
      param_id = i + 2
      rect_y = y + i * 32

      contents.fill_rect(x, rect_y, contents.width - 30, 28, Color.new(0, 0, 0, 40))
      contents.fill_rect(x + 1, rect_y + 1, contents.width - 32, 26, Color.new(100, 100, 150, 20))

      change_color(Color.new(200, 200, 255))
      contents.font.size = 18
      draw_text(x + 8, rect_y + 2, 120, 24, Vocab::param(param_id))

      current_value = @actor.param(param_id)
      temp_value = @temp_actor ? @temp_actor.param(param_id) : current_value

      change_color(
        temp_value > current_value ? Color.new(100, 255, 100) :
        temp_value < current_value ? Color.new(255, 100, 100) :
        Color.new(255, 255, 255)
      )

      contents.font.size = 20
      draw_text(x + 140, rect_y + 1, 60, 26, temp_value, 2)

      if @temp_actor && temp_value != current_value
        diff = temp_value - current_value
        diff_text = diff > 0 ? "(+#{diff})" : "(#{diff})"
        contents.font.size = 16
        draw_text(x + 205, rect_y + 4, 80, 20, diff_text)
      end

      contents.font.size = Font.default_size
    end
  end
end

#==============================================================================
# ** Window_EquipCommand_New
#==============================================================================
class Window_EquipCommand_New < Window_HorzCommand
  attr_accessor :actor

  def initialize(x, y)
    @actor = nil
    super(x, y)
    self.opacity = 240
    self.back_opacity = 220
  end

  def window_width; Graphics.width - 340; end
  def col_max; 3; end
  def spacing; 8; end

  def make_command_list
    add_command("Equip", :equip)
    add_command("Auto", :optimize)
    add_command("Clear", :clear)
  end

  def draw_item(index)
    rect = item_rect_for_text(index)

    if index == self.index
      contents.fill_rect(rect.x - 4, rect.y - 2, rect.width + 8, rect.height + 4, Color.new(100, 150, 255, 100))
      contents.fill_rect(rect.x - 3, rect.y - 1, rect.width + 6, rect.height + 2, Color.new(150, 200, 255, 60))
    else
      contents.fill_rect(rect.x - 4, rect.y - 2, rect.width + 8, rect.height + 4, Color.new(50, 50, 50, 80))
      contents.fill_rect(rect.x - 3, rect.y - 1, rect.width + 6, rect.height + 2, Color.new(100, 100, 100, 40))
    end

    change_color(normal_color, command_enabled?(index))
    contents.font.size = 20
    draw_text(rect, command_name(index), 1)
    contents.font.size = Font.default_size
  end

  def item_rect_for_text(index)
    rect = item_rect(index)
    rect.x += 4
    rect.width -= 8
    rect
  end
end

#==============================================================================
# ** Window_EquipSlot_New
#==============================================================================
class Window_EquipSlot_New < Window_Selectable
  attr_accessor :status_window, :item_window

  def initialize(x, y, width)
    @actor = nil
    super(x, y, width, window_height)
    self.opacity = 240
    self.back_opacity = 220
    refresh
  end

  def window_height; fitting_height(visible_line_number); end
  def visible_line_number; 6; end

  def actor=(actor)
    return if @actor == actor
    @actor = actor
    refresh
    self.index = 0 if @actor
  end

  def item_max; @actor ? @actor.equip_slots.size : 0; end
  def item; @actor ? @actor.equips[index] : nil; end

  def draw_item(index)
    return unless @actor
    return if index >= @actor.equip_slots.size
    rect = item_rect_for_text(index)

    contents.fill_rect(rect.x - 8, rect.y - 2, rect.width + 16, rect.height + 4, Color.new(0, 0, 0, 30))
    contents.fill_rect(rect.x - 8, rect.y - 2, rect.width + 16, rect.height + 4, Color.new(100, 150, 255, 60)) if index == self.index

    change_color(Color.new(200, 200, 255), enable?(index))
    contents.font.size = 18
    name = $data_system.terms.etypes[@actor.equip_slots[index]]
    draw_text(rect.x, rect.y + 2, 120, rect.height - 4, name)

    item = @actor.equips[index]
    if item
      draw_icon(item.icon_index, rect.x + 125, rect.y + 1)
      change_color(normal_color, enable?(index))
      contents.font.size = 16
      draw_text(rect.x + 155, rect.y + 4, rect.width - 155, rect.height - 8, item.name)
    else
      change_color(Color.new(150, 150, 150))
      contents.font.size = 16
      draw_text(rect.x + 125, rect.y + 4, rect.width - 125, rect.height - 8, "- None -")
    end

    contents.font.size = Font.default_size
  end

  def enable?(index)
    @actor ? @actor.equip_change_ok?(index) : false
  end

  def refresh
    contents.clear
    draw_all_items
  end

  def update_help
    @help_window.set_item(item) if @help_window
    @status_window.set_temp_actor(nil) if @status_window
  end
end

#==============================================================================
# ** Window_EquipItem_New
#==============================================================================
class Window_EquipItem_New < Window_ItemList
  attr_accessor :status_window, :slot_window

  def initialize(x, y, width, height)
    @actor = nil
    @slot_id = 0
    super(x, y, width, height)
    self.opacity = 240
    self.back_opacity = 220
    @category = :weapon
  end

  def actor=(actor)
    return if @actor == actor
    @actor = actor
    refresh
  end

  def slot_id=(slot_id)
    return if @slot_id == slot_id
    @slot_id = slot_id
    @category = (@actor.equip_slots[@slot_id] == 0 ? :weapon : :armor) if @actor
    refresh
    self.index = 0
  end

  def include?(item)
    return false unless item
    return false if !@actor || @slot_id.nil? || @slot_id < 0
    return false unless @actor.equippable?(item)
    item.etype_id == @actor.equip_slots[@slot_id]
  end

  def enable?(item)
    true
  end

  def draw_item(index)
    return if index >= @data.size
    item = @data[index]
    return unless item

    rect = item_rect(index)
    contents.fill_rect(rect.x, rect.y, rect.width, rect.height, Color.new(100, 150, 255, 40)) if index == self.index
    draw_icon(item.icon_index, rect.x + 4, rect.y + 1)
    change_color(normal_color, enable?(item))
    contents.font.size = 16
    draw_text(rect.x + 28, rect.y + 4, rect.width - 32, rect.height - 8, item.name)
    contents.font.size = Font.default_size
  end

  def update_help
    @help_window.set_item(item) if @help_window

    if @actor && @status_window && @slot_id >= 0
      temp_actor = Marshal.load(Marshal.dump(@actor))
      temp_actor.force_change_equip(@slot_id, item) if item
      @status_window.set_temp_actor(temp_actor)
    end
  end

  def refresh
    begin
      make_item_list
      create_contents
      draw_all_items
    rescue => e
      File.open("equip_menu_crash_log.txt", "a") do |f|
        f.puts "Error: #{e.message}"
        f.puts e.backtrace.join("\n")
        f.puts "---"
      end
    end
  end
end

#==============================================================================
# ** Scene_Equip
#==============================================================================
class Scene_Equip < Scene_MenuBase
  def start
    super
    @actor = $game_party.menu_actor
    create_background
    create_help_window
    create_status_window
    create_command_window
    create_slot_window
    create_item_window
  end

  def create_background
    @background_sprite = Sprite.new
    @background_sprite.bitmap = SceneManager.background_bitmap
    @background_sprite.color.set(16, 16, 16, 128)
  end

  def create_help_window
    @help_window = Window_Help.new
    @help_window.opacity = 240
    @help_window.back_opacity = 220
  end

  def create_status_window
    @status_window = Window_EquipStatus_New.new(0, 0)
    @status_window.actor = @actor
  end

  def create_command_window
    wx = @status_window.width
    @command_window = Window_EquipCommand_New.new(wx, 0)
    @command_window.help_window = @help_window
    @command_window.set_handler(:equip, method(:command_equip))
    @command_window.set_handler(:optimize, method(:command_optimize))
    @command_window.set_handler(:clear, method(:command_clear))
    @command_window.set_handler(:cancel, method(:return_scene))
    @command_window.actor = @actor
  end

  def create_slot_window
    wx = @status_window.width
    wy = @command_window.height
    ww = Graphics.width - @status_window.width
    @slot_window = Window_EquipSlot_New.new(wx, wy, ww)
    @slot_window.help_window = @help_window
    @slot_window.status_window = @status_window
    @slot_window.actor = @actor
    @slot_window.set_handler(:ok, method(:on_slot_ok))
    @slot_window.set_handler(:cancel, method(:on_slot_cancel))
  end

  def create_item_window
    wx = @status_window.width
    wy = @slot_window.y + @slot_window.height
    ww = Graphics.width - @status_window.width
    wh = Graphics.height - wy
    @item_window = Window_EquipItem_New.new(wx, wy, ww, wh)
    @item_window.help_window = @help_window
    @item_window.status_window = @status_window
    @item_window.slot_window = @slot_window
    @item_window.actor = @actor
    @item_window.set_handler(:ok, method(:on_item_ok))
    @item_window.set_handler(:cancel, method(:on_item_cancel))
    @slot_window.item_window = @item_window
  end

  def terminate
    super
    @background_sprite.dispose if @background_sprite
  end

  def command_equip
    @slot_window.activate
    @slot_window.select(0)
  end

  def command_optimize
    Sound.play_equip
    @actor.optimize_equipments
    refresh_all
    @command_window.activate
  end

  def command_clear
    Sound.play_equip
    @actor.equip_slots.each_with_index do |_, i|
      @actor.change_equip(i, nil) if @actor.equip_change_ok?(i)
    end
    refresh_all
    @command_window.activate
  end

  def on_slot_ok
    @item_window.slot_id = @slot_window.index
    @item_window.activate
    @item_window.select(0)
  end

  def on_slot_cancel
    @slot_window.unselect
    @command_window.activate
  end

  def on_item_ok
    Sound.play_equip
    @actor.change_equip(@slot_window.index, @item_window.item)
    refresh_all
    @item_window.unselect
    @slot_window.activate
  end

  def on_item_cancel
    @item_window.unselect
    @slot_window.activate
  end

  def refresh_all
    @status_window.refresh
    @slot_window.refresh
    @item_window.refresh
  end

  def on_actor_change
    @status_window.actor = @actor
    @command_window.actor = @actor
    @slot_window.actor = @actor
    @item_window.actor = @actor
    refresh_all
  end
end

Spoiler: Hotkey manager
Ruby:
#==============================================================================
# ** Key Input Extender
# Toggle menus with keys:
# I - Inventory
# P - Status 
# U - Equipment
# O - Quests
#
#==============================================================================

module Key
  # Key codes for I, P, U, O respectively
  I_KEY = 0x49
  P_KEY = 0x50
  U_KEY = 0x55
  O_KEY = 0x4F # 'O' key

  # Menu state tracking for crash prevention
  @menu_open = false
  @last_menu_time = 0
  @menu_close_time = 0
  @debounce_time = 5 # frames to prevent rapid crashes
  @reopen_cooldown = 15 # frames to prevent ghost reopen after closing

  begin
    GetKeyState = Win32API.new("user32", "GetAsyncKeyState", ['i'], 'i')
  rescue
    GetKeyState = nil
    puts "Warning: Win32API not available - keyboard shortcuts disabled"
  end

  def self.trigger?(key_code)
    return false unless GetKeyState
    begin
      (GetKeyState.call(key_code) & 0x01) == 1
    rescue
      false
    end
  end

  def self.menu_open?
    @menu_open
  end

  def self.set_menu_open(state)
    @menu_open = state
    if state == false
      @menu_close_time = Graphics.frame_count
    end
  end

  def self.can_open_menu?
    current_time = Graphics.frame_count
   
    # Check if we just closed a menu (prevent ghost reopen)
    return false if current_time - @menu_close_time < @reopen_cooldown
   
    # Check normal debounce
    return false if current_time - @last_menu_time < @debounce_time
   
    return false if $game_system && $game_message.busy?
    return false if SceneManager.scene_is?(Scene_Gameover)
    return false if SceneManager.scene_is?(Scene_Title)
    true
  end

  def self.set_menu_time
    @last_menu_time = Graphics.frame_count
  end

  def self.safe_dispose
    begin
      # Ensure proper disposal of sprites and windows
      if SceneManager.scene.respond_to?(:dispose_all_windows)
        SceneManager.scene.dispose_all_windows
      end
     
      # Force garbage collection if needed
      GC.start if Graphics.frame_count % 300 == 0
    rescue => e
      puts "Warning during disposal: #{e.message}"
    end
  end
end

class Scene_Map < Scene_Base
  alias toggle_key_update update unless method_defined?(:toggle_key_update)
 
  def update
    toggle_key_update
    check_toggle_keys if SceneManager.scene_is?(Scene_Map) && Key.can_open_menu?
  end

  def check_toggle_keys
    return unless $game_system && !$game_message.busy?
   
    if Key.trigger?(Key::I_KEY)
      open_inventory
    elsif Key.trigger?(Key::P_KEY)
      open_status
    elsif Key.trigger?(Key::U_KEY)
      open_equipment
    elsif Key.trigger?(Key::O_KEY) && defined?(CSCA_Scene_Quest)
      open_quest
    end
  end

  def open_inventory
    Key.set_menu_open(true)
    Key.set_menu_time
    Sound.play_ok
    SceneManager.call(Scene_Item)
  end

  def open_status
    Key.set_menu_open(true)
    Key.set_menu_time
    Sound.play_ok
    SceneManager.call(Scene_Status)
  end

  def open_equipment
    Key.set_menu_open(true)
    Key.set_menu_time
    Sound.play_ok
    SceneManager.call(Scene_Equip)
  end

  def open_quest
    Key.set_menu_open(true)
    Key.set_menu_time
    Sound.play_ok
    SceneManager.call(CSCA_Scene_Quest) if defined?(CSCA_Scene_Quest)
  end
end

# Enhanced Scene_Base for proper disposal
class Scene_Base
  alias key_extender_terminate terminate unless method_defined?(:key_extender_terminate)
 
  def terminate
    Key.set_menu_open(false) if Key.menu_open?
    Key.safe_dispose
    key_extender_terminate
  end
end

# Menu scenes - REMOVED ALL KEY HANDLING
# This lets the game's built-in Input module handle everything naturally
# The menu buttons I,P,U,O will no longer toggle/close - they do nothing
# ESC works normally (calls Scene_Menu or closes via game's default behavior)

menu_scenes = [Scene_Item]
menu_scenes << CSCA_Scene_Quest if defined?(CSCA_Scene_Quest)

menu_scenes.each do |scene_class|
  next unless scene_class
 
  scene_class.class_eval do
    unless method_defined?(:key_extender_update)
      alias key_extender_update update
     
      def update
        key_extender_update
        # NO CUSTOM KEY HANDLING - let game handle ESC naturally
      end

      alias key_extender_terminate terminate unless method_defined?(:key_extender_terminate)
     
      def terminate
        Key.set_menu_open(false)
        Key.safe_dispose
        key_extender_terminate
      end
    end
  end
end

# Enhanced Status scene - NO CUSTOM KEY HANDLING
class Scene_Status < Scene_MenuBase
  unless method_defined?(:key_extender_update)
    alias key_extender_update update
   
    def update
      key_extender_update
      # NO CUSTOM KEY HANDLING - let game handle ESC naturally
    end

    alias key_extender_terminate terminate unless method_defined?(:key_extender_terminate)
   
    def terminate
      Key.set_menu_open(false)
      Key.safe_dispose
      key_extender_terminate
    end
  end
end

# Enhanced Equipment scene - NO CUSTOM KEY HANDLING
class Scene_Equip < Scene_MenuBase
  unless method_defined?(:key_extender_update)
    alias key_extender_update update
   
    def update
      key_extender_update
      # NO CUSTOM KEY HANDLING - let game handle ESC naturally
    end

    alias key_extender_terminate terminate unless method_defined?(:key_extender_terminate)
   
    def terminate
      Key.set_menu_open(false)
      Key.safe_dispose
      key_extender_terminate
    end
  end
end

# Scene Manager enhancement for better menu tracking
class << SceneManager
  alias key_extender_return return unless method_defined?(:key_extender_return)
 
  def return
    Key.set_menu_open(false)  # This sets the close time and prevents ghost reopen
    key_extender_return
  end
end
Hello,

Can you put your scripts into the script environment? (that way the text is not messed up and it will be easier to help you)
You can do this by clicking on the code option on the right of the text options:
1762507910721.png

And select Ruby environment for coloring.
1762507976084.png


With these scripts, my game sometimes hard crashes when opening or closing menus, and I can’t figure out why. I’ve tried fixing it on my own but couldn’t.
Do you get an error? If so, can you display it?

By the way, your scripts don't have any author listed, did you write them? Otherwise, do you know where you got them? EDIT: I did not read properly the first sentence
Hello,

Can you put your scripts into the script environment? (that way the text is not messed up and it will be easier to help you)
You can do this by clicking on the code option on the right of the text options:
View attachment 352664
And select Ruby environment for coloring.
View attachment 352665


Do you get an error? If so, can you display it?

By the way, your scripts don't have any author listed, did you write them? Otherwise, do you know where you got them? EDIT: I did not read properly the first sentence
I updated the post with the code blocks.
Okay, so I tried the scripts, I don't get error messages, which is a good news but maybe my playtest did not test every cases.
Do you get an error? If so, can you display it?
It is very important if you can answer this question: if you get error messages, then there is a place in your code that does something wrong and a backtrace script might be helpful.
Otherwise, if you just get random crashes without messages, then this is likely a memory issue: this happen if you don't dispose properly of your sprites or windows when leaving scenes.

I ran your code with a graphical object reference script that I heavily suggest you use to debug your scripts, I got extra undisposed sprites everytime I close your customized scenes (items, equipments, status)
1762516071228.png


It seems that if I disable your hotkey manager script, then only the equipment scene will leave extra graphical objects.
So I guess you should check that you properly dispose of extra sprites or windows in your equipment menu and check what you've modified in your Hotkey script to not properly dispose of things from the other two scenes.
Okay, so I tried the scripts, I don't get error messages, which is a good news but maybe my playtest did not test every cases.

It is very important if you can answer this question: if you get error messages, then there is a place in your code that does something wrong and a backtrace script might be helpful.
Otherwise, if you just get random crashes without messages, then this is likely a memory issue: this happen if you don't dispose properly of your sprites or windows when leaving scenes.

I ran your code with a graphical object reference script that I heavily suggest you use to debug your scripts, I got extra undisposed sprites everytime I close your customized scenes (items, equipments, status)
View attachment 352666

It seems that if I disable your hotkey manager script, then only the equipment scene will leave extra graphical objects.
So I guess you should check that you properly dispose of extra sprites or windows in your equipment menu and check what you've modified in your Hotkey script to not properly dispose of things from the other two scenes.
I don’t get any error messages, just random crashes. I’ll try to fix it, thanks for the help!
I don’t get any error messages, just random crashes. I’ll try to fix it, thanks for the help!
So not getting error messages is because there is no error, but the code is incomplete because you are not disposing the graphics for the scene before disposing the windows (likely)
So your 'random crashes' is because of memory leaks and the game will just stop working because it can't handle the instability you create.

For the equip script, just looking at the code, the obvious thing that stands out is you dispose the graphic after disposing the scene.
Switch that around and add in a dispose for the bitmap. That actually might be the issue.
Your terminate should be this
Ruby:
  def terminate
    @background_sprite.bitmap.dispose if @background_sprite
    @background_sprite.dispose if @background_sprite
    super
  end
It looks like this is a duplication of the default code in SceneMenu.
remove it and the error goes away, unless you wanted to use another graphic

But reviewing all the code, it looks like you used AI the create this. That's probably why it is so bad.
You have in the status and item script a command to perform a function that doesn't exist anywhere and to do that command if another function exists (which it doesn't)

Original thread: https://forums.rpgmakerweb.com/threads/help-needed-rpg-maker-vx-ace-game-crashes-with-custom-menu-scripts.180908/