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.

Storage System

Hi again.

In my game, I want to be able to store certain items in a database/bank location, so unwanted items don't accumulate in the player's inventory.

All I want to know is if it is possible to accomplish this by eventing, or is scripting knowledge required?
 

poccil

Sponsor

The following illustrates how a storage system can be implemented.  Please note that the code below is not tested and there is no user interface for the storage system.  The ItemStorage class has three methods:

deposit(item,count=1) - Deposits an item (RPG::Item, RPG::Weapon, RPG::Armor) to storage.
withdraw(item,count=1) - Withdraws an item from storage
quantity(item) - Determines the amount of a specified item in storage.
each - Iterates through each item in storage

Constants:

MAXSIZE - Maximum different items that can be stored.
MAXPERSLOT - Maximum quantity per item slot.

Code:
module ItemStorageHelper
 def self.pbQuantity(items,maxsize,item)
  ret=0
  for i in 0...maxsize
   itemslot=items[i]
   if itemslot && itemslot[0]==item
    ret+=itemslot[1]
   end
  end
  return ret
 end
 def self.pbDeleteItem(items,maxsize,item,qty)
  raise "Invalid value for qty: #{qty}" if qty<0
  return true if qty==0
  ret=false
  for i in 0...maxsize
   itemslot=items[i]
   if itemslot && itemslot[0]==item
    amount=[qty,itemslot[1]].min
    itemslot[1]-=amount
    qty-=amount
    items[i]=nil if itemslot[1]==0
    if qty==0
     ret=true
     break
    end
   end
  end
  items.compact!
  return ret
 end
 def self.pbCanStore?(items,maxsize,maxPerSlot,item,qty)
  raise "Invalid value for qty: #{qty}" if qty<0
  return true if qty==0
  for i in 0...maxsize
   itemslot=items[i]
   if !itemslot
    qty-=[qty,maxPerSlot].min
    return true if qty==0
   elsif itemslot[0]==item && itemslot[1]<maxPerSlot
    newamt=itemslot[1]
    newamt=[newamt+qty,maxPerSlot].min
    qty-=(newamt-itemslot[1])
    return true if qty==0
   end
  end
  return false
 end
 def self.pbStoreItem(items,maxsize,maxPerSlot,item,qty)
  raise "Invalid value for qty: #{qty}" if qty<0
  return true if qty==0
  for i in 0...maxsize
   itemslot=items[i]
   if !itemslot
    items[i]=[item,[qty,maxPerSlot].min]
    qty-=items[i][1]
    return true if qty==0
   elsif itemslot[0]==item && itemslot[1]<maxPerSlot
    newamt=itemslot[1]
    newamt=[newamt+qty,maxPerSlot].min
    qty-=(newamt-itemslot[1])
    itemslot[1]=newamt
    return true if qty==0
   end
  end
  return false
 end
end
####################################
####################################

class GenericItem
 attr_reader :id,:type
 def initialize(id,type)
  @id=id
  @type=type
 end
 def ==(other)
  return other.id==@id && other.type==@type
 end
end

class ItemStorage
 MAXSIZE=100
 MAXPERSLOT=99
 def initialize
  @items=[]
 end
 def each
  @items.each {|i|  yield i  }
 end
 def toGenericItem(item)
  case item
    when RPG::Item
      return GenericItem(item.id,0)
    when RPG::Weapon
      return GenericItem(item.id,1)
    when RPG::Armor
      return GenericItem(item.id,2)
    else
      return nil
  end
 end
 # _item_ is RPG::Item, RPG::Weapon, or RPG::Armor
 def quantity(item)
  genericItem=toGenericItem(item)
  return ItemStorageHelper.pbQuantity(@items,MAXSIZE,genericItem(item))
 end 
 # _item_ is RPG::Item, RPG::Weapon, or RPG::Armor
 def deposit(item,count=1)
  genericItem=toGenericItem(item)
  count=[$game_party.item_number(item),count].min
  if count>0
   $game_party.lose_item(item,count)
   return ItemStorageHelper.pbStoreItem(
     @items,MAXSIZE,MAXPERSLOT,genericItem,count)
  end
 end
 # _item_ is RPG::Item, RPG::Weapon, or RPG::Armor
 def withdraw(item,count=1)
  genericItem=toGenericItem(item)
  count=[quantity(item),count].min
  if count>0
   $game_party.gain_item(item,count)
   return ItemStorageHelper.pbDeleteItem(
     @items,MAXSIZE,toGenericItem(item),count)
  end
 end
end
 

Thank you for viewing

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

Discord

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

Content

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