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.

Permanently changing item price through script?

My characters keep the same SP/MP max/min throughout the entire game, so I want the price of all the SP/MP recovery items to increase at some points in the game.

How would I do this through a script? Increase some kind of price variable for an item? Is this possible?

Thanks!!
 
This would be it, though it changes it permanently so beware of that fact.

Code:
$data_items[1].price = 100
$data_weapons[1].price = 100
$data_armors[1].price = 100

Just FYI, this was taken from a post by gotcha16.
 
That will temporarily change the price for the rest of the current session. If you save, quit & continue, the price will be back to what it was set in the database.

to change it permanently, you would need to save it back to the Items.rxdata file. Keep in mind that this ACTUALLY changes it permanently. Meaning that if the user starts a new game, it will still be at the new price.

Code:
$data_items[1].price = 100
save_data($data_items, "Data/Items.rxdata")
 
What you can do instead of what Brewmeister suggested is search for $Scene_File (or somesuch, it's been a while since I looked at RMXP's defaults). You should find two long lists, one saving and one loading. Add on a line at the bottom of each by copy/pasting the line above and changing the new line's file name from "Game_Actors" or whatever to "Data_Items" (if that doesn't work, try "Game_Items").

If you do that for both the saving and loading lists, the game will save a copy of the rxdata to the save file, and your item prices will be set in stone--for that one save.

If this is for VX, let me know and I can just whip up some aliased code for you to plug 'n play as a seperate script.
 
Hmm, that permanent changing sounds dangerous.

Would it be possible to put in a script somewhere something like:

$data_items[1].price = 4 + (4 * $game_variables[1])

?

And the game variable would increase throughout the game. Unless I'm missing something, I think that would work? If you put it somewhere in a script that the line is loaded upon start up. Maybe in Main? I'm not sure.

What do you think?

@meph: I doubt he'll let me use that for free. :3~
 

bukai

Member

l made a script, paste it above main and read the comments.

Code:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# â–ˆ Change Item Prices
# Credits: bukai aka spk
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ID = ID of the Item. Either Weapon, Armor or Item. 
# PRICE = New Price
# TYPE
# 0 = Item
# 1 = Weapon
# 2 = Armor
#
#
# Available methods,
# Callscript:
#    $cost.new_price(ID, PRICE, TYPE)  # set new price for an item
#    $cost.add_price(ID, PRICE, TYPE)  # increase the price by X
#    $cost.sub_price(ID, PRICE, TYPE)  # decrease the price by X
#
#=======================================================================
class PriceChange  
  attr_accessor :id
  attr_accessor :price
  attr_accessor :type

def initialize
    @id = id
    @price = price
    @type = type
  end
  
def new_price(id, price, type)  
  case type 
   when 0
   $data_items[id].price = price    
   when 1
   $data_weapons[id].price = price
   when 2
   $data_armors[id].price = price
 else
   p "INVALID INPUT"
  end   
end

def add_price(id, price, type)
  case type 
   when 0
   $data_items[id].price += price    
   when 1
   $data_weapons[id].price += price
   when 2
   $data_armors[id].price += price
 else
   p "INVALID INPUT"
  end   
end

def sub_price(id, price, type)
  case type 
   when 0
   $data_items[id].price -= price    
   when 1
   $data_weapons[id].price -= price
   when 2
   $data_armors[id].price -= price
 else
   p "INVALID INPUT"
  end   
 end


end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
# Call Class
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Scene_Title
  alias bukai_main main
  def main
    $cost = PriceChange.new
    bukai_main
  end
end

class Scene_Save < Scene_File
  def write_save_data(file)
    # Make character data for drawing save file
    characters = []
    for i in 0...$game_party.actors.size
      actor = $game_party.actors[i]
      characters.push([actor.character_name, actor.character_hue])
    end
    # Write character data for drawing save file
    Marshal.dump(characters, file)
    # Wrire frame count for measuring play time
    Marshal.dump(Graphics.frame_count, file)
    # Increase save count by 1
    $game_system.save_count += 1
    # Save magic number
    # (A random value will be written each time saving with editor)
    $game_system.magic_number = $data_system.magic_number
    # Write each type of game object
    Marshal.dump($game_system, file)
    Marshal.dump($game_switches, file)
    Marshal.dump($game_variables, file)
    Marshal.dump($game_self_switches, file)
    Marshal.dump($game_screen, file)
    Marshal.dump($game_actors, file)
    Marshal.dump($game_party, file)
    Marshal.dump($game_troop, file)
    Marshal.dump($game_map, file)
    Marshal.dump($game_player, file)
    Marshal.dump($cost, file)
    Marshal.dump($data_items, file)
    Marshal.dump($data_weapons, file)
    Marshal.dump($data_armors, file)
  end
end

class Scene_Load < Scene_File
  #--------------------------------------------------------------------------
  # * Read Save Data
  #     file : file object for reading (opened)
  #--------------------------------------------------------------------------
  def read_save_data(file)
    # Read character data for drawing save file
    characters = Marshal.load(file)
    # Read frame count for measuring play time
    Graphics.frame_count = Marshal.load(file)
    # Read each type of game object
    $game_system        = Marshal.load(file)
    $game_switches      = Marshal.load(file)
    $game_variables     = Marshal.load(file)
    $game_self_switches = Marshal.load(file)
    $game_screen        = Marshal.load(file)
    $game_actors        = Marshal.load(file)
    $game_party         = Marshal.load(file)
    $game_troop         = Marshal.load(file)
    $game_map           = Marshal.load(file)
    $game_player        = Marshal.load(file)
    $cost               = Marshal.load(file)
    $data_items         = Marshal.load(file)
    $data_weapons       = Marshal.load(file)
    $data_armors        = Marshal.load(file)
    # If magic number is different from when saving
    # (if editing was added with editor)
    if $game_system.magic_number != $data_system.magic_number
      # Load map
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
    end
    # Refresh party members
    $game_party.refresh
  end
end
l don't know any bugs.

Hope it can help you.

edit: now the prices are permanent.
 
@bukai: This seems to be a way too complicated way... actually...

Twin Matrix":118m97fj said:
$data_items[1].price = 4 + (4 * $game_variables[1])

... I think this is the most efficient way: Just add a variable's value to the base price somewhere in Scene_Shop and whereever else you have items handled (I'd suggest initialize definition). You could script something like an algorythm to go through each item and add a specifically stored value... for example from my Database Flag Script :p Only for fixed variables though... if you really want constantly varying prizes, do it through an array's index, aka:

Code:
@prices = [100, 30, 4]
Code:
for item in $data_items
  item.price = @prices[item.index]
end

Same for weapons and armor, if you need that... should work, but is untested ^^

EDIT: You should make sure your @prices array is saved... you could for example store it in Game_System and put an attr_accessor for the array in that class.
 
So many people, doing things the hard way. :haha:

Code:
class Game_System
  attr_reader :item_price_changes
  attr_reader :weapon_price_changes
  attr_reader :armor_price_changes
  alias_method :seph_changesprice_gmsys_init, :initialize
  def initialize
    seph_changesprice_gmsys_init
    @item_price_changes = {}
    @weapon_price_changes = {}
    @armor_price_changes = {}
  end
end

class RPG::Item
  alias_method :seph_changesprice_item_price, :price
  def price
    if $game_system != nil && $game_system.item_price_changes != nil &&
        $game_system.item_price_changes.has_key?(@id)
      return $game_system.item_price_changes[@id]
    end
    return seph_changesprice_item_price
  end
end

class RPG::Weapon
  alias_method :seph_changesprice_weapon_price, :price
  def price
    if $game_system != nil && $game_system.weapon_price_changes != nil &&
        $game_system.weapon_price_changes.has_key?(@id)
      return $game_system.weapon_price_changes[@id]
    end
    return seph_changesprice_weapon_price
  end
end

class RPG::Armor
  alias_method :seph_changesprice_armor_price, :price
  def price
    if $game_system != nil && $game_system.armor_price_changes != nil &&
        $game_system.armor_price_changes.has_key?(@id)
      return $game_system.armor_price_changes[@id]
    end
    return seph_changesprice_armor_price
  end
end

Now just use:
Code:
i = $game_system.item_price_changes
i[item_id] = n
# or
i = $game_system.weapon_price_changes
i[weapon_id] = n
# or
i = $game_system.armor_price_changes
i[armor_id] = n

Try that. ;)

(And even that was overkill lol)
 
Good to see you too Brew. I am sure Dargor can testify to this, once you script 1000 scripts, you learn all sorts of tricks you can just use over and over again.

But Twin, isn't the Virtual Database already in your project? ;)

Though it's not as simple as the script above, it can be used for all those $data array objects.
Code:
#      object = $game_virtualdb.data_item(item_id)
#      object.price = n
#      objects = $game_virtualdb.data_items
#      objects[item_id] = object
#      $game_virtualdb.data_items = objects
 

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