Storage Boxes

Hey Imp1,

I was kinda hoping you would add the spacing thing to the main script.
However, that you basically re-release my add-on as yours wasn't quite how I pictured this to go.
But I guess that means less maintenance and thus more free time for me...


I'm afraid I need to report a bug.
I'm using
- the base script v1.10
- the Item Sizes Add-on v1.4
- TheoAllen - Limited Inventory v1.4c

When LimInv.ForceGain is set to false and the player presses "take all" when withdrawing from a storage box, it will remove them all from the box even if they don't all fit.

Example: you can carry 3 more items. There are 5 potions in the box. You press "take all".
Result: no more potions in the box, but only 3 (new ones) in your inventory.

Could you please implement a check in the Item Size add-on, so that the remaining 2 potions stay in the box? Thank you.

I haven't tested this with "take Lots", but I presume it occurs there as well.

And when using "take 1", I somehow cannot fill the last slot. I keep getting the buzzer sound as if my inventory was already full?!
(unless of course, the item has a size of zero)

Thanks for the bug report. It's an interesting one, especially coming back after not really having touched these scripts in many many years.

I think the Take All/Lots needs to be have a bit of a rethink. If you only have space for 3, then maybe it makes more sense to not show an option that says "Take 5".
But yeah, take all should take as many as you have room for.

I'll have a look over stuff this weekend and get all the add-ons working, and test it with Theo's Limited Inventory script, and (hopefully) fix this bug.

Also, I hope you don't feel put out by the readonly script basically be the same as yours. I felt a bit weird editing your script to remove the space padding and then giving that as an option of yours, because that would be putting your name to something I'd edited (and so something you might no longer want your name on). And I also wanted to split up the space padding from the readonly script, as they feel like separate things. That said, there are tiny adjustments from your script to mine (I think! I wrote it pretty late at night). But using a size of -1 made sense. I've also made it specifically -1 in case someone comes along with other ideas that might potentially use -2, -3, etc.



@kyonides: I think, depending on your needs obviously, using a box might make certain things easier. The example that springs to mind is a mailbox. Where the game might deliver letters throughout the story, and the player might not have to read them at any given time. You definitely could do that with eventing (eventing is just coding in a different way!), I would find it easier to use a readonly box, and just add 'letter' items to it throughout the game.
Hi,
I didn't notice you have already replied. As you probably won't be notified about edited posts, I'll quote myself here:
Edit: Found yet another bug: if my inventory is full, I cannot put anything in a box which has a limited capacity.
Reason: you made a mistake when overwriting the capacity(box_id) method in the add-on. You check how many slots the inventory has left free, rather than the box.

It's true that the padding thing and the read-only feature are separate things. And yes, you did make some changes by turning my data type-based IF statements into ones that use proper boolean config variables.
I'm not used to writing interpreter code, so usually people don't get to see the result (code-wise) of my obsession with performance optimization. 😅

Thanks in advance for fixing those bugs.
Hi again Imp1,

in case you haven't fixed it already, I wanted to point out why the "I can't fill my last inventory slot" bug, which I reported, occurs:
In the item size add-on, the method can_move_item_to_inventory? should read
Ruby:
$game_party.total_inv_size + item.inv_size <= $game_party.inv_max
rather than
Ruby:
$game_party.total_inv_size + item.inv_size < $game_party.inv_max
It's missing an equal sign.


I would also like to suggest a small improvement to the default values of your "Storage Boxes" config.

Instead of hard-coding a number into TAKE_LOTS_TEXT and GIVE_LOTS_TEXT, you might wanna change them to
Ruby:
GIVE_LOTS_TEXT = "Store #{GIVE_LOTS_AMOUNT}"
TAKE_LOTS_TEXT = "Take #{TAKE_LOTS_AMOUNT}"
That way the text is automatically up-to-date, if the developer ever changes either of those ..._AMOUNT constants.
Hello once more Imp1,

call me impatient, but I wanted to achieve some progress with my own scripts. And as one of them requires a compatibility patch to work with "Storage Boxes", I didn't wanna wait any longer and fixed the bugs, which I reported in my last posts, myself.
I actually found another one while doing this, which I fixed as well.

As it still has your name on it, I presume you don't mind.


Storage Boxes "item size" add-on v1.5​

Spoiler: code
Ruby:
#==============================================================================
# Storage Boxes Addon: Item Sizes v1.5
#   by IMP1
# with bug fixes by D4NTE
#------------------------------------------------------------------------------
# THIS SCRIPT REQUIRES THE 'STORAGE BOXES' SCRIPT BY IMP1
#------------------------------------------------------------------------------
#   Compatability:
#
# Aliased Methods:
#   IMP1_Game_Boxes.fullness
#   IMP1_Game_Boxes.space_for
#   Scene_ItemStorage.can_move_item_to_inventory?
#   Window_BoxTitle.refresh
#
# New Methods/Fields:
#   Window_BoxTitle.draw_inventory_amount
#   Scene_ItemStorage.can_take_all?
#   Scene_ItemStorage.can_take_lots?
#   Window_TakeMore.take_all
#   Window_TakeMore.take_lots
#
# Overwritten Methods:
#   Scene_ItemStorage.offer_to_take_multiple
#   Scene_ItemStorage.take_lots
#   Scene_ItemStorage.take_one
#
#------------------------------------------------------------------------------
#   Version History:
# v1.5 [2022/11/01] : Fixed the following bugs: (by D4NTE)
#        - "Take Lots" and "Take All" could over-fill the inventory, even when
#            ForceGain was set to false
#        - the "Take Lots" and "Take All" options would remain selectable after
#            the amount of items in the box was reduced below the corresponding
#            threshold via "Take 1" or "Take Lots"
#        - the last inventory slot could not be filled by withdrawing an item, if
#            ForceGain was set to false
#        - it was not possible to place any items in a box with finite capacity,
#            if the party's inventory was full or over-filled
# v1.4 [2018/04/04] : Added capacity method as well.
# v1.3 [2015/06/27] : Use updates to base script.
# v1.2 [2014/11/16] : Fixed bug where inventory limit would not update.
# v1.1 [2014/11/15] : Added and displays the player inventory limit.
# v1.0 [2014/11/14] : Initial release.
#==============================================================================

#==============================================================================
# Game_Boxes
#==============================================================================
class IMP1_Game_Boxes
  #--------------------------------------------------------------------------
  # Returns the space in a box.
  #--------------------------------------------------------------------------
  alias :default_size_fullness :fullness unless $@
  def fullness(box_id)
    if $imported[:Theo_LimInventory]
      i = 0
      box(box_id).each do |item, amount|
        i += item.inv_size * amount
      end
      return i
    else
      return default_size_fullness(box_id)
    end
  end
  #--------------------------------------------------------------------------
  # Returns true if there is enough room for multiple items.
  #--------------------------------------------------------------------------
  alias :default_size_space :space_for unless $@
  def space_for(item, amount, box)
    if $imported[:Theo_LimInventory]
      amount *= item.inv_size
    end
    return default_size_space(item, amount, box)
  end
  #--------------------------------------------------------------------------
  # Returns how many of said item can fit into the box.
  #--------------------------------------------------------------------------
  alias :default_size_fit :how_many_fit unless $@
  def how_many_fit(item, box_id)
    if $imported[:Theo_LimInventory]
      return (capacity(box_id) / item.inv_size).to_i
    else  
      return default_size_fit(item, box_id)
    end
  end
  
end # IMP1_Game_Boxes

#==============================================================================
# Scene_ItemStorage
#==============================================================================
class Scene_ItemStorage
  #--------------------------------------------------------------------------
  # Returns true if there is enough room in the player's inventory.
  #--------------------------------------------------------------------------
  alias :default_can_move? :can_move_item_to_inventory? unless $@
  def can_move_item_to_inventory?(item)
    return false if !default_can_move?(item)
    space_in_inventory = true
    if $imported[:Theo_LimInventory]
      space_in_inventory = 
          $game_party.total_inv_size + item.inv_size <= $game_party.inv_max
    end
    return space_in_inventory
  end
  
  #--------------------------------------------------------------------------
  # Give the player the option to take one, X, or all. X is controlled be the
  # TAKE_LOTS_AMOUNT in IMP1_Storage_Boxes.
  #--------------------------------------------------------------------------
  def offer_to_take_multiple(item)
    item_count = $game_boxes.item_number(item, @box_id)
    return false if item_count <= 0
    can_take_all = can_take_all?(item)
    can_take_lots = can_take_lots?(item)
    if can_take_all || can_take_lots
      @take_window = Window_TakeMore.new(can_take_all, can_take_lots)
      @take_window.set_handler(:take_one, method(:take_one))
      @take_window.set_handler(:take_lots, method(:take_lots))
      @take_window.set_handler(:take_all, method(:take_all))
      @box_window.deactivate
      @take_window.activate
      return true
    end
    return false
  end
  
  def can_take_all?(item)
    item_count = $game_boxes.item_number(item, @box_id)
    return IMP1_Storage_Boxes::TAKE_ALL_OPTION &&
      item_count >= IMP1_Storage_Boxes::TAKE_ALL_THRESHOLD &&
      ($game_party.inv_max_item(item) >= item_count || Theo::LimInv::ForceGain)
  end
  
  def can_take_lots?(item)
    item_count = $game_boxes.item_number(item, @box_id)
    return IMP1_Storage_Boxes::TAKE_LOTS_OPTION &&
      item_count >= IMP1_Storage_Boxes::TAKE_LOTS_AMOUNT &&
      ($game_party.inv_max_item(item) >= IMP1_Storage_Boxes::TAKE_LOTS_AMOUNT ||
        Theo::LimInv::ForceGain)
  end
  
  #--------------------------------------------------------------------------
  # Take X of this item from the box, where X is TAKE_LOTS_AMOUNT
  #--------------------------------------------------------------------------
  def take_lots
    item = @box_window.item
    amount = IMP1_Storage_Boxes::TAKE_LOTS_AMOUNT
    if can_move_item_to_inventory?(item)
      success = $game_boxes.transfer(item, amount, @box_id, :party)
      if !success
        Sound.play_buzzer
        return
      end
      play_withdraw_sound(item)
      #new_item_count = $game_boxes.item_number(item, @box_id)
      
      leave_open = true
      if !IMP1_Storage_Boxes::TAKE_LOTS_LEAVE_OPEN || !can_take_lots?(item) &&
            @take_window.take_lots
        leave_open = false
      end
      if IMP1_Storage_Boxes::TAKE_ALL_OPTION && !can_take_all?(item) &&
            @take_window.take_all
        leave_open = false
      end
      
      
      if leave_open
        @take_window.activate
      else
        @take_window.deactivate.close
        @box_window.activate
      end
      refresh
    else
      Sound.play_buzzer
    end
  end
  
  #--------------------------------------------------------------------------
  # Take only one of this item from the box.
  #--------------------------------------------------------------------------
  def take_one
    item = @box_window.item
    if can_move_item_to_inventory?(item)
      success = $game_boxes.transfer(item, 1, @box_id, :party)
      if !success
        Sound.play_buzzer
        return
      end
      play_withdraw_sound(item)
      new_item_count = $game_boxes.item_number(item, @box_id)
      
      leave_open = true
      if !IMP1_Storage_Boxes::TAKE_ONE_LEAVE_OPEN || new_item_count < 1
        leave_open = false
      end
      if IMP1_Storage_Boxes::TAKE_LOTS_OPTION && !can_take_lots?(item) &&
            @take_window.take_lots
        leave_open = false
      end
      if IMP1_Storage_Boxes::TAKE_ALL_OPTION && !can_take_all?(item) &&
            @take_window.take_all
        leave_open = false
      end
      
      if leave_open
        @take_window.activate
      else
        @take_window.deactivate.close
        @box_window.activate
      end
      refresh
    else
      Sound.play_buzzer
    end
  end
    
  
end # Scene_ItemStorage

#==============================================================================
# Window_BoxTitle
#==============================================================================
class Window_BoxTitle
  #--------------------------------------------------------------------------
  # Draws the contents.
  #--------------------------------------------------------------------------
  alias :non_limited_inventory_refresh :refresh unless $@
  def refresh(*args)
    non_limited_inventory_refresh(*args)
    if is_inventory_title?
      draw_inventory_amount
    end
  end
  #--------------------------------------------------------------------------
  # Displays how full the inventory is.
  #--------------------------------------------------------------------------
  def draw_inventory_amount
    if $imported[:Theo_LimInventory]
      text = "#{$game_party.total_inv_size}/#{$game_party.inv_max}"
      draw_text(0, 0, (Graphics.width/2)-32, line_height, text, 2)
    end
  end
  
end # Window_BoxTitle

#==============================================================================
# Window_TakeMore
#==============================================================================
class Window_TakeMore < Window_Command
  def take_lots
    @take_lots
  end
  
  def take_all
    @take_all
  end
end # Window_TakeMore


It still has an inconvenience in it: if all the items (of one type) in a box don't fit in the inventory, "take all" simply won't be seletable, instead of taking as many as possible. Maybe you can fix them some time.


Is it intentional or a bug that you cannot withdraw items with a size/weight of zero, if the inventory is already over-filled?



On an unrelated note: is there any particular reason, none of your scripts are in the Master Script List (on fandom)?

Have a nice week.
Am I doing something wrong? I didn't make any changes to the script.
Am I doing something wrong? I didn't make any changes to the script.
Looks like that may be a thing that was never tested. I tried to do what you did (which is in the script header) and I also got the error.
Adding in one line seems to fix the issue.
on line 587 add in next if amount == nil
Ruby:
  def fullness(box_id)
    i = 0
    box(box_id).each do |item, amount|
      next if amount == nil
      i += amount
    end
    return i
  end
Box size feature doesn't work for me, it shows 0/1 even when an item is stored. Maybe this has to do with the fix mentioned by Roninator2 above? Can anyone test this?
Seems that the script had some changes and now perhaps has an issue with registering the space used.
I was able to correct this, but it requires two things.
On line 334 add in these three lines
Ruby:
    attr_reader :items
    attr_reader :weapons
    attr_reader :armours
then on line 598 replace def fullness with this
Ruby:
  #--------------------------------------------------------------------------
  # Returns how full the box is.
  #--------------------------------------------------------------------------
  def fullness(box_id)
    i = 0
    items = @boxes[box_id].items
    weapons = @boxes[box_id].weapons
    armours = @boxes[box_id].armours
    items.each do |item, amount|
      i += amount
    end
    weapons.each do |item, amount|
      i += amount
    end
    armours.each do |item, amount|
      i += amount
    end
    return i
  end
the original code when checking the items stored would pull in the actual item not the hash key,value.
This separates the groups into individual lists and processes them.

Not sure if this was missed by IMP1 when rewriting the code from previous bug patches I addressed.
Oh, and there's also something wrong with the "Store All" button when you have more items in your inventory than the limit imposed on the box. The screen kind of freezes but you can still return by pressing Esc.
Change the update item selection. line 750ish
Ruby:
  def update_item_selection
    if input_cancel
      play_close_sound
      return_scene
    elsif input_ok
      move_item
    elsif input_switch
      Sound.play_cursor
      switch_over
    end
    if !@take_window.nil? && @take_window.open?
      @take_window.activate
      @bag_window.deactivate
      @box_window.deactivate
    elsif !@give_window.nil? && @give_window.open?
      @give_window.activate
      @bag_window.deactivate
      @box_window.deactivate
    end
  end
there also may be memory leaks (there are for me in testing)
change the following methods
*updated previous code made a new problem
Ruby:
  def offer_to_take_multiple(item)
    item_count = $game_boxes.item_number(item, @box_id)
    return false if item_count <= 0
    can_take_all = IMP1_Storage_Boxes::TAKE_ALL_OPTION &&
            item_count >= IMP1_Storage_Boxes::TAKE_ALL_THRESHOLD
    can_take_lots = IMP1_Storage_Boxes::TAKE_LOTS_OPTION &&
            item_count >= IMP1_Storage_Boxes::TAKE_LOTS_AMOUNT
    if can_take_all || can_take_lots
      if !@take_window.nil?
        @take_window.dispose
      end
      @take_window = Window_TakeMore.new(can_take_all, can_take_lots)
      @take_window.set_handler(:take_one, method(:take_one))
      @take_window.set_handler(:take_lots, method(:take_lots))
      @take_window.set_handler(:take_all, method(:take_all))
      @box_window.deactivate
      @take_window.activate
      return true
    end
    return false
  end

Ruby:
  def offer_to_give_multiple(item)
    item_count = $game_party.item_number(item)
    return false if item_count <= 0
    can_give_all = IMP1_Storage_Boxes::GIVE_ALL_OPTION &&
            item_count >= IMP1_Storage_Boxes::GIVE_ALL_THRESHOLD
    can_give_lots = IMP1_Storage_Boxes::GIVE_LOTS_OPTION &&
            item_count >= IMP1_Storage_Boxes::GIVE_LOTS_AMOUNT
    if can_give_all || can_give_lots
      if !@give_window.nil?
        @give_window.dispose
      end
      @give_window = Window_GiveMore.new(can_give_all, can_give_lots)
      @give_window.set_handler(:give_one, method(:give_one))
      @give_window.set_handler(:give_lots, method(:give_lots)) if can_give_lots
      @give_window.set_handler(:give_all, method(:give_all)) if can_give_all
      @bag_window.deactivate
      @give_window.activate
      return true
    end
    return false
  end

Also this to prevent from being able to control the window activation while using the transfer window.
line 805~ish
Ruby:
  #--------------------------------------------------------------------------
  # Move item from one side to the other.
  #--------------------------------------------------------------------------
  def move_item
    withdrawn = deposited = false
    if @box_window.active && can_move_item_to_inventory?(@box_window.item)
      item = @box_window.item
      return if offer_to_take_multiple(item)
      $game_boxes.transfer(item, 1, @box_id, :party)
      play_withdraw_sound(item)
      withdrawn = true
      @box_window.deactivate
    elsif @bag_window.active && can_move_item_to_box?(@bag_window.item)
      item = @bag_window.item
      return if offer_to_give_multiple(item)
      $game_boxes.transfer(item, 1, :party, @box_id)
      play_deposit_sound(item)
      deposited = true
      @bag_window.deactivate
    end
    if !(withdrawn || deposited)
      Sound.play_buzzer
    end
    refresh
  end

Original thread: https://forums.rpgmakerweb.com/threads/storage-boxes.27727/