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.

Item/Weapon/Armor Buy Back (1.0)

Item/Weapon/Armor Buy Back (1.0)
Version:1.0

Introduction
This script adds the option of buying back items/weapons/armors that you've previously sold by forming a list. You can edit the max size of the list, what types of items make it up, and the name of the "Buy Back" option on the shop menu.

Features
  • Choose the types of items eligible for rebuying
  • Choose max number of items kept in list
  • Optional saving of list

Screenshots
ItemBuyBack-1.png

Demo
It's not really necessary, but if you want it:
Demo

Script


Code:
#===========================================================
#
# Item/Weapon/Armor BuyBack v 1.0
# by U-Division
# give credit if used
#============================================================

#------------PURPOSE-------------------
#This script adds the option of buying back items/weapons/armors
#that you've previously sold by forming a list.  You can edit
#the max size of the list, what types of items make it up, and
#the name of the "Buy Back" option on the shop menu.

#------------INSTRUCTIONS-------------
#This is really easy to use.  Just paste the script above main
#but below the default shop scripts.  It will override them.
#To configure the script, look at the following sections.

#------------VERY IMPORTANT-----------
#As it is now, the Buy Back list will be lost when be saved, which
#some may people may find desirable.  If you're not one of those
#people, add the following to your save scripts in the appropriate
#places.

#For Saving---
 #   Marshal.dump($All_Sold, file)
 #   Marshal.dump($Buy_Back, file)
#For Loading--
 #   $All_Sold          = Marshal.load(file)
 #   $Buy_Back          = Marshal.load(file)
 
#------------DO NOT MODIFY----------------
$Buy_Back = []  #This will be filled with the current list of re-buyable items
$All_Sold = []  #This will be filled with the list of EVERYTHING you've sold
#-----------------------------------------

#------------MODIFY THESE IF DESIRED-----------------
BUY_BACK_WEAPONS = true #True if player can buy back weapons
BUY_BACK_ARMORS = true #True if player can buy back armors
BUY_BACK_ITEMS = false #True if player can buy back items
BUY_BACK_SIZE_LIMIT = true #Enables limiting of the number of items remembered
BUY_BACK_SIZE = 10 #Number of items the list remembers
BUY_BACK_NAME = "Buy Back" #Name of buyback option in shop menu
#-----------------------------------------

class Window_ShopCommand < Window_Selectable
# ------------------------------------
def initialize
 super(0, 64, 480, 64)
 self.contents = Bitmap.new(width - 32, height - 32)
 @item_max = 4
 @column_max = 4
 @commands = ["Buy", BUY_BACK_NAME, "Sell", "Exit"]
 refresh
 self.index = 0
 end
# ------------------------------------
 def refresh
 self.contents.clear
 for i in 0...@item_max
 draw_item(i)
 end
 end
# ------------------------------------ 
 def draw_item(index)
 x = 4 + index * 120
 self.contents.draw_text(x, 0, 128, 32, @commands[index])
 end
end

#==============================================================================
# ** Window_ShopNumber
#------------------------------------------------------------------------------
#  This window is for inputting quantity of items to buy or sell on the
#  shop screen.
#==============================================================================

class Window_ShopNumber < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 128, 368, 352)
    self.contents = Bitmap.new(width - 32, height - 32)
    @item = nil
    @max = 1
    @price = 0
    @number = 1
    @lock = false
  end
  #--------------------------------------------------------------------------
  # * Set Items, Max Quantity, and Price
  #--------------------------------------------------------------------------
  def set(item, max, price)
    @item = item
    @max = max
    @price = price
    @number = 1
    refresh
  end
  def lock
   @lock = true
 end
 def unlock
   @lock = false
   end
  #--------------------------------------------------------------------------
  # * Set Inputted Quantity
  #--------------------------------------------------------------------------
  def number
    return @number
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    draw_item_name(@item, 4, 96)
    self.contents.font.color = normal_color
    self.contents.draw_text(272, 96, 32, 32, "×")
    self.contents.draw_text(308, 96, 24, 32, @number.to_s, 2)
    self.cursor_rect.set(304, 96, 32, 32)
    # Draw total price and currency units
    domination = $data_system.words.gold
    cx = contents.text_size(domination).width
    total_price = @price * @number
    self.contents.font.color = normal_color
    self.contents.draw_text(4, 160, 328-cx-2, 32, total_price.to_s, 2)
    self.contents.font.color = system_color
    self.contents.draw_text(332-cx, 160, cx, 32, domination, 2)
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    if self.active
      if @lock == false
      # Cursor right (+1)
      if Input.repeat?(Input::RIGHT) and @number < @max
        $game_system.se_play($data_system.cursor_se)
        @number += 1
        refresh
      end
      # Cursor left (-1)
      if Input.repeat?(Input::LEFT) and @number > 1
        $game_system.se_play($data_system.cursor_se)
        @number -= 1
        refresh
      end
      # Cursdr up (+10)
      if Input.repeat?(Input::UP) and @number < @max
        $game_system.se_play($data_system.cursor_se)
        @number = [@number + 10, @max].min
        refresh
      end
      # Cursor down (-10)
      if Input.repeat?(Input::DOWN) and @number > 1
        $game_system.se_play($data_system.cursor_se)
        @number = [@number - 10, 1].max
        refresh
      end
    end
  end
  end
end









class Scene_Shop

# ------------------------------------
 def main
 @help_window = Window_Help.new
 @command_window = Window_ShopCommand.new
 @gold_window = Window_Gold.new
 @gold_window.x = 480
 @gold_window.y = 64
 @dummy_window = Window_Base.new(0, 128, 640, 352)
 @buy_window = Window_ShopBuy.new($game_temp.shop_goods)
 @buy_window.active = false
 @buy_window.visible = false
 @buy_window.help_window = @help_window
 @sell_window = Window_ShopSell.new
 @sell_window.active = false
 @sell_window.visible = false
 @sell_window.help_window = @help_window
 @number_window = Window_ShopNumber.new
 @number_window.active = false
 @number_window.visible = false
 @status_window = Window_ShopStatus.new
 @status_window.visible = false
 Graphics.transition
 loop do
 Graphics.update
 Input.update
 update
 if $scene != self
 break
 end
 end
 Graphics.freeze
 @help_window.dispose
 @command_window.dispose
 @gold_window.dispose
 @dummy_window.dispose
 @buy_window.dispose
 @sell_window.dispose
 @number_window.dispose
 @status_window.dispose
 end
# ------------------------------------
 def update
 @help_window.update
 @command_window.update
 @gold_window.update
 @dummy_window.update
 @buy_window.update
 @sell_window.update
 @number_window.update
 @status_window.update
 if @command_window.active
 update_command
 return
 end
 if @buy_window.active
 update_buy
 return
 end
 if @sell_window.active
 update_sell
 return
 end
 if @number_window.active
 update_number
 return
 end
 end
# ------------------------------------
 def update_command
 if Input.trigger?(Input::B)
 $game_system.se_play($data_system.cancel_se)
 $scene = Scene_Map.new
 return
 end
 if Input.trigger?(Input::C)
 case @command_window.index
 when 0
 @number_window.unlock
 $game_system.se_play($data_system.decision_se)
 @command_window.active = false
 @dummy_window.visible = false
 @buy_window = Window_ShopBuy.new($game_temp.shop_goods) 
 @buy_window.active = true
 @buy_window.visible = true
 @buy_window.refresh
 @status_window.visible = true
 when 1
 @number_window.lock
 $game_system.se_play($data_system.decision_se)
 @command_window.active = false
 @dummy_window.visible = false
 @buy_window = Window_ShopBuy.new($Buy_Back) 
 @buy_window.active = true
 @buy_window.visible = true
 @buy_window.refresh
 @status_window.visible = true
 when 2
   @number_window.unlock
 $game_system.se_play($data_system.decision_se)
 @command_window.active = false
 @dummy_window.visible = false
 @sell_window.active = true
 @sell_window.visible = true
 @sell_window.refresh
 when 3
 $game_system.se_play($data_system.decision_se)
 $scene = Scene_Map.new
 end
 return
 end
 end
# ------------------------------------ 
 def update_buy
 @status_window.item = @buy_window.item
 if Input.trigger?(Input::B)
 @number_window.unlock
 $game_system.se_play($data_system.cancel_se)
 @command_window.active = true
 @dummy_window.visible = true
 @buy_window.active = false
 @buy_window.visible = false
 @status_window.visible = false
 @status_window.item = nil
 @help_window.set_text("")
 @buying_back = false
 return
 end
 if Input.trigger?(Input::C)
 @item = @buy_window.item
 if @item == nil or @item.price > $game_party.gold
 $game_system.se_play($data_system.buzzer_se)
 return
 end
 case @item
 when RPG::Item
 number = $game_party.item_number(@item.id)
 when RPG::Weapon
 number = $game_party.weapon_number(@item.id)
 when RPG::Armor
 number = $game_party.armor_number(@item.id)
 end
 if number == 99
 $game_system.se_play($data_system.buzzer_se)
 return
 end
 $game_system.se_play($data_system.decision_se)
 max = @item.price == 0 ? 99 : $game_party.gold / @item.price
 max = [max, 99 - number].min
 if @buying_back == true
 max = 1
 if number == 99
 max = 0
 end 
 end
 @buy_window.active = false
 @buy_window.visible = false
 @number_window.set(@item, max, @item.price)
 @number_window.active = true
 @number_window.visible = true
 end
 end
# ------------------------------------
 def update_sell
 if Input.trigger?(Input::B)
 $game_system.se_play($data_system.cancel_se)
 @command_window.active = true
 @dummy_window.visible = true
 @sell_window.active = false
 @sell_window.visible = false
 @status_window.item = nil
 @help_window.set_text("")
 return
 end
 if Input.trigger?(Input::C)
 @item = @sell_window.item
 @status_window.item = @item
 if @item == nil or @item.price == 0
 $game_system.se_play($data_system.buzzer_se)
 return
 end
 $game_system.se_play($data_system.decision_se)
 case @item
 when RPG::Item
 number = $game_party.item_number(@item.id)
 when RPG::Weapon
 number = $game_party.weapon_number(@item.id)
 when RPG::Armor
 number = $game_party.armor_number(@item.id)
 end
 max = number
 @sell_window.active = false
 @sell_window.visible = false
 @number_window.set(@item, max, @item.price / 2)
 @number_window.active = true
 @number_window.visible = true
 @status_window.visible = true
 end
 end
# ------------------------------------ 
 def update_number
 if Input.trigger?(Input::B)
 $game_system.se_play($data_system.cancel_se)
 @number_window.active = false
 @number_window.visible = false
 case @command_window.index
 when 0
 @buy_window.active = true
 @buy_window.visible = true
 when 1
 @buy_window.active = true
 @buy_window.visible = true
 when 2
 @sell_window.active = true
 @sell_window.visible = true
 @status_window.visible = false
 end
 return
 end
 if Input.trigger?(Input::C)
 $game_system.se_play($data_system.shop_se)
 @number_window.active = false
 @number_window.visible = false
 case @command_window.index
 when 0
 $game_party.lose_gold(@number_window.number * @item.price)
 case @item
 when RPG::Item
 $game_party.gain_item(@item.id, @number_window.number)
 when RPG::Weapon
 $game_party.gain_weapon(@item.id, @number_window.number)
 when RPG::Armor
 $game_party.gain_armor(@item.id, @number_window.number)
 end
 @gold_window.refresh
 @buy_window.refresh
 @status_window.refresh
 @buy_window.active = true
 @buy_window.visible = true
 when 1 #WHEN BUYING BACK
 $game_party.lose_gold(@number_window.number * @item.price)
 case @item
 when RPG::Item
 $game_party.gain_item(@item.id, @number_window.number)
 when RPG::Weapon
 $game_party.gain_weapon(@item.id, @number_window.number)
 when RPG::Armor
 $game_party.gain_armor(@item.id, @number_window.number)
 end
 $Buy_Back[@buy_window.index]= nil 
 $Buy_Back.compact!
 @gold_window.refresh
 @buy_window.refresh
 @status_window.refresh
 @buy_window.active = true
 @buy_window.visible = true
 when 2
 $game_party.gain_gold(@number_window.number * (@item.price / 2))
 case @item
 when RPG::Item
 $game_party.lose_item(@item.id, @number_window.number)
 when RPG::Weapon
 $game_party.lose_weapon(@item.id, @number_window.number)
 when RPG::Armor
 $game_party.lose_armor(@item.id, @number_window.number)
end

#-----THIS UPDATES TEH BUY BACK LIST
 for i in 0 ... @number_window.number
 case @item
 when RPG::Weapon 
 a = 1
 b =[a,@item.id]
 $Buy_Back.push(b) if BUY_BACK_WEAPONS == true
 $All_Sold.push(b) if BUY_BACK_WEAPONS == true
 when RPG::Armor
 a = 2 
 b =[a,@item.id]
 $Buy_Back.push(b) if BUY_BACK_ARMORS == true
 $All_Sold.push(b) if BUY_BACK_ARMORS == true
 when RPG::Item 
 a = 0
 b =[a,@item.id]
 $Buy_Back.push(b) if BUY_BACK_ITEMS == true
 $All_Sold.push(b) if BUY_BACK_ITEMS == true
end

#----THIS LIMITS THE BUY BACK LIST
if BUY_BACK_SIZE_LIMIT ==true
  $Buy_Back[0] =nil if $Buy_Back.size > BUY_BACK_SIZE
  $Buy_Back.compact! 
end

 end
 @gold_window.refresh
 @sell_window.refresh
 @status_window.refresh
 @sell_window.active = true
 @sell_window.visible = true
 @status_window.visible = false
 end
 return
 end
 end
end

Instructions
This is really easy to use. Just paste the script above main but below the default shop scripts. It will override them. To configure the script, look at the following sections.

Compatibility
I assume it won't work with other Shop Menu Modifiers.

Credits and Thanks
Thanks to Kain Nobel for clearing up a technical problem.

Author's Notes
None at the moment.

Terms and Conditions

Give credit if used.
 

Rya

Member

Very cool. Great idea for a script. I don't know how many times in games I've accidentally sold something, and hadn't saved in a while. Quite useful for just such occasions.
 

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