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.

Adventure Item script

Edit: new version is up.
fixed a bug, removed the need for variables,
and made item menu return to map.

Adventure Item script
by silver wind


Description

This scripts lets an event bring up the item menu, and do an action based on the selected item.
- Useful for adventure games, where players must use items on game object to advance.
- Useful for a crafting system, or any system that requires selection of weapon/armors from the menu.

Instructions

1. start item selection:
in a script command, use:
select_item

2. check if item was used:
in a cond. branch script use:
item_used?(x)

replace x with the item's number,
or name in brackets, like: 'potion'
* for weapon/armor, add 'weapon' or 'armor',
ie: item_used?(x, 'weapon')

3. Enable selection of weapon/armor
in a script command, use:
Items::select_equip = true

* To disable, set it to false.

FAQ

- Why do I need a script for that?
A: By default, weapons, armors and key items (scope 'none')
are disabled on the item menu.
This script lets the player select them anyway.
It also provides a way to check what item was selected.
and a way to enable/disable selection of weapon/armor.

If you have anymore question, feel free to ask. :)

Demo
download demo
^ it's skyDrive, just click Download
Please ignore the 'danger!! .exe file' warning.

Script

old version
[rgss]#====================================================================
# ** Item menu addon
#-------------------------------------------------------------------
#  by: Silver Wind
#
#  - What is this script for ?
#  By default, weapons, armors and items with
#  scope 'none' are disabled on the item menu.
#  This script lets the player select them anyway.
#  Once selected, the item menu will close, and the item
#  is saved in variables 001 and 002, and also in $game_temp.
#  variable 001: number of the item/weapon/armor
#  variable 002: type. 0 means item, 1=weapon and 2=armor.
#  * if you wish to use other variables,
#    change the numbers in lines 26-27.
#====================================================================
 
class Game_Temp
  attr_accessor  :select_equip    # enable weapon/armor selection?
  attr_accessor  :item_id
  attr_accessor  :item_kind
end
 
class Scene_Item
  ID_VAR    = 1
  KIND_VAR  = 2
  def update_item
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to menu screen
      $scene = Scene_Menu.new(0)
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Get currently selected data on the item window
      @item = @item_window.item
           
      # ---------- my edit --------------------------
      #can_use = item.is_a?(RPG::Item)
      case @item
      when RPG::Item
        can_use = @item.scope == 0 or
                  $game_party.item_can_use?(@item.id)
      else
        can_use = ( !@item.nil? and $game_temp.select_equip )
      end
     
      # If it can't be used
      unless can_use
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # chek if the item has a target
      is_weapon = @item.is_a?(RPG::Weapon)
      is_armor  = @item.is_a?(RPG::Armor)
      is_item   = @item.is_a?(RPG::Item)
      no_target = ( is_weapon or
                    is_armor or
                    @item.scope == 0 )
      if no_target
        # save the item and return
        $game_variables[ID_VAR] = @item.id
        kind = is_weapon ? 1 : (is_armor ? 2 : 0)
        $game_variables[KIND_VAR] = kind
        $game_temp.item_id = @item.id
        $game_temp.item_kind = kind
        $scene = Scene_Map.new
        return
      end
      # ---------- my edit --------------------------
     
      # If effect scope is an ally
      if @item.scope >= 3
        # Activate target window
        @item_window.active = false
        @target_window.x = (@item_window.index + 1) % 2 * 304
        @target_window.visible = true
        @target_window.active = true
        # Set cursor position to effect scope (single / all)
        if @item.scope == 4 || @item.scope == 6
          @target_window.index = -1
        else
          @target_window.index = 0
        end
      # If effect scope is other than an ally
      else
        # If command event ID is valid
        if @item.common_event_id > 0
          # Command event call reservation
          $game_temp.common_event_id = @item.common_event_id
          # Play item use SE
          $game_system.se_play(@item.menu_se)
          # If consumable
          if @item.consumable
            # Decrease used items by 1
            $game_party.lose_item(@item.id, 1)
            # Draw item window item
            @item_window.draw_item(@item_window.index)
          end
          # Switch to map screen
          $scene = Scene_Map.new
          return
        end
      end
      return
    end
  end
 
end
 
 
class Window_Item
 
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    can_use = item.is_a?(RPG::Item)
    case item
    when RPG::Item
      number = $game_party.item_number(item.id)
      can_use = item.scope == 0 or
                $game_party.item_can_use?(@item.id)
    when RPG::Weapon
      number = $game_party.weapon_number(item.id)
      can_use = $game_temp.select_equip
    when RPG::Armor
      number = $game_party.armor_number(item.id)
      can_use = $game_temp.select_equip
    end
    # if can use item/ armor /weapon
    if can_use
      self.contents.font.color = normal_color
    else
      self.contents.font.color = disabled_color
    end
    x = 4 + index % 2 * (288 + 32)
    y = index / 2 * 32
    rect = Rect.new(x, y, self.width / @column_max - 32, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    bitmap = RPG::Cache.icon(item.icon_name)
    opacity = self.contents.font.color == normal_color ? 255 : 128
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
    self.contents.draw_text(x + 240, y, 16, 32, ":", 1)
    self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)
  end
 
end
 
 
module Items
 
  module_function
 
  def get_id(key, kind=0)
    # if key is an id number
    if key.is_a?(Integer) and key > 0 and key < $data_items.size
      return key
    end
    case kind
    when 0
      list = $data_items
    when 1
      list = $data_weapons
    when 2
      list = $data_armors
    end
    # if key is a name
    for i in 1...list.size
      item = list
      return item.id if item.name == key
    end
    # otherwise
    return nil
  end
 
  def used?(item, kind=0)
    id = get_id(item, kind)
    return ( id and item_id?(id) and item_kind?(kind) )
  end
 
  # Check the id of the last used item.
  def item_id?(i)
    return ( $game_temp.item_id == i )
  end
 
  # Check the kind of the last used item.
  def item_kind?(k)
    return ( $game_temp.item_kind == k )
  end
 
end
 
 
class Interpreter
  #----------------------------------------------------------
  #  This code can be used in a script command.
  #  Examples:
  #   - item_used?('potion')    OR
  #   - item_used?(1)
  #
  #   - item_used?(1,'weapon')  OR  
  #   - item_used?('Iron Sword','weapon')
  #
  #   - item_used?(1,'armor')   OR
  #   - item_used?('Iron Shield','armor')
  #----------------------------------------------------------
 
  def item_used?(item, kind='item')
    k = ['item','weapon','armor'].index(kind)
    Items::used?(item,k)
  end
 
  def select_item
    $scene = Scene_Item.new
  end
 
end
[/rgss]
new version:
[rgss] 
#====================================================================
# ** Item menu addon
#-------------------------------------------------------------------
#  by: Silver Wind
#
#  Version 1.1
#   - fixed a major bug in Scene_Item
#   - cancling on the item menu will now take you back
#     to the map (if the item menu is opened from an event)
#   - grouped all item-related methods into module 'Items'
#
#  - What is this script for ?
#  By default, weapons, armors and items with
#  scope 'none' are disabled on the item menu.
#  This script lets the player select them anyway.
#  Once selected, the item menu will close, and the item
#  is saved in $game_temp.
#  
#  Instructions
# 1. start item selection
#    in a script command do:
#    select_item
#
# 2. Check if item was used
#    in a cond. barnch (under script) do:
#    item_used?(item, kind)
#    
#    item is ID number or name. ie: 25, 'potion'
#    kind is 'weapon' or 'armor'
#    - for normal items, ommit kind
#
#    ie: item_used?('potion')      << used potion
#        item_used?(1,'weapon')    << used bronze sword
#        item_used?('Iron Shield','armor')  << Iron Shield
#
# 3. Enable equipment selection: (weapon/armor)
#    Items::select_equip = true
#
#====================================================================
 
class Game_Temp
  # flag: enable weapon/armor selection ?
  attr_accessor  :select_equip
  # flag: return to map from the item menu ?
  attr_accessor  :item_menu_to_map
end
 
class Scene_Item
 
  alias adveture_item_main main
  def main
    adveture_item_main
    # turn off the flags. their job is done.
    Items::select_equip = false
    Items::menu_to_map = false
  end
 
  def update_item
   
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
     
      if Items::menu_to_map
        # Return to map
        $scene = Scene_Map.new
      else
        # Switch to menu screen
        $scene = Scene_Menu.new(0)
      end
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Get currently selected data on the item window
      @item = @item_window.item
           
      # ---------- my edit --------------------------
      # If it can't be used
      unless Items::can_use(@item)
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # save the item
      Items::save(@item)
      # return if there's no target
      if Items::no_target
        $scene = Scene_Map.new
        return
      end
      # ---------- my edit --------------------------
     
      # If effect scope is an ally
      if @item.scope >= 3
        # Activate target window
        @item_window.active = false
        @target_window.x = (@item_window.index + 1) % 2 * 304
        @target_window.visible = true
        @target_window.active = true
        # Set cursor position to effect scope (single / all)
        if @item.scope == 4 || @item.scope == 6
          @target_window.index = -1
        else
          @target_window.index = 0
        end
      # If effect scope is other than an ally
      else
        # If command event ID is valid
        if @item.common_event_id > 0
          # Command event call reservation
          $game_temp.common_event_id = @item.common_event_id
          # Play item use SE
          $game_system.se_play(@item.menu_se)
          # If consumable
          if @item.consumable
            # Decrease used items by 1
            $game_party.lose_item(@item.id, 1)
            # Draw item window item
            @item_window.draw_item(@item_window.index)
          end
          # Switch to map screen
          $scene = Scene_Map.new
          return
        end
      end
      return
    end
  end
 
end
 
 
class Window_Item
 
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    can_use = item.is_a?(RPG::Item)
    case item
    when RPG::Item
      number = $game_party.item_number(item.id)
      can_use = (item.scope == 0)
      can_use |= $game_party.item_can_use?(item.id)
    when RPG::Weapon
      number = $game_party.weapon_number(item.id)
      can_use = Items::select_equip
    when RPG::Armor
      number = $game_party.armor_number(item.id)
      can_use = Items::select_equip
    end
    # if can use item/ armor /weapon
    if can_use
      self.contents.font.color = normal_color
    else
      self.contents.font.color = disabled_color
    end
    x = 4 + index % 2 * (288 + 32)
    y = index / 2 * 32
    rect = Rect.new(x, y, self.width / @column_max - 32, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    bitmap = RPG::Cache.icon(item.icon_name)
    opacity = self.contents.font.color == normal_color ? 255 : 128
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
    self.contents.draw_text(x + 240, y, 16, 32, ":", 1)
    self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)
  end
 
end
 
 
module Items
 
  module_function
 
  def get_id(key, kind=0)
    # if key is an id number
    if key.is_a?(Integer) and key > 0 and key < $data_items.size
      return key
    end
    # if key is a name
    case kind
    when 0
      list = $data_items
    when 1
      list = $data_weapons
    when 2
      list = $data_armors
    end
    # return item's id
    for i in 1...list.size
      item = list
      return item.id if item.name == key
    end
    # otherwise
    return nil
  end
 
  def used?(item, kind=0)
    id = get_id(item, kind)
    return ( id and item_id?(id) and item_kind?(kind) )
  end
 
  def item_id?(i)
    return ( @item_id == i )
  end
 
  def item_kind?(k)
    return ( @item_kind == k )
  end
 
  def save(item)
    @item = item
    @item_id = @item.id
    @item_kind = item_kind
  end
 
  def item_kind
    # 0=item, 1=weapon, 2=armor
    kind = is_weapon ? 1 : (is_armor ? 2 : 0)
    return kind
  end
 
  def is_weapon
    @item.is_a?(RPG::Weapon)
  end
 
  def is_armor
    @item.is_a?(RPG::Armor)
  end
 
  def no_target
    return ( is_weapon or
             is_armor or
             @item.scope == 0 )
  end
           
  def can_use(item)
    case item
      when RPG::Item
        use = (item.scope == 0)
        use |= $game_party.item_can_use?(item.id)
      else
        # if armor/ weapon selction is enabled
        use = select_equip
        use = use and !item.nil?
      end
    return use
  end
 
  def menu_to_map
    $game_temp.item_menu_to_map
  end
 
  def select_equip
    $game_temp.select_equip
  end
 
  def menu_to_map=(bool)
    $game_temp.item_menu_to_map = bool
  end
 
  def select_equip=(bool)
    $game_temp.select_equip = bool
  end
 
end
 
 
class Interpreter
 
  def item_used?(item, kind='item')
    k = ['item','weapon','armor'].index(kind)
    Items::used?(item,k)
  end
 
  def select_item
    Items::menu_to_map = true
    $scene = Scene_Item.new
  end
 
end
 
[/rgss]
 

Star

Sponsor

Now this is useful! I can definitely implement this and use it for any game I make. I'll credit you of course. This is should have been in RMXP to begin with.
 

Atoa

Member

Hum... something like the old school FFs where you need to use some key items to trigger some events.
Cool
 
Nice script! Encountered a really -annoying- bug, however.

If a player has nothing in his inventory, he will (without this script) be unable to select anything.

178nk8.png

However, WITH this script, he will be able to select the empty space and be met with this error:

74161946.png

Other than that, this script is AWESOME.

I'd like to see a possibility that if the player cancels out of the inventory window, they'll be returned to Scene_Map instead of Scene_Menu instead. No iffy. :D
 
I'm glad you guys like this script ^^

@Mortarhate
Thanks, I've fixed the code above. I'll update the demo later. You can easily fix this yourself. Simply change line 50 to:
can_use = ( !@item.nil? and $game_temp.select_equip )
 

Star

Sponsor

Mortarhate":3abuz32q said:
I'd like to see a possibility that if the player cancels out of the inventory window, they'll be returned to Scene_Map instead of Scene_Menu instead. No iffy. :D
You would just change line 33 to $scene = Scene_Map.new
 
True, but it'll go back to the map even when you open the item menu normally (not through an event).
I'll fix this too when I update the demo.

Do you guys mind if I remove the variables from the demo?
I don't like it when scripts rely on variables, but I understood those who prefer variables , as they aren't comfortable with scripts.
Currently, the item's number and kind are stored in variables 001 and 002, but that's pointless. You can simply call item_used?(x) .
The problem is that people are forced to spare 2 variables for the script, even if they never use them.
 

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