Requesting a little addon for IMP Storage Box script

Hi
I've been using a Storage Box script by IMP1 and needing a little feature or addon added to it. I want to able to create boxes with shared data between saves. Basically making a shared stash feature from RPGs, like Diablo2 or Titan Quest and many more.

Meaning that if a player stores there some equipment, then loads up another save, he will be able to withdraw the stored equipments put there with an another save.

Link to script
While it's not quite an add on to the storage box script, here's a script that most of what you're interested in:

https://lescripts.wordpress.com/rgss3/shared-storage-single-v1-00/

The shared storage is saved as a separate file in your game's folder, meaning that it will be accessible across save files, and even if you delete all your previous save files.
Thank you Vis_Mage. I've looked into it. Yeah, it is what I want, however, the script that I want the shared storage is more than that. It can be used as a storage box script, generate items and equipment in them, and with the add-ons for it, it has limited inventory, can deposit gold and also limit what types of types of equipment can it contain. Im using it for the loot system in my game (meaning that after battle the enemies won't give just automatically 3 items or more with other scripts to the player, but one of these containers open for the player with 8-10 items in it, then the player can take whatever he wants (of course if his or her inventory isnt full)), for the chests, for limit the inventory of the player, using it is a kinda realistic container for furniture (meaning that with the addons, if a player opens a drawer, he cannot put there swords and staffs and other big items, which realistically cannot be put into a small drawer, or if the player opens a cabinet, he or she can put inside it only clothes), and for a personal chest for the player, meaning he or she can store his or her equipment or any other loot he or she can find and want to store for later (for which I want the shared stash addon). That's why I cannot replace the script I want the shared storage for with the one you provided. But again, thank you anyway 🙂
Anyone?
Bump
Bump
Hey there,

Uh, does saving the boxes work for you in the first place? Haven't tested the script myself yet, but I'd expect every box to clear when you save the game, restart the application and load the savestate again.
(You might not have this problem if you are using Tsukihimes instanced item script or another one that saves the database item list in your savestates).

A similar problem might also occur if you use the
$game_box.setup( ... )
command (plus this should set up the box for every savestate at once until the application is exited).

Edit: See next post.

Apart from that, if you're using the default save system, should saving a savestate multiple times, dumping the inventory from one of them into the shared box and continuing with the other one be handled somehow? (If that's a problem for you?)
you should make your thread titles more meaningful. I thought this was going to be a repost of your other request, which you titled almost exactly the same, and started off the thread the same
https://forums.rpgmakerweb.com/inde...le-feature-for-this-script.84293/#post-818977

more specific titles would make it clear they're different requests.
@Shaz Sorry, renamed the title of the thread, so they don't have similar names.

@Another Fen I'm using Vlue's equipment randomization script, not Tsukihimes. Tested the boxes and the saves, and the items stay in the boxes, even after saving the game, quitting, then loading the save. And even if I use the $game_box.setup( ... ) command to generate the box, the items still stay in the boxes even after saving, quitting and loading.

As for if the player puts every item into a box, then saves and loads another save file and takes out all of the items put there, that's his or her call. I'm using Theo´s limited inventory script to limit the inventory, and the Storage box script itself to limit the box size, so the player cannot put unlimited items into his or her inventory nor his or her box. I'm gonna limit the storage box too using IMP1's Storage box script.
Uh, Sorry, I misinterpreted how the script worked when skimming it and my predictions in my last post were off...
Using $game_box.setup will not clear the items when you restart the game, but will reset the box's settings (name, size, ...) to default. So if you are using this command, I'd recommend to setup a box directly before you are opening it.

As for the other issue, it's still true that when you restart the game and load a savestate, the game will not recognize the items in the box as the ones in your database. If you put a Potion in a box, save, restart, load the game and put another Potion in the box, you'll notice that they will not stack for this reason.
This should only be a cosmetic issue though, the "false" Potion should still be added correctly to your parties inventory.

Vlue's equipment randomization saves weapons and armors with the savestate, so you shouldn't have this issue when storing these in your boxes.


Apart from that: Are you using other scripts that might affect the boxes or their contents?
I'm asking because Vlue's script already brings up the issue that between different savestates, multiple randomized items might share the same database ID (which would need to be resolved in a shared storage script). 🙂
I setup every box in the script itself, then before allowing the player to open the storage box, I just generate the loot that will appear in the box, using Galv's Random loot script.

As for the multiple same items, it doesn't bother me, as it is just a minor cosmetic issue, as you said.

Well, aside from the ones I already mentioned:

- Imp1 Item Sizes addon
- vFoggy Gold withdrawal addon (however this only adds the possibility of generating gold for the player to take out from the box, so this I think will not involve in the shared storage box, as the player cannot store gold into the boxes, just take out the generated ones)
- Galv's Random loot script with the variable modification from A-Moonless-Night (I'm using this to generate random loot, which is saved to a variable. I think this one will too not collide with the shared storage script, as I will not be generating items to the shared storage)

I think I'm not using more scripts that might affect the content of the boxes. I'm using a few bugfix scripts for Vlue's equipment randomization
( Fix for negative stat bonuses (The negative stat bonuses don't work in Vlue's script, this fixes that) and Equipment Parameter description (This just generates automatic descriptions for the items generated by Vlue's script). These 2 script addons shouldn't change anything drastic in the contents of the storage box. Also, here (its the 12. post), I requested a little feature for Vlue's script, which lets me set arrays for affix names, which will randomly choose a name for an affix (This little change maybe affect the box content, as in names of some random items???).

These are all I can think of that I'm using might affect the contents of the boxes in some way.
Hey there,

Sorry for the late response.
I tried my luck with a small addon, though as I'm a bit out of practise the result may be a bit more inflated than necessary... 😀
The script is split in two parts. The second part is for compatibility with Vlue's Equipment Randomization and can be removed if you ever decide to stop using it.

I haven't throughly tested the scripts yet, but I'll update the post if I find any overlooked issue tomorrow. 🙂

Spoiler
Code:
#=============================================================================
#
# * "Shared Storage Boxes" Addon for IMP1's "Storage Boxes" Script
#   Version 1.01 (February 2nd, 2018)
#   by Another Fen <forums.rpgmakerweb.com>
#
# Contents: ------------------------------------------------------------------
#   Allow certain storage boxes to be "shared" and carry over their contents
#   between all savestates. Boxes are updated whenever the game is saved.
#   Shared storage boxes can be configured below.
#
#   This script should be placed below IMP1's "Storage Boxes" script, which
#   can be found here:
#     http://forums.rpgmakerweb.com/index.php?/topic/27727-storage-boxes
#
#=============================================================================

# ---------------------------------------------------------------------------
#  IL_Shared_Storage
#    Handles Shared Box Saving.
# ---------------------------------------------------------------------------
module IL_Shared_Storage
 
  BOXES = [1, 2]                             # IDs of shared storage boxes
  NORMAL_FILENAME = "SharedStorage.rvdata2"  # Shared Storage Filename
  BACKUP_FILENAME = "SharedStorage.tmp"      # Backup Filename
 
 
  def load_shared_storage
    contents = load_shared_storage_data rescue nil
    BOXES.each_with_index do |box_id, index|
      $game_boxes.remove_all(box_id)
      next unless contents && contents[index]
      contents[index].each_item do |item, amount|
        $game_boxes.add_item(item, amount, box_id, :force)
      end
    end
  end
 
  def load_shared_storage_data
    if File.exists?(BACKUP_FILENAME)
      begin
        backup = File.open(BACKUP_FILENAME, "rb") { |file| Marshal.load(file) }
      rescue
        # Continue (Corrupt Backup)
      end
      if backup
        File.open(NORMAL_FILENAME, "wb") { |file| Marshal.dump(backup, file) }
        File.delete(BACKUP_FILENAME)
        return backup
      end
    end
    if File.exists?(NORMAL_FILENAME)
      return File.open(NORMAL_FILENAME, "rb") { |file| Marshal.load(file) }
    else
      return nil
    end
  end
 
  def save_shared_storage
    contents = BOXES.map do |box_id|
      next Storage_Box.new($game_boxes.box(box_id).each_pair)
    end
    save_shared_storage_data(contents) rescue nil
  end
 
  def save_shared_storage_data(contents)
    buffer = Marshal.dump(contents)
    File.open(BACKUP_FILENAME, "wb") { |file| file.write(buffer) }
    File.open(NORMAL_FILENAME, "wb") { |file| file.write(buffer) }
    File.delete(BACKUP_FILENAME)
  end
end


# ---------------------------------------------------------------------------
#  IL_Shared_Storage::Storage_Box
#   Holds serializable contents of a storage box
# ---------------------------------------------------------------------------
class IL_Shared_Storage::Storage_Box
 
  Item_Key = Struct.new(:type, :id)
 
  def initialize(enum = nil)
    @contents = Array.new
    enum.each { |item, amount| add(item, amount) }  if enum
  end
 
  def add(item, amount)
    @contents.push(key_for(item), amount)  if item
  end
 
  def each_item
    @contents.each_slice(2) { |key, amount| yield(item_for(key), amount) }
  end
 
  def key_for(item)
    case item
    when RPG::Item   then type = :item
    when RPG::Armor  then type = :armor
    when RPG::Weapon then type = :weapon
    else raise "Unexpected Item:\n#{item}"
    end
    return Item_Key.new(type, item.id)
  end
 
  def item_for(key)
    case key.type
    when :item   then set = $data_items
    when :armor  then set = $data_armors
    when :weapon then set = $data_weapons
    else raise "Unexpected Key:\n#{key}"
    end
    return set[key.id]
  end
end


# ---------------------------------------------------------------------------
#  class of DataManager *Modified*
#    Synchronize the Shared Storage on Load/Save
# ---------------------------------------------------------------------------
class << DataManager
  include IL_Shared_Storage
 
  alias_method(:create_game_objects_ILC_sharedStorage, :create_game_objects)
  def create_game_objects
    create_game_objects_ILC_sharedStorage
    load_shared_storage
  end
 
  alias_method(:load_game_ILC_sharedStorage, :load_game)
  def load_game(index)
    success = load_game_ILC_sharedStorage(index)
    load_shared_storage  if success
    return success
  end
 
  alias_method(:save_game_ILC_sharedStorage, :save_game)
  def save_game(index)
    success = save_game_ILC_sharedStorage(index)
    save_shared_storage  if success
    return success
  end
end
Spoiler
Code:
#=============================================================================
#
# * Shared Storage Boxes
#     Compatibility Patch for Vlue's Weapon/Armor Randomization Script
#   Version 1.01  (February 2nd, 2018)
#   by Another Fen <forums.rpgmakerweb.com>
#
# Contents: ------------------------------------------------------------------
#   Allows weapons and armors generated by Vlue's Weapon/Armor Randomization
#   script to be stored in and picked up from shared storage boxes.
#
#   Place this below the original and Vlue's Weapon/Armor Randomization script,
#   which can be found here:
#     https://forums.rpgmakerweb.com/index.php?threads/weapon-armor-randomization.14771/
#
# Known Issues: --------------------------------------------------------------
#   - So far, this script does not work with randomized non-equip items.
#   - Equipment may be ordered differently between different savestates.
#   - The script copies the criterions for stacking identical equipment from
#     Vlue's script V1.7 and might have to be adjusted if these change.
#   - Randomizing an already randomized item will leave it with an invalid
#     original_id for other savestates (not an issue by default).
#=============================================================================

# ---------------------------------------------------------------------------
#  RPG::BaseItem *Modified*
#    Remember generated items over different savestates.
# ---------------------------------------------------------------------------
class RPG::BaseItem
 
  attr_reader :shared_storage_id
 
  def generated?
    self.id != self.original_id
  end
 
  def setup_shared_storage_id
    # Must be explicitely set up before saving the item in the Shared Storage.
    # Used to identify the original item in a savestate (Saves the creation
    # of a new item and allows savestates to remember and utilize the items ID)
    @shared_storage_id ||= make_unique_identifier
  end

  def make_unique_identifier
    (Time.now.nsec << 52) + rand(1 << 52)
  end
 
  alias_method(:set_original_ILC_resetSharedStorage, :set_original)
  def set_original(original_id)
    set_original_ILC_resetSharedStorage(original_id)
    @shared_storage_id = nil  # This item is no longer the original
  end
 
  def same_item?(item)
    self.shared_storage_id && self.shared_storage_id == item.shared_storage_id
  end
end

#~ class RPG::Item
#~
#~   def same_item?(item)
#~     # Condition copied from $game_party.add_item (Vlue's Random Equip)
#~     super || (self.price == item.price && self.name == item.name)
#~   end
#~ end

class RPG::EquipItem
 
  def same_item?(item)
    # Condition copied from $game_party.add_weapon/armor (Vlue's Random Equip)
    super || (STACK_SAME_EQUIP && self.params == item.params &&
        self.price == item.price && self.name == item.name &&
        self.color == item.color)
  end
end


# ---------------------------------------------------------------------------
#  IL_Shared_Storage::Storage_Box *Modified*
#    Save items separately that might not be available in other savestates.
# ---------------------------------------------------------------------------
class IL_Shared_Storage::Storage_Box
 
  alias_method(:key_for_ILC_generatedItems, :key_for)
  def key_for(item)
    if item.generated?
      item.setup_shared_storage_id
      return Item_Key.new(:export, item)
    else
      return key_for_ILC_generatedItems(item)
    end
  end
 
  alias_method(:item_for_ILC_generatedItems, :item_for)
  def item_for(key)
    if key.type == :export
      return $game_party.import_item(key.id)
    end
    return item_for_ILC_generatedItems(key)
  end
end


# ---------------------------------------------------------------------------
#  Game_Party *Modified*
#    Import items from other savestates
# ---------------------------------------------------------------------------
class Game_Party
 
  def saved_item_set(item)
    case item
    when RPG::Weapon then return self.saved_weapons
    when RPG::Armor  then return self.saved_armors
    #when RPG::Item   then return ...  # Not saved elsewhere by default
    else return nil
    end
  end
 
  def import_item(item)
    # Search existing database for item:
    set = saved_item_set(item)
    return nil  unless set
    result = set.find { |other| other && other.same_item?(item) }
    return result  if result
    # Add Item if not found:
    item.id = set.length
    set.push(item)
    return item
  end
end
IT'S WONDERFUL!!! It works as it should, meaning you can create shareable storage boxes, put or take out items with all of your saves. Thank you Another Fen, I will definitely credit you in my game 🙂

Now, after some testings, here are a few thoughts:

It needs to be put under the Vlue randomization script, Imp1 Storage boxes script and the Shared Storage addon script, otherwise you will get errors. It is written into the compatibility script itself.

The only error I'm getting is when an item is generated with 2 prefixes and/or 2 suffixes. You put that item into the shared storage, then you try to take it out from the box. I described the problem in this thread (its the 5. post). I posted there a demo too. I don't want it to generate equipments with 2 prefixes and/or suffixes.

Oh, and I forgot to mention: There is an addon for Vlue equipment randomization script, which lets you make affixes for items too, along with the ability to add effects to equipments via notetags from any script, which lets you notetag equipments with some effects (like add movement speed, add thorn effect to armors, etc.) I myself use that addon for the notetag feature for now, and never tried the item affix feature for it.
Here is the script

The storage box and compatibility patch created by Another Fen works with the notetags feature from this script, tested it myself. However, as I mentioned, never tried the item affix feature, as I don't use that in my game for now.

I will edit this post in a few minutes with the error message it gives me when trying to get out an item from the storage box with 2 prefixes and/or 2 suffixes.

Heres the error:
Script 'Sixth Random item desc' line 117: NoMethodError occurred.
undefined method 'params' for nil:NilClass

However, I think that removing the bug described in my another post will remove this bug too.
Uh, right, the problem seems to be that every randomized item created also becomes eligible loot for Galv's script, meaning that the cases where you ended up with multiple prefixes were probably the ones where you looted one of your randomized items (which you then randomized a second time).

The error occurs because the item also ends up with an invalid "original id" for other savestates so far, and Sixth's Random Item Description script actually utilizes this value.
(to be continued if no one else is faster 🙂 )

Original thread: https://forums.rpgmakerweb.com/threads/requesting-a-little-addon-for-imp-storage-box-script.89775/