Scripting Help: Disable Choice Script

Hello guys. I’m trying to make a disable choice script for RPGMaker XP.

Himeswork had one going for VXAce but it’s down, and it utilizes RGSS3, not RGSS1 (the links that are currently up have no explanation of what the code does, all is lost…)

I’ve gotten this far with the help of Google Search AI, but the code it helped me organize is not working as intended.

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

# ■ Case-by-Case Choice Disabler for RPG Maker XP (RGSS1)

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

class Window_Command < Window_Selectable

  

  #--- Overwrite Initialization to clean up text tags on the fly ---

  alias xp_tag_choice_initialize initialize

  def initialize(width, commands)

    @raw_commands = commands.clone # Save original text containing tags

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

    xp_tag_choice_initialize(width, commands)

  end

  

  #--- Draw the item, changing color if its tagged switch is 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

  

  #--- Accessor to let the selection process see the raw tags ---

  def raw_commands

    return @raw_commands

  end

end




#--- Block the input if the player tries to select a disabled option ---

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


Something’s terribly wrong with this. Whenever it runs through, not only does it not get rid of the switch text (\s[1]), it also does not change the color. I suspect it’s failing at either .clone or at .map

Ok first things first, at this part…

    @raw_commands = commands.clone # Save original text containing tags
    clean_commands = commands.map { |text| text.gsub(/\\[Ss]\[\d+\]/, "") }
    xp_tag_choice_initialize(width, commands)

You define “raw commands”, that’s fine. Then recreate a new list of commands with commands.map called clean_commands, but THEN still pass the original commands as parameter in that third line. This should be why they’re not drawn as they should. I believe this should do the trick, just by changing this line

    # <---pass clean_commands instead of commands
    xp_tag_choice_initialize(width, clean_commands)

Then for drawing, I believe this part should be using @commands instead of @raw_commands

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

The last part is just not gonna work. RMXP manages windows differently. In VXAce the window itself has the logic for “adding callbacks”, so the action is called from within the window, you can then ask the window itself to not run the code or to disable an option. In RMXP, it’s usually the scene who dictates what happens when you press the selection button. So even if you make the selectable window to return after pressing C under certain conditions, it’s just going to sound the buzzer sound then still do whatever it’s supposed to be “disabled”. :\

Where is this supposed to appear?

EDIT: Kinda rewrote the first part of the code a bit. Feel free to use or however you want. This still lacks where it should “skip” running the action if you try to select a grayed out option.

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