Chest System

Chest system
Version: 2.0
By: Mark Redeman (or MarkR)


Introduction
With this script you can create chests where the player can store or take items, weapons and armors from.

Features
A chest has two properties. The amount of slots it has and whether the items are stackable or not.
    Stackable.
    Stackable means that the chest can contain more than one of the same type of items on one slot. For example:
    An chest which is stackable can contain 99 Potions on one slot, but a chest which isn’t stackable must have 99 open slots to contain the potions.
    Slots.
    The amount of slots determines how many items a chest can have. A chest which is stackable and has 20 slots can contain 20 different items.

Demo
You can download the demo here.

Script
  1. #==============================================================================
  2. # ** Game_Chest
  3. #------------------------------------------------------------------------------
  4. # Author: Mark Redeman
  5. # Version: 2.0
  6. # Date: 13-01-2011
  7. # Description:
  8. #  This class handles chests, wich can be used to store items.
  9. #  You can choose the amount of slots a chest contains and wheter an chest is
  10. #   stackable or not. If the chest isn't stackable, 1 slot can only contain 1
  11. #   item. If it's stackable all of the slots can contain an infinite amount
  12. #   of items.
  13. # Instructions:
  14. #  To create a chest, create a call script wich uses the following lines:
  15. #-------------------------------------------------------------------------------
  16. #            id = 0
  17. #            if $game_map.chests[id] == nil
  18. #              chest = $game_map.chests
  19. #              chest[id] = Game_Chest.new(slots, stackable)
  20. #            end
  21. #-------------------------------------------------------------------------------
  22. #  To open a chest, use the following line (in a call script):
  23. #   $scene = Scene_Chest.new(chest_id)
  24. #  Where chest_id is the ID of the chest.
  25. #-------------------------------------------------------------------------------
  26. #        To add items, weapons or armors to the chest use the following
  27. #         lines:
  28. #            chest = $game_map.chests[id]
  29. #            chest.gain_item(item_id, quantity)
  30. #            chest.gain_weapon(weapon_id, quantity)
  31. #            chest.gain_item(armor_id, quantity)
  32. #==============================================================================
  33.  
  34. class Game_Chest
  35.   attr_reader :max_slots
  36.   attr_reader :stackable
  37.   #--------------------------------------------------------------------------
  38.   # * Object Initialization
  39.   #--------------------------------------------------------------------------
  40.   def initialize(max_slots = 60, stackable = false)
  41.     @max_slots = [[max_slots, 2997].min, 0].max
  42.     @stackable = stackable
  43.     # Create amount in possession hash for items, weapons, and armor
  44.     @items   = {}
  45.     @weapons = {}
  46.     @armors  = {}
  47.   end
  48.   #--------------------------------------------------------------------------
  49.   # * Get Number of Items Possessed
  50.   #     item_id : item ID
  51.   #--------------------------------------------------------------------------
  52.   def item_number(item_id)
  53.     # If quantity data is in the hash, use it. If not, return 0
  54.     return @items.include?(item_id) ? @items[item_id] : 0
  55.   end
  56.   #--------------------------------------------------------------------------
  57.   # * Get Number of Weapons Possessed
  58.   #     weapon_id : weapon ID
  59.   #--------------------------------------------------------------------------
  60.   def weapon_number(weapon_id)
  61.     # If quantity data is in the hash, use it. If not, return 0
  62.     return @weapons.include?(weapon_id) ? @weapons[weapon_id] : 0
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # * Get Amount of Armor Possessed
  66.   #     armor_id : armor ID
  67.   #--------------------------------------------------------------------------
  68.   def armor_number(armor_id)
  69.     # If quantity data is in the hash, use it. If not, return 0
  70.     return @armors.include?(armor_id) ? @armors[armor_id] : 0
  71.   end
  72.   #--------------------------------------------------------------------------
  73.   # * Gain Items (or lose)
  74.   #     item_id : item ID
  75.   #     n       : quantity
  76.   #--------------------------------------------------------------------------
  77.   def gain_item(item_id, n)
  78.     # Update quantity data in the hash.
  79.     if item_id > 0 && (n < 0 || gain_item?($data_items[item_id], n))
  80.       @items[item_id] = [item_number(item_id) + n, 0].max
  81.       return true
  82.     end
  83.     return false
  84.   end
  85.   #--------------------------------------------------------------------------
  86.   # * Gain Weapons (or lose)
  87.   #     weapon_id : weapon ID
  88.   #     n         : quantity
  89.   #--------------------------------------------------------------------------
  90.   def gain_weapon(weapon_id, n)
  91.     # Update quantity data in the hash.
  92.     if weapon_id > 0 && (n < 0 || gain_item?($data_weapons[weapon_id], n))
  93.       @weapons[weapon_id] = [weapon_number(weapon_id) + n, 0].max
  94.       return true
  95.     end
  96.     return false
  97.   end
  98.   #--------------------------------------------------------------------------
  99.   # * Gain Armor (or lose)
  100.   #     armor_id : armor ID
  101.   #     n        : quantity
  102.   #--------------------------------------------------------------------------
  103.   def gain_armor(armor_id, n)
  104.     # Update quantity data in the hash.
  105.     if armor_id > 0 && (n < 0 || gain_item?($data_armors[armor_id], n))
  106.       @armors[armor_id] = [armor_number(armor_id) + n, 0].max
  107.       return true
  108.     end
  109.     return false
  110.   end
  111.   #--------------------------------------------------------------------------
  112.   # * Lose Items
  113.   #     item_id : item ID
  114.   #     n       : quantity
  115.   #--------------------------------------------------------------------------
  116.   def lose_item(item_id, n)
  117.     # Reverse the numerical value and call it gain_item
  118.     gain_item(item_id, -n)
  119.   end
  120.   #--------------------------------------------------------------------------
  121.   # * Lose Weapons
  122.   #     weapon_id : weapon ID
  123.   #     n         : quantity
  124.   #--------------------------------------------------------------------------
  125.   def lose_weapon(weapon_id, n)
  126.     # Reverse the numerical value and call it gain_weapon
  127.     gain_weapon(weapon_id, -n)
  128.   end
  129.   #--------------------------------------------------------------------------
  130.   # * Lose Armor
  131.   #     armor_id : armor ID
  132.   #     n        : quantity
  133.   #--------------------------------------------------------------------------
  134.   def lose_armor(armor_id, n)
  135.     # Reverse the numerical value and call it gain_armor
  136.     gain_armor(armor_id, -n)
  137.   end
  138.   #--------------------------------------------------------------------------
  139.   # * Amount of items
  140.   #--------------------------------------------------------------------------
  141.   def amount_of_items
  142.     number = 0
  143.     (1...$data_items.size).each   {|item|   number += item_number(item)    }
  144.     (1...$data_weapons.size).each {|weapon| number += weapon_number(weapon)}
  145.     (1...$data_armors.size).each  {|armor|  number += armor_number(armor)  }
  146.     return number
  147.   end
  148.   #--------------------------------------------------------------------------
  149.   # * Amount of types
  150.   #--------------------------------------------------------------------------
  151.   def amount_of_types
  152.     number = 0
  153.     $data_items.each  {|item| number += 1 if item_number(item.id)   > 0 }
  154.     $data_weapons.each{|item| number += 1 if weapon_number(item.id) > 0 }
  155.     $data_armors.each {|item| number += 1 if armor_number(item.id)  > 0 }
  156.     return number
  157.   end
  158.   #--------------------------------------------------------------------------
  159.   # * Gain item
  160.   #    item : item
  161.   #    n    : amount of items
  162.   #--------------------------------------------------------------------------
  163.   def gain_item?(item, n = 1)
  164.     if @stackable
  165.       # Check to see if item is allready in chest
  166.       case item
  167.       when RPG::Item
  168.         return true if item_number(item.id) > 0
  169.       when RPG::Weapon
  170.         return true if weapon_number(item.id) > 0
  171.       when RPG::Armor
  172.         return true if armor_number(item.id) > 0
  173.       end
  174.       # If it isn't in the chest, check if there is an open slot
  175.       return @max_slots >= (amount_of_types + 1)
  176.     else
  177.       return @max_slots >= (amount_of_items + n)
  178.     end
  179.   end
  180. end
  181.  
  182. #==============================================================================
  183. # ** Game_Map
  184. #==============================================================================
  185.  
  186. class Game_Map
  187.   attr_accessor :chests
  188.   #--------------------------------------------------------------------------
  189.   # * Object Initialization
  190.   #--------------------------------------------------------------------------
  191.   alias game_map_chest_initialize_mark initialize
  192.   def initialize
  193.     # Create an array of chests
  194.     @chests = []
  195.     # Call old method
  196.     game_map_chest_initialize_mark
  197.   end
  198. end
  199.  
  200. #==============================================================================
  201. # ** Scene_Chest
  202. #------------------------------------------------------------------------------
  203. # Author: Mark Redeman
  204. # Version: 2.0
  205. # Date: 13-01-2011
  206. # Description: This scene creates a menu where you can store or take items
  207. #               from a chest.
  208. # Instructions: To open the chest menu, call:
  209. #                      $scene = Scene_Chest.new(chest_id)
  210. #==============================================================================
  211.  
  212. class Scene_Chest
  213.   #--------------------------------------------------------------------------
  214.   # * Object Initialization
  215.   #--------------------------------------------------------------------------
  216.   def initialize(id)
  217.     # Get an error message, if chest doesn't exist
  218.     if $game_map.chests.size <= id || id < 0
  219.       p 'Wrong chest id'
  220.     end
  221.     @chest_id = id
  222.   end
  223.   #--------------------------------------------------------------------------
  224.   # * Main Processing
  225.   #--------------------------------------------------------------------------
  226.   def main
  227.     # Make sprite set
  228.     @spriteset             = Spriteset_Map.new
  229.     # Make container windows
  230.     @party_window          = Window_Container.new($game_party)
  231.     @chest_window          = Window_Container.new($game_map.chests[@chest_id])
  232.     @chest_window.visible  = false
  233.     # Make command window
  234.     @command_window        = Window_Horizontal_Command.new(['Inventory', 'Chest'])
  235.     @command_window.active = true
  236.     # Make help window
  237.     @help_window = Window_Container_Help.new($game_map.chests[@chest_id])
  238.     # Associate help window
  239.     @party_window.help_window  = @help_window
  240.     @chest_window.help_window = @help_window
  241.     # Execute transition
  242.     Graphics.transition
  243.     # Main loop
  244.     loop do
  245.       # Update game screen
  246.       Graphics.update
  247.       # Update input information
  248.       Input.update
  249.       # Frame update
  250.       update
  251.       # Abort loop if screen is changed
  252.       if $scene != self
  253.         break
  254.       end
  255.     end
  256.     # Prepare for transition
  257.     Graphics.freeze
  258.     # Dispose of windows
  259.     @party_window.dispose
  260.     @chest_window.dispose
  261.     @command_window.dispose
  262.     @help_window.dispose
  263.     @spriteset.dispose
  264.   end
  265.   #--------------------------------------------------------------------------
  266.   # * Frame Update
  267.   #--------------------------------------------------------------------------
  268.   def update
  269.     # Update windows
  270.     @party_window.update
  271.     @chest_window.update
  272.     @command_window.update
  273.     @help_window.update
  274.     # Update command window, if it's active
  275.     if @command_window.active
  276.       update_command
  277.       return
  278.     end
  279.     # Update item window, if it's active
  280.     if @party_window.active
  281.       update_item
  282.       return
  283.     end
  284.     # Update chest window, if it's active
  285.     if @chest_window.active
  286.       update_chest
  287.       return
  288.     end
  289.   end
  290.   #--------------------------------------------------------------------------
  291.   # * Frame Update (when item window is active)
  292.   #--------------------------------------------------------------------------
  293.   def update_command
  294.     # Set help text
  295.     @help_window.set_text("Select either the party's or the chest's possessions")
  296.     if Input.trigger?(Input::B)
  297.       # Play cancel SE
  298.       $game_system.se_play($data_system.cancel_se)
  299.       # Switch to map screen
  300.       $scene = Scene_Map.new
  301.       return
  302.     end
  303.     case @command_window.index
  304.     # Display selected container
  305.     when 0 # Inventory
  306.       @party_window.visible  = true
  307.       @chest_window.visible  = false
  308.     when 1 # Chest
  309.       @party_window.visible  = false
  310.       @chest_window.visible  = true
  311.     end
  312.     # If C button was pressed
  313.     if Input.trigger?(Input::C)
  314.       case @command_window.index
  315.       when 0 # Inventory
  316.         # Play decision SE
  317.         $game_system.se_play($data_system.decision_se)
  318.         # Activate item window
  319.         @party_window.active   = true
  320.         # Deactivate command window
  321.         @command_window.active = false
  322.       when 1 # Chest
  323.         # Play decision SE
  324.         $game_system.se_play($data_system.decision_se)
  325.         @chest_window.active  = true
  326.         # Deactivate command window
  327.         @command_window.active = false
  328.       end
  329.       return
  330.     end
  331.   end
  332.   #--------------------------------------------------------------------------
  333.   # * Frame Update (when item window is active)
  334.   #--------------------------------------------------------------------------
  335.   def update_item
  336.     # If B button was pressed
  337.     if Input.trigger?(Input::B)
  338.       # Play cancel SE
  339.       $game_system.se_play($data_system.cancel_se)
  340.       @party_window.active   = false
  341.       @command_window.active = true
  342.       return
  343.     end
  344.     # If C button was pressed
  345.     if Input.trigger?(Input::C) || Input.repeat?(Input::C)
  346.       # Get chest
  347.       chest = $game_map.chests[@chest_id]
  348.       # Get currently selected data on the item window
  349.       item = @party_window.item
  350.       unless  chest.gain_item?(item)
  351.         # Play buzzer SE
  352.         $game_system.se_play($data_system.buzzer_se)
  353.         return
  354.       end
  355.       # Check item type
  356.       case item
  357.       when RPG::Item
  358.         # Check if item number is enough
  359.         if $game_party.item_number(item.id) > 0
  360.           # Play decision SE
  361.           $game_system.se_play($data_system.decision_se)
  362.           # Store item in chest
  363.           $game_party.lose_item(item.id, 1)
  364.           chest.gain_item(item.id, 1)
  365.           # Redraw items
  366.           @party_window.lose_item(item)
  367.           @chest_window.add_item(item)
  368.           return
  369.         else
  370.           # Play buzzer SE
  371.           $game_system.se_play($data_system.buzzer_se)
  372.           return
  373.         end
  374.       when RPG::Weapon
  375.         # Check if item number is enough
  376.         if $game_party.weapon_number(item.id) > 0
  377.           # Play decision SE
  378.           $game_system.se_play($data_system.decision_se)
  379.           # Store item in chest
  380.           $game_party.lose_weapon(item.id, 1)
  381.           chest.gain_weapon(item.id, 1)
  382.           # Redraw items
  383.           @party_window.lose_item(item)
  384.           @chest_window.add_item(item)
  385.           return
  386.         else
  387.           # Play buzzer SE
  388.           $game_system.se_play($data_system.buzzer_se)
  389.           return
  390.         end
  391.       when RPG::Armor
  392.         # Check if item number is enough
  393.         if $game_party.armor_number(item.id) > 0
  394.           # Play decision SE
  395.           $game_system.se_play($data_system.decision_se)
  396.           # Store item in chest
  397.           $game_party.lose_armor(item.id, 1)
  398.           chest.gain_armor(item.id, 1)
  399.           # Redraw items
  400.           @party_window.lose_item(item)
  401.           @chest_window.add_item(item)
  402.           return
  403.         else
  404.           # Play buzzer SE
  405.           $game_system.se_play($data_system.buzzer_se)
  406.           return
  407.         end
  408.       end
  409.     end
  410.   end
  411.   #--------------------------------------------------------------------------
  412.   # * Frame Update (when chest window is active)
  413.   #--------------------------------------------------------------------------
  414.   def update_chest
  415.     # If B button was pressed
  416.     if Input.trigger?(Input::B)
  417.       # Play cancel SE
  418.       $game_system.se_play($data_system.cancel_se)
  419.       @chest_window.active   = false
  420.       @command_window.active = true
  421.       return
  422.     end
  423.     # If C button was pressed
  424.     if Input.trigger?(Input::C) or Input.repeat?(Input::C)
  425.       # Get chest
  426.       chest = $game_map.chests[@chest_id]
  427.       # Get currently selected data on the item window
  428.       item = @chest_window.item
  429.       case item
  430.       when RPG::Item
  431.         # Check if item number is enough
  432.         if chest.item_number(item.id) > 0 && $game_party.item_number(item.id) < 99
  433.           # Play decision SE
  434.           $game_system.se_play($data_system.decision_se)
  435.           # Store item in chest
  436.           chest.lose_item(item.id, 1)
  437.           $game_party.gain_item(item.id, 1)
  438.           # Redraw items
  439.           @party_window.add_item(item)
  440.           @chest_window.lose_item(item)
  441.           return
  442.         else
  443.           # Play buzzer SE
  444.           $game_system.se_play($data_system.buzzer_se)
  445.           return
  446.         end
  447.       when RPG::Weapon
  448.         # Check if item number is enough
  449.         if chest.weapon_number(item.id) > 0 && $game_party.weapon_number(item.id) < 99
  450.           # Play decision SE
  451.           $game_system.se_play($data_system.decision_se)
  452.           # Store item in chest
  453.           chest.lose_weapon(item.id, 1)
  454.           $game_party.gain_weapon(item.id, 1)
  455.           # Redraw items
  456.           @party_window.add_item(item)
  457.           @chest_window.lose_item(item)
  458.           return
  459.         else
  460.           # Play buzzer SE
  461.           $game_system.se_play($data_system.buzzer_se)
  462.           return
  463.         end
  464.       when RPG::Armor
  465.         # Check if item number is enough
  466.         if chest.armor_number(item.id) > 0 && $game_party.armor_number(item.id) < 99
  467.           # Play decision SE
  468.           $game_system.se_play($data_system.decision_se)
  469.           # Store item in chest
  470.           chest.lose_armor(item.id, 1)
  471.           $game_party.gain_armor(item.id, 1)
  472.           # Redraw items
  473.           @party_window.add_item(item)
  474.           @chest_window.lose_item(item)
  475.           return
  476.         else
  477.           # Play buzzer SE
  478.           $game_system.se_play($data_system.buzzer_se)
  479.           return
  480.         end
  481.       else # Player selected an empty slot
  482.         # Play buzzer SE
  483.         $game_system.se_play($data_system.buzzer_se)
  484.         return
  485.       end
  486.     end
  487.   end
  488. end
  489.  
  490. #==============================================================================
  491. # ** Window_Container
  492. #------------------------------------------------------------------------------
  493. # Author: Mark Redeman
  494. # Version: 2.0
  495. # Date: 13-01-2011
  496. # Description:
  497. #  This window displays items in the party's or chest's possession
  498. #==============================================================================
  499.  
  500. class Window_Container < Window_Selectable
  501.   #--------------------------------------------------------------------------
  502.   # * Object Initialization
  503.   #--------------------------------------------------------------------------
  504.   def initialize(container = $game_player)
  505.     @container = container
  506.     @x_offset  = 12
  507.     super(128, 112, 384, 256)
  508.     @column_max = 8
  509.     refresh
  510.     self.index = 0
  511.     self.opacity = 155
  512.     self.active  = false
  513.   end
  514.   #--------------------------------------------------------------------------
  515.   # * Get Item
  516.   #--------------------------------------------------------------------------
  517.   def item
  518.     return @data[self.index]
  519.   end
  520.   #--------------------------------------------------------------------------
  521.   # * Refresh
  522.   #--------------------------------------------------------------------------
  523.   def refresh
  524.     if self.contents != nil
  525.       self.contents.dispose
  526.       self.contents = nil
  527.     end
  528.     @data = []
  529.     # Add items
  530.     for i in 1...$data_items.size
  531.       if @container.item_number(i) > 0
  532.         if @container.is_a?(Game_Chest) && !@container.stackable
  533.           (0...@container.item_number(i)).each{ |data| @data.push($data_items[i]) }
  534.         else
  535.           @data.push($data_items[i])
  536.         end
  537.       end
  538.     end
  539.     # Add weapons
  540.     for i in 1...$data_weapons.size
  541.       if @container.weapon_number(i) > 0
  542.         if @container.is_a?(Game_Chest) && !@container.stackable
  543.           (0...@container.weapon_number(i)).each{ @data.push($data_weapons[i]) }
  544.         else
  545.           @data.push($data_weapons[i])
  546.         end
  547.       end
  548.     end
  549.     # Add armors
  550.     for i in 1...$data_armors.size
  551.       if @container.armor_number(i) > 0
  552.         if @container.is_a?(Game_Chest) && !@container.stackable
  553.           (0...@container.armor_number(i)).each{ @data.push($data_armors[i]) }
  554.         else
  555.           @data.push($data_armors[i])
  556.         end
  557.       end
  558.     end
  559.     # If item count is not 0, make a bitmap and draw all items
  560.     @item_max = @data.size
  561.     if @container.is_a?(Game_Chest) && !@container.stackable
  562.       rows   = (@container.max_slots / @column_max) + 1
  563.     else
  564.       rows   = (($data_items.size + $data_weapons.size + $data_armors.size) /
  565.        @column_max + 1)
  566.     end
  567.     height = rows * (32)
  568.     # Recalculate height via
  569.     self.contents = Bitmap.new(width - 32, height)
  570.     self.contents.font.size = 15
  571.     if @item_max > 0
  572.       for i in 0...@item_max
  573.         draw_item(i)
  574.       end
  575.     end
  576.   end
  577.   #--------------------------------------------------------------------------
  578.   # * Draw Item
  579.   #     index : item index
  580.   #--------------------------------------------------------------------------
  581.   def draw_item(index, opacity = nil)
  582.     x = (index % @column_max) * (32 + @x_offset)
  583.     y = (index / @column_max) * (32)
  584.     item = @data[index]
  585.     case item
  586.     when RPG::Item
  587.       number = @container.item_number(item.id)
  588.     when RPG::Weapon
  589.       number = @container.weapon_number(item.id)
  590.     when RPG::Armor
  591.       number = @container.armor_number(item.id)
  592.     end  
  593.     # Set opacity
  594.     opacity = number > 0 ? 255 : 125 if opacity == nil
  595.     # Clear
  596.     rect = Rect.new(x, y, 32, 32)
  597.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  598.     # Load icon graphic
  599.     bitmap = RPG::Cache.icon(item.icon_name)
  600.     # Set icon position in the middle of the slot
  601.     x += (32 - bitmap.width) / 2
  602.     y += (32 - bitmap.height) / 2
  603.     # Draw icon
  604.     self.contents.blt(x, y, bitmap, Rect.new(0, 0, 24, 24), opacity)
  605.     # Draw amount of items if container is stackable
  606.     if number > 0 && ((@container.is_a?(Game_Chest) && @container.stackable) || !@container.is_a?(Game_Chest))
  607.       # Draw shadow of the amount of items in possession
  608.       self.contents.font.color = Color.new(0, 0, 0)
  609.       self.contents.draw_text(x - (32 - bitmap.width) / 2 + 1 , y + 7, 32-2, 32, number.to_s, 2)
  610.       # Draw amount of items in possession
  611.       self.contents.font.color = Color.new(255, 255, 255)
  612.       self.contents.draw_text(x - (32 - bitmap.width) / 2 , y + 6, 32 - 2, 32, number.to_s, 2)
  613.     end
  614.   end
  615.   #--------------------------------------------------------------------------
  616.   # * Add Item
  617.   #     item : item
  618.   #--------------------------------------------------------------------------
  619.   def add_item(item)
  620.     if @data.include?(item) && !(@container.is_a?(Game_Chest) && !@container.stackable)
  621.       # Redraw amount of items
  622.       draw_item(@data.index(item))
  623.     else
  624.       # Check if there is an empty slot
  625.       (0..@data.size).each { |i|
  626.       if @data[i] == nil
  627.         # Place a new item in the empty slot
  628.         @data[i] = item
  629.         # Get new item max
  630.         @item_max = @data.size
  631.         # Draw new item
  632.         draw_item(i)
  633.         return
  634.       end                  }
  635.       # Push new item in @data
  636.       @data.push(item)
  637.       # Get new item max
  638.       @item_max = @data.size
  639.       # Draw new item
  640.       draw_item(@data.size - 1)
  641.     end
  642.   end
  643.   #--------------------------------------------------------------------------
  644.   # * Lose Item
  645.   #     item : item
  646.   #--------------------------------------------------------------------------
  647.   def lose_item(item)
  648.     if (@container.is_a?(Game_Chest) && !@container.stackable)
  649.       # Redraw item with half opacity
  650.       draw_item(@index, 125)
  651.       # Set data to nil
  652.       @data[@index] = nil
  653.     else
  654.       # Redraw amount of items
  655.       draw_item(@data.index(item))
  656.     end
  657.   end
  658.   #--------------------------------------------------------------------------
  659.   # * Update Cursor Rectangle
  660.   #--------------------------------------------------------------------------
  661.   def update_cursor_rect
  662.     # If cursor position is less than 0
  663.     if @index < 0
  664.       self.cursor_rect.empty
  665.       return
  666.     end
  667.     # Get current row
  668.     row = @index / @column_max
  669.     # If current row is before top row
  670.     if row < self.top_row
  671.       # Scroll so that current row becomes top row
  672.       self.top_row = row
  673.     end
  674.     # If current row is more to back than back row
  675.     if row > self.top_row + (self.page_row_max - 1)
  676.       # Scroll so that current row becomes back row
  677.       self.top_row = row - (self.page_row_max - 1)
  678.     end
  679.     # Calculate cursor width
  680.     cursor_width = 32#self.width / @column_max - 32
  681.     # Calculate cursor coordinates
  682.     x = @index % @column_max * (cursor_width + @x_offset)
  683.     y = @index / @column_max * (32) - self.oy
  684.     # Update cursor rectangle
  685.     self.cursor_rect.set(x, y, cursor_width, 32)
  686.   end
  687.   #--------------------------------------------------------------------------
  688.   # * Help Text Update
  689.   #--------------------------------------------------------------------------
  690.   def update_help
  691.     if self.item == nil
  692.       @help_window.set_text('This is an empty slot')
  693.       return
  694.     end
  695.     # Check if container is a chest or a party
  696.     if @container.is_a?(Game_Chest)
  697.       # Show amount of items in party's possession
  698.       @help_window.set_party(self.item)
  699.     else
  700.       # Show amount of items in the chest's possession
  701.       @help_window.set_container(self.item)
  702.     end    
  703.   end
  704. end
  705.  
  706. #==============================================================================
  707. # ** Window_Container
  708. #------------------------------------------------------------------------------
  709. # Author: Mark Redeman
  710. # Version: 2.0
  711. # Date: 13-01-2011
  712. # Description:
  713. #  This is an horizontal Window_Command window.
  714. #==============================================================================
  715.  
  716. class Window_Horizontal_Command < Window_Selectable
  717.   #--------------------------------------------------------------------------
  718.   # * Object Initialization
  719.   #--------------------------------------------------------------------------
  720.   def initialize(commands)
  721.     super(128, 368, 384, 64)
  722.     self.contents = Bitmap.new(width - 32, height - 32)
  723.     @commands   = commands
  724.     @column_max = @commands.size
  725.     @item_max   = @commands.size
  726.     refresh
  727.     self.index = 0
  728.     self.opacity = 155
  729.     self.active  = true
  730.   end
  731.   #--------------------------------------------------------------------------
  732.   # * Refresh
  733.   #--------------------------------------------------------------------------
  734.   def refresh
  735.     # Calculate cursor coordinates
  736.     self.contents.clear
  737.     for i in 0...@item_max
  738.       draw_item(i, normal_color)
  739.     end
  740.   end
  741.   #--------------------------------------------------------------------------
  742.   # * Draw Item
  743.   #     index : item number
  744.   #     color : text color
  745.   #--------------------------------------------------------------------------
  746.   def draw_item(index, color)
  747.     self.contents.font.color = color
  748.     # Calculate cursor width
  749.     cursor_width = self.width / @column_max - 32
  750.     # Calculate x
  751.     x = index % @column_max * (cursor_width + 32) + 2
  752.     rect = Rect.new(x, 0, self.contents.width - x, 32)
  753.     # Clear drawn area
  754.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  755.     # Draw command
  756.     self.contents.draw_text(rect, @commands[index])
  757.   end
  758. end
  759. #==============================================================================
  760. # ** Window_Container_Help
  761. #------------------------------------------------------------------------------
  762. # Author: Mark Redeman
  763. # Version: 2.0
  764. # Date: 13-01-2011
  765. # Description:
  766. #  This window shows amount of items in the party's or container's possession
  767. #==============================================================================
  768.  
  769. class Window_Container_Help < Window_Base
  770.   #--------------------------------------------------------------------------
  771.   # * Object Initialization
  772.   #--------------------------------------------------------------------------
  773.   def initialize(container)
  774.     super(128, 48, 384, 64)
  775.     @container = container
  776.     self.opacity  = 155
  777.     self.contents = Bitmap.new(width - 32, height - 32)
  778.   end
  779.   #--------------------------------------------------------------------------
  780.   # * Set Text
  781.   #  text  : text string displayed in window
  782.   #  align : alignment (0..flush left, 1..center, 2..flush right)
  783.   #--------------------------------------------------------------------------
  784.   def set_text(text, align = 0)
  785.     # If at least one part of text and alignment differ from last time
  786.     if text != @text or align != @align
  787.       # Redraw text
  788.       self.contents.clear
  789.       self.contents.font.color = normal_color
  790.       self.contents.draw_text(0, 0, self.contents.width, 32, text, align)
  791.       @text = text
  792.       @align = align
  793.     end
  794.     self.visible = true
  795.   end
  796.   #--------------------------------------------------------------------------
  797.   # * Set party
  798.   #   Shows the amount of items in the party's possession
  799.   #  item  : selected item
  800.   #--------------------------------------------------------------------------
  801.   def set_party(item)
  802.     unless item.nil?
  803.       text = item.name + " in party's possession: "
  804.       # Get the number of items in the party's possession
  805.       case item
  806.       when RPG::Item
  807.         number = $game_party.item_number(item.id)
  808.       when RPG::Weapon
  809.         number = $game_party.weapon_number(item.id)
  810.       when RPG::Armor
  811.         number = $game_party.armor_number(item.id)
  812.       end
  813.       # Clear contents
  814.       self.contents.clear
  815.       # Set normal color
  816.       self.contents.font.color = normal_color
  817.       # Draw item name
  818.       self.contents.draw_text(0, 0, self.contents.width - 32, 32, text)
  819.       # Set system color
  820.       self.contents.font.color = number > 0 ? system_color : disabled_color
  821.       # Draw amount of items
  822.       self.contents.draw_text(self.contents.width - 32, 0, 32, 32, number.to_s, 2)
  823.     else
  824.       # The slot is empty
  825.       set_text('This slot is an empty slot.')
  826.     end
  827.   end
  828.   #--------------------------------------------------------------------------
  829.   # * Set container
  830.   #   Shows the amount of items in the container's possession
  831.   #  item  : selected item
  832.   #--------------------------------------------------------------------------
  833.   def set_container(item)
  834.     text = item.name + " in chest's possession: "
  835.     # Get the number of items in the container's possession
  836.     case item
  837.     when RPG::Item
  838.       number = @container.item_number(item.id)
  839.     when RPG::Weapon
  840.       number = @container.weapon_number(item.id)
  841.     when RPG::Armor
  842.       number = @container.armor_number(item.id)
  843.     end
  844.     # Clear contents
  845.     self.contents.clear
  846.     # Set normal color
  847.     self.contents.font.color = normal_color
  848.     # Draw item name
  849.     self.contents.draw_text(0, 0, self.contents.width - 32, 32, text)
  850.     # Set system color
  851.     self.contents.font.color = number > 0 ? system_color : disabled_color
  852.     # Draw amount of items
  853.     self.contents.draw_text(self.contents.width - 32, 0, 32, 32, number.to_s, 2)
  854.   end
  855. end
Expand to see the code.


Instructions
Creating a chest.
Creating a new chest is very easy. Use the following code in a call script:
  1. if $game_map.chests[ID] == nil
  2. chest = $game_map.chests
  3. chest[ID] = Game_Chest.new(max_slots, stockable)
  4. end
Expand to see the code.

Where the ID is the number which you will be using to call the chest menu.
max_slots is the amount of slots the chest has (Default value is 60).
stockable is a Boolean which determines if the chest is stackable or not (Default value is false).


Example 1:
Creates a chest which can contain 20 different item types.
  1. if $game_map.chests[0] == nil
  2. chest = $game_map.chests
  3. chest[0] = Game_Chest.new(20, true)
  4. end
  5.  
Expand to see the code.


Example 2:
This chest will have 80 slots. Every item, weapon or armor which is in the chest will use one slot.
  1. if $game_map.chests[0] == nil
  2. chest = $game_map.chests
  3. chest[0] = Game_Chest.new(80, false)
  4. end 
Expand to see the code.


Adding items to a chest
Adding items, weapons and armors can be done via the following code:
  1. chest = $game_map.chests[ID]
  2. chest.gain_item(item_id, quantity)
  3. chest.gain_weapon(weapon_id, quantity)
  4. chest.gain_weapon(item_id, quantity)
  5.  
Expand to see the code.

Where ID is the ID number you used to create the chest.

Activating the chest menu
You can activate the menu with the following code:
  1. $scene = Scene_Chest.new(Chest_ID)
Expand to see the code.


Screenshots
This is the menu from which you can store or take items from the chest.
Image

This is what a typical chest event would look like.
Image

Compatibility
The script won’t work with old save files.

Author's Notes
I would appreciate it if I could get some criticism on this script as it’s my first script I published on an English community. ;)

Terms and Conditions
You may use this script as long as you give me credits for making the script.
Nice script. It remembers grandia one. As a easy addition it can be interesting to set the items types that a chest can hold.

Im going to add feedhback:

Quote:
# if $game_map.chests[0] == nil
# chest = $game_map.chests
# chest[0] = Game_Chest.new(20, true)
# end


I dont like this. Why use the if here?



Also i like shorter methods in the intepreter that call complex code:

  1.  
  2. def create_chest(num, type, id)
  3.       # if $game_map.chests[id] == nil
  4. # chest = $game_map.chests
  5. # chest[id] = Game_Chest.new(num, type)
  6. # end
  7. end
  8.  
Expand to see the code.


Thats posible too with the adds or show methods.

For last its better to tell the user the default values or the internal script limits.
First of all, thanks for the feedback! 😀

I put the if there so it won't replace an old chest with a new chest.
For example: If you want a chest which you can access in every inn in the game, you would have to create the chest at the start of the game, because if you where to put only the chest[id] = Game_Chest.new(num, type) you would overwrite the chest every time you try to open that chest.

gerrtunk wrote:
Nice script. It remembers grandia one. As a easy addition it can be interesting to set the items types that a chest can hold.
Also i like shorter methods in the intepreter that call complex code:

  1.  
  2. def create_chest(num, type, id)
  3.       # if $game_map.chests[id] == nil
  4. # chest = $game_map.chests
  5. # chest[id] = Game_Chest.new(num, type)
  6. # end
  7. end
  8.  
Expand to see the code.
I will look into that. I haven't worked with the interpreter yet so I didn't think of that.

gerrtunk wrote:
As a easy addition it can be interesting to set the items types that a chest can hold.
I will be adding that feature as an addon as well as showing both windows (the chest window and the party window) at the same time.

gerrtunk wrote:
For last its better to tell the user the default values or the internal script limits.
Do you mean the default values of the amount of slots and whether a chest is stackable or not? I hadn't thought about that. I will add a feature to configure these settings in the next version.
First of all... I like the idea of the script... while it's certainly not your idea to pack items in chests for later use, to my knowledge you're the first to make a script for that. You seem to be rather new at scripting, though, and I think I see some typical eventer's habits here and there... but yeah, let me give you some more pointy feedback as well...

First of all, your layout is a bit questionable if you ask me... you're breaking with standart RMXP conventions a few times in your screenshot there (which is all I've seen from your script, as I didn't try the demo). First of all, you decided against item names in favor for a lot of items... the question is, is that a good thing to do, as usually, people use the same icon for multiple things. Also, item names generally help you finding weapons, as the name is what you identify them by usually - the icon is just a supportive feature.
Second, your help and inventory/chest icons seem to be exchanged... the order to me should be 'select where to operate' -> 'operate (as in select item)' -> 'get help for the item if needed'.
Last but not least, your Window_Command instance is a bit off... your items there are supposed to have an x value of 4, which is why yours look so damn stuck to the left side of the selection area, I think... that you should definately fix imo, it doesn't look very good.

Interface-wise, a selection menu for the two different item storages seems like an easy way to go, requires an additional (and unneeded) key press. Now if you take the Bethesda approach, displaying your inventory and the chest next to each other not only let's you select the other window with a single keypress (L/R, for example), but also shows both lists at once. It's a design that proved itself, and with a bit of writing, you can make sure every last person gets it... personally, I think it's one of the most superior scripts handling this, while I won't miss my very own chance to take it on... but more on that later.

Your scripting style is a bit questionable at times (you have Window_Container in your headers twice, for example... as well as your scene before the window definitions, which isn't really what anyone does... and yeah, you once use if Input.trigger?(Input::C) or Input.repeat?(Input::C), and once if Input.trigger?(Input::C) || Input.repeat?(Input::C) ^^ ), however basically solid. Without looking at it too extensively, the fact that it's 900 lines makes me wonder if you don't have anything unneeded in there, as I don't think you'd need that many to get this functionality done.

So yeah... it might seem to you that I teared it pretty much apart, but believe me - you got off the hook easily ^^ I actually think this script has a lot of potential, and it's a nice way of getting you very deep into RGSS ^^ (otherwise I wouldn't invest that much time writing this now, would i?).
That being said - keep up the good work, and I'm looking forward to updates ;)



Last but not least... the promised concept of mine regarding chests - and actually a unique and pretty awesome approach, if I may say so myself. ;)
The thought behind it is that you need several things at once, without loosing time or finger strength pressing too many buttons. Also, the fact that you might at all times see the amount in a chest vs. the amount in your inventory without looking both up individually is a factor. Therefore, I created a concept working with 3 columns:

  1. [ Chest ] [ Item List ] [ Inventory ]
Expand to see the code.


The center column shows you the list of all the items that are either a) in the current chest, or b) in your inventory, or c) in both. You best get this if you iterate through $data_items, and check in exactly the way I said earlier (though you can neglect case c) ).
The left and right columns show the amount of items carried of the specific one... or 0 in disabled color if there are none at the time (because they're only in the other container).

Now, you operate it with your arrow keys solely: Up and down keys scroll through items, left moves the current item from chest to inventory, and right does the opposite. You completely save switching through scenes, as well as gain overview, as you can compare 1:1 which items are where, and how many of 'em.
Obviously, this won't work with the icon bunch style that you chose, so if that's what you want to keep up, well... this beauty of an idea of mine (modest, am I not?) won't work. Seriously though: You don't need a chest system in a game where you can carry 6 billion items. Therefore: I can only vote for the list, and I think my system COULD work out pretty nicely... the idea is yours under one condition: You get rid of the 'mark's in the alias names, and promise to work on your scripting style :p (that's right, no credit needed)
I try using this in my game
*thx before

but the same item not add in 1 item.. the result when I input 5 hi-potion..
there 5 hi potion in chest.. not 1 hi-potion with 5 number like the ss above

is there a config to make like SS?
I’m sorry for this late response. I have been busy with other things and forgot about this script. :X

Thanks a lot for your criticism. I’m certain it will help me make a better script. I didn’t know that the items in a command window are supposed to have an x value of 4, but now that I read you post I can see that it indeed looks strange.

Like you said, I made some stupid mistakes with my scripting style (most likely because I was a bit too eager to publish this script) and will hopefully fix that in the next version.

I like your concept. It’s definitely more user friendly, but I also liked the idea of the chests having slots so I’m not yet sure if I will use your idea. I might make two versions, one for large chests and another one for smaller chests, which will use the slots.

One last thing. You said that I should get rid of the mark’s in my aliases (which is fine by me), but I read in the SDK that I should format my aliases like this:
yourname_scriptname_classname_methodname

So should I still use this format (only if the idea came from me of course) or is it not necessary?

setanbedul wrote:
I try using this in my game
*thx before

but the same item not add in 1 item.. the result when I input 5 hi-potion..
there 5 hi potion in chest.. not 1 hi-potion with 5 number like the ss above

is there a config to make like SS?


Could you be a bit more specific? What script codes did you use?
I normally don't speak English, so it would also help if you improved your spelling and grammar. (A)
MarkR wrote:
One last thing. You said that I should get rid of the mark’s in my aliases (which is fine by me), but I read in the SDK that I should format my aliases like this:
yourname_scriptname_classname_methodname

So should I still use this format (only if the idea came from me of course) or is it not necessary?
Well, that's something I'm kinda fighting over with everyone... theoretically
, there could be someone creating an equal script to yours, naming their methods the same and therefore have the same alias names... and apparently, what the SDK creators thought was that there's someone who needs two chest scripts in their game... which is something I don't see happening anytime soon.

So basically, naming aliases is about uniqueness, but not on the cost of being precise and as short as possible... as you could also name an alias fdjngondmfoejornownrfonreognerog_update - not doing anyone any good. Personally, I prefer the format [script]_[original method], such as chestsystem_update. I know that the SDK states something else, so I guess it's up to you to decide on this (as you're the scripter ;) )... personally, the author's name doesn't help me while reading the script, so more unique but actually helpful alias names is definately what I prefer over bluescope_aka_the_third_man_aka_mike_update ^^
if the img small. just click it
MarkR wrote:
...
setanbedul wrote:
I try using this in my game
*thx before

but the same item not add in 1 item.. the result when I input 5 hi-potion..
there 5 hi potion in chest.. not 1 hi-potion with 5 number like the ss above

is there a config to make like SS?


Could you be a bit more specific? What script codes did you use?
I normally don't speak English, so it would also help if you improved your spelling and grammar. (A)
okay
Image
this is my basic world.. there 2 event.. a girl that give me some item and chest
Image
I talked to her and receive item (10 for each item)
Image
in my item menu

Image
now the chest
Image
my item before add
Image
my chest before add
Image
I add a few item
Image
when i click on chest I got this?!? the item is not group but separate
Image
when i return some item.. the item that I remove is still in there .. actualy i can't click it.. is there a bug or this is the script fitur??

thx 4 the script
@BlueScope: Thanks for explaining that to me! I will try to improve the scripting style in the next version of this script.

@setanbedul: That's because the chest you made isn't "stackable", if you don't want the items to be separate, you should use the following code:
  1.  
  2. if $game_map.chests[chest_id] == nil
  3. chest = $game_map.chests
  4. chest[chest_id] = Game_Chest.new(amount_of_slots, true)
  5. end
  6.  
Expand to see the code.

Don't forget to change the chest_id and amount_of_slots.

This is the code I used for the chest in the first screenshot:
  1.  
  2. if $game_map.chests[2] == nil
  3. chest = $game_map.chests
  4. chest[2] = Game_Chest.new(99, true)
  5. end
  6.  
Expand to see the code.

This will create a chest which is stackable and has 99 slots, which means that you can store 99 different items in the chest.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=73473