Battle Item Count VX

Battle Item Count VX

by Kyonides
​

Introduction

Do you need to update the number of potions or elixirs currently available for the next couple of actors during battle?
Or were you in need of canceling your previous actor's action to change the item the actor is going to consume next?
Then this scriptlet is for you! 😀

Ruby:
# * Battle Item Count VX * #
#   Plug & Play Script
#   Scripter : Kyonides Arkanthes
#   v1.0.4 - 2024-02-03

# The scriptlet updates the number of available items every single time you pick
# one or go back to the prior actor in battle.

class Game_Actor
  def clear_battle_item
    @battle_item_id = nil
  end
  attr_accessor :battle_item_id
end

class Game_Party
  def clear_battle_items
    members.each {|m| m.clear_battle_item }
  end

  def members_battle_items
    members.map {|m| m.battle_item_id }
  end

  def battle_item_number(item)
    return item_number(item) unless $game_temp.in_battle
    item_id = item.id
    battle_items = members_battle_items.select {|bit_id| item_id == bit_id }
    item_number(item) - battle_items.size
  end
end

class Window_Item
  alias :kyon_btl_itm_cnt_win_ref :refresh
  def refresh
    @battle_items = {}
    @battle_items.default = 99
    kyon_btl_itm_cnt_win_ref
  end

  def enable?(item)
    $game_party.item_can_use?(item) and @battle_items[item.id] > 0
  end

  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    item = @data[index]
    return unless item
    number = $game_party.battle_item_number(item)
    @battle_items[item.id] = number
    enabled = enable?(item)
    rect.width -= 4
    draw_item_name(item, rect.x, rect.y, enabled)
    self.contents.draw_text(rect, sprintf(":%2d", number), 2)
  end

  def battle_item_none?
    @battle_items[@data[@index].id] == 0
  end
end

class Scene_Battle
  alias :kyon_btl_itm_cnt_scn_btl_term :terminate
  alias :kyon_btl_itm_cnt_scn_btl_st_pty_cmd_sel :start_party_command_selection
  alias :kyon_btl_itm_cnt_scn_btl_prr_act :prior_actor
  alias :kyon_btl_itm_cnt_scn_btl_end_trgt_act_sel :end_target_actor_selection
  def terminate
    kyon_btl_itm_cnt_scn_btl_term
    $game_party.clear_battle_items
  end

  def start_party_command_selection
    $game_party.clear_battle_items
    kyon_btl_itm_cnt_scn_btl_st_pty_cmd_sel
  end

  def turn_decrease_item_count
    @active_battler.clear_battle_item if @active_battler
  end

  def prior_actor
    turn_decrease_item_count
    kyon_btl_itm_cnt_scn_btl_prr_act
    turn_decrease_item_count
  end

  def update_target_actor_selection
    @target_actor_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      end_target_actor_selection
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      @active_battler.action.target_index = @target_actor_window.index
      @active_battler.battle_item_id = @item.id if @item
      @item = nil
      end_target_actor_selection
      end_skill_selection
      end_item_selection
      next_actor
    end
  end

  def update_item_selection
    @item_window.active = true
    @item_window.update
    @help_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      end_item_selection
    elsif Input.trigger?(Input::C)
      @item = @item_window.item
      $game_party.last_item_id = @item.id if @item
      return Sound.play_buzzer unless $game_party.item_can_use?(@item)
      return Sound.play_buzzer if @item_window.battle_item_none?
      Sound.play_decision
      determine_item
    end
  end
end

Terms & Conditions

Free for use in ANY game.
Due credit is mandatory.
That's it! 😛
Bug Fix Available!​

After receiving a weird report concerning the item selection process, I started working on the issue described there and found out that I just needed to move the cancellation of the item counter to another method. Now it works as first intended. 😉
Second Bug Fix Available!​

I had to revisit my code a second time and I noticed that I might have skipped an actor when clearing the item count. Who could it be? 😅 Well, it'd be the last actor selected by the player for sure.

Now it should only trigger the buzzer SE when you truly picked the last available item in your party's bag. 😉
Minor Bug Fix Has Arrived!​

I had not noticed before how heroes could easily select any given item even after the item counter had hit the 0 limit. Version 1.0.4 gets rid of this exploitable bug for good. 😉

Original thread: https://forums.rpgmakerweb.com/threads/battle-item-count-vx.163036/