Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

HBGames

The script you haven't been waiting for!
Code:
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
I am willing to add to this script when I have time, so I will accept requests to do with improving it.

For the time being you specify a skill (by it's id: Item_Use_Skill = 1) which when an enemy goes to do will instead use an item defautly a potion but I added some code to show how to make a specific enemy use a differant item, so you just need to make an enemy use the skill you pick to make it use an item.
I hope that was understandable.

Edit: Version by DerVVulfman (don't forget to credit him)
Code:
#==============================================================================
# ** Enemies Use Items
#------------------------------------------------------------------------------
#    by Fomar0153
#    version 2
#    04-27-2007
#    Full SDK 2.2 Compatible (Does not need or require SDK 2.2)
#------------------------------------------------------------------------------
#
# INTRODUCTION:
#
# This system allows enemy battlers to use items during battle.  This is accom-
# plished by switching the skill used by an enemy to that of an item in a pre-
# defined list in the configuration section.
#
# By example, you could set up a Skeleton enemy to use a 'Tonic' in battle when
# it performs its 'Sleep' skill.   This 'skill-to-item' change would have to be
# defined in either the SWITCH_SKILL_ITEM hash,  or the SWITCH_ENEMY_SKILL_ITEM 
# hash (explained in the configuration section below).
#
# The enemies can either have infinite item usage, or can be limited to the no.
# of items used based on the SP points at their disposal.  This feature is con-
# trolled by the SWITCH_SP_LIMIT value. As long as it is false, enemies can use
# items without running out.   But if it is true, the enemies will use up their
# SP points as they use items.  The SP points used will be based on the SP Cost
# of the Skill it is switched from.
#
#------------------------------------------------------------------------------
#
# COMPATABILITY:
#
# This system is compatible  with the default battlesystem,  SDK 2.2,  the RTAB
# system,  Fomar's Action Cost,  ParaDog's  and XRXS's battle system in certain
# limits.
#
# Systems that  use AT bars  (everything except the 'default' and 'SDK default' 
# battlesystems) must have the SWITCH_SP_LIMIT value set to 'false'.  All other
# systems either use up the enemy sp  through repeated calls of the make_action
# def as their AT gauges are filled, or uses values other than sp_cost.
#
# In short,  the 'SWITCH_SP_LIMIT' value can only be 'true' if you're using the
# default or SDK default battlesystems.
#
#==============================================================================


   #========================================================================
   #  **  C  O  N  F  I  G  U  R  A  T  I  O  N      S  Y  S  T  E  M  **  #
   #========================================================================

   
# This constant holds the list of skills and items they are switched with.  It
# is in the following format:  { skill.id => item.id, skill.id => item.id,... }
# The sample currently switches the "Heal" skill with the "Sharp Stone" item, 
# and the "Greater Heal" with the "Potion" item.
  SWITCH_SKILL_ITEM       = {1 => 13, 2 => 1}


# This constant holds the lists of skills & items switched  for each enemy that
# uses them. The format to apply the switch mimics the design to the one above:
# { enemy.id => ( skill.id => item.id), enemy.id =>{ skill.id => item.id},... }
# The sample currently switches the "Mass Heal" skill with the "Tonic" item for
# the GHOST enemy,  and changes the "Heal" skill  with the "Barrier Stone" item
# for the BASILISK enemy.
  SWITCH_ENEMY_SKILL_ITEM = { 1 => {3 => 9}, 2 => {1 => 14}}
  

# This merely determines if you limit the items based on Skill SP cost. If this
# is true,  then the enemy must have enough SP to use the original skill before
# the item is used.  If it is false, then the enemy has an infinite supply.
  SWITCH_SP_LIMIT = false

  
#==============================================================================
# ** Game_Enemy
#------------------------------------------------------------------------------
#  This class handles enemies. It's used within the Game_Troop class
#  ($game_troop).
#==============================================================================

class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # * Make Action
  #--------------------------------------------------------------------------  
  alias item_make_action make_action
  def make_action
    item_make_action
    temp_hash = {}
    switch_id = self.current_action.skill_id  
    # Sets the Skill Limiting switch if the SWITCH_SP_LIMIT is true.
    # If true, this sets 'sp_limted' to true if SP is too low for item use.
    if SWITCH_SP_LIMIT
      if $data_skills[switch_id].sp_cost > self.sp
        sp_limited = true
      else
        sp_limited = nil
      end
    else
      sp_limited = nil
    end
    # Perform Skill/Item switch if 'sp_limited' switch isn't true.
    # It won't be true if there's enough SP, or if SWITCH_SP_LIMITED is false.
    if !sp_limited
      if SWITCH_SKILL_ITEM.include? switch_id
        self.current_action.kind = 2
        self.current_action.item_id = SWITCH_SKILL_ITEM[switch_id]      
        self.current_action.decide_random_target_for_enemy      
      end
      temp_hash = SWITCH_ENEMY_SKILL_ITEM[self.id] if SWITCH_ENEMY_SKILL_ITEM.include? self.id
      if temp_hash.include? switch_id
        self.current_action.kind = 2
        self.current_action.item_id = temp_hash[switch_id]      
        self.current_action.decide_random_target_for_enemy            
      end
    end
    # Use up SP (If SWITCH_SP_LIMIT is true)
    if SWITCH_SP_LIMIT
      self.sp =- $data_skills[switch_id].sp_cost
    end
  end
end



#==============================================================================
# ** Scene_Battle
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  #--------------------------------------------------------------------------
  # * Make Item Action Results
  #--------------------------------------------------------------------------  
  alias e_make_item_action_result make_item_action_result
  def make_item_action_result(battler = @active_battler)
    @rtab = !@target_battlers
    if @active_battler.is_a?(Game_Actor)
      @rtab ? e_make_item_action_result(battler) : e_make_item_action_result
    else
      # Get item
      @item = $data_items[battler.current_action.item_id]
      if @rtab
        # Setting animation ID
        battler.anime1 = @item.animation1_id
        battler.anime2 = @item.animation2_id
        # Setting common event ID
        battler.event = @item.common_event_id        
      else
        # 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
      end
      # Deciding the object
      index = battler.current_action.target_index
      target = $game_party.smooth_target_actor(index)      
      if @rtab
        # Setting the object side battler
        set_target_battlers(@item.scope, battler)  
        # Applying the effect of the item
        for target in battler.target
          target.item_effect(@item, battler)
        end
      else
        # Set targeted battlers
        set_target_battlers(@item.scope)
        # Apply item effect
        for target in @target_battlers
          target.item_effect(@item)
        end
      end
    end
  end
end

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top