Enemies use items (XP)

One of my favourite scripts, this let’s enemies use items, I always wanted to update it to include a stealing system of sorts but I never did at the time anyway here’s the script:

class Game_Enemy < Game_Battler

  Item_Use_Skill = 1

  alias item_initialize initialize
  def initialize(troop_id, member_index)
    item_initialize(troop_id, member_index)
    @items = [1]

    if self.id == 2
      @items = [1, 2]
    end

  end

  alias item_make_action make_action
  def make_action
    item_make_action
    if Item_Use_Skill == self.current_action.skill_id
      self.current_action.kind = 2
      self.current_action.item_id = @items[rand(@items.size)]
      self.current_action.decide_random_target_for_enemy
    end
  end

end

class Scene_Battle

  def make_item_action_result
    # Get item
    @item = $data_items[@active_battler.current_action.item_id]
    # If unable to use due to items running out
    unless $game_party.item_can_use?(@item.id)
      # Shift to step 1
      @phase4_step = 1
      return
    end

    # MODIFIED SECTION IF YOU ARE MERGING THIS SCRIPT
    # ORIGINALLY
    ## If consumable
    #if @item.consumable
    #  # Decrease used item by 1
    #  $game_party.lose_item(@item.id, 1)
    #end

    if @item.consumable and @active_battler.is_a?(Game_Actor)
      $game_party.lose_item(@item.id, 1)
    end

    # END OF MODIFIED SECTION IF YOU ARE MERGING THIS SCRIPT

    # Display item name on help window
    @help_window.set_text(@item.name, 1)
    # Set animation ID
    @animation1_id = @item.animation1_id
    @animation2_id = @item.animation2_id
    # Set common event ID
    @common_event_id = @item.common_event_id
    # Decide on target
    index = @active_battler.current_action.target_index
    target = $game_party.smooth_target_actor(index)
    # Set targeted battlers
    set_target_battlers(@item.scope)
    # Apply item effect
    for target in @target_battlers
      target.item_effect(@item)
    end
  end

end

Leave a Reply