じ☆ve冰风 发表于 2026-7-11 08:17:20

Sort items manually instead of by ID

Some time ago I found this post on another forum. I think it's a pretty common situation when making a game in RPG maker.

                                                        Ledai (translated) said:                                       
                        I don't know if this ever happened to you that by trying to save space in the database, specially on the items section, you try to leave some blank spaces so they get sorted by kind, but after a while there are more than you were expecting, so you have to include them at the end of the list. That causes a disorder on the menus, since by default the items are sorted by ID.               
Click to expand...


Trying to sort them back is inviable because that would mean you have to change every appearance they had in every event. That's why I made this script that allows to set a custom order in the windows instead. It's a pretty simple script, this time.

Author: Wecoc
Release Date: November 2016
Last Version: November 2016

Terms of Use
- Giving credits (Wecoc) for using this script is optional, but appreciated.
- You can repost this code.
- You can edit this code freely, and distribute the edits as well.
- This code can be used on commercial games.

Code

                Ruby:       
#==============================================================================# ** Custom Item Sorting#------------------------------------------------------------------------------#Author: Wecoc (no credits required)#------------------------------------------------------------------------------#This script allows to sort the items as the creator wants instead of by ID#as default. Even with that, they keep getting sorted like this:#   Items - Weapons - Armors#In other words, the sorting only affects those of the same type.##To do this, search on the start of the script these constants:#   SORTED_ITEMS, SORTED_WEAPONS, SORTED_ARMORS#and there define the sorting you want. The ID that are not included there#will appear in the end of the list by ID.#------------------------------------------------------------------------------# * To test this you can use this script call:#   $game_party.add_everything##That adds each item/weapon/armor to the party.#------------------------------------------------------------------------------#BEWARE! This script is not compatible with the IGOS system.#The script sorts the items in:#   Scene_Item, Scene_Battle (Items), Scene_Equip, Scene_Shop#If you use systems that modify the items, they may need to be adapted##Modified methods:#   Window_Item       :refresh#   Window_EquipItem:refresh#   Window_ShopBuy    :refresh#   Window_ShopSell   :refresh#==============================================================================module Custom_Sort   # Sort itemsSORTED_ITEMS =    # Sort weaponsSORTED_WEAPONS =    # Sort armorsSORTED_ARMORS = end#==============================================================================class Arraydef sort_ids(array)    return self if self.size == 0 or array.size == 0    name = self.class.name    r = self.map { |item| item.id }    result = []    for i in 0...array.size      result = nil    end    for i in 0...r.size      id = r ; result = id if array.include?(id)    end    result.compact!    for i in 0...r.size      id = r ; result.push(id) if !result.include?(id)    end    case name    when "RPG::Item"      result.map! { |id| $data_items }    when "RPG::Weapon"      result.map! { |id| $data_weapons }    when "RPG::Armor"      result.map! { |id| $data_armors }    end    return resultenddef sort_ids!(array)    self.replace(self.sort_ids(array))endend#==============================================================================class Game_Partydef add_everything(n=1)    for i in 1...$data_items.size      gain_item(i, n)   if $data_items.name != ""    end    for i in 1...$data_weapons.size      gain_weapon(i, n) if $data_weapons.name != ""    end    for i in 1...$data_armors.size      gain_armor(i, n)if $data_armors.name != ""    endendend#==============================================================================# ** Window_Item#==============================================================================class Window_Item < Window_Selectable#--------------------------------------------------------------------------# * Refresh#--------------------------------------------------------------------------def refresh    if self.contents != nil      self.contents.dispose      self.contents = nil    end    @data = []    data_items   = []    data_weapons = []    data_armors= []    # Add item    for i in 1...$data_items.size      if $game_party.item_number(i) > 0      data_items.push($data_items)      end    end    data_items.sort_ids!(Custom_Sort::SORTED_ITEMS)    # Also add weapons and items if outside of battle    unless $game_temp.in_battle      for i in 1...$data_weapons.size      if $game_party.weapon_number(i) > 0          data_weapons.push($data_weapons)      end      end      for i in 1...$data_armors.size      if $game_party.armor_number(i) > 0          data_armors.push($data_armors)      end      end      data_weapons.sort_ids!(Custom_Sort::SORTED_WEAPONS)      data_armors.sort_ids!(Custom_Sort::SORTED_ARMORS)    end    @data.concat(data_items)    @data.concat(data_weapons)    @data.concat(data_armors)    # If item count is not 0, make a bit map and draw all items    @item_max = @data.size    if @item_max > 0      self.contents = Bitmap.new(width - 32, row_max * 32)      for i in 0...@item_max      draw_item(i)      end    endendend#==============================================================================# ** Window_EquipItem#==============================================================================class Window_EquipItem < Window_Selectable#--------------------------------------------------------------------------# * Refresh#--------------------------------------------------------------------------def refresh    if self.contents != nil      self.contents.dispose      self.contents = nil    end    @data = []    data_weapons = []    data_armors= []    # Add equippable weapons    if @equip_type == 0      weapon_set = $data_classes[@actor.class_id].weapon_set      for i in 1...$data_weapons.size      if $game_party.weapon_number(i) > 0 and weapon_set.include?(i)          data_weapons.push($data_weapons)      end      end    end    # Add equippable armor    if @equip_type != 0      armor_set = $data_classes[@actor.class_id].armor_set      for i in 1...$data_armors.size      if $game_party.armor_number(i) > 0 and armor_set.include?(i)          if $data_armors.kind == @equip_type-1            data_armors.push($data_armors)          end      end      end    end    data_weapons.sort_ids!(Custom_Sort::SORTED_WEAPONS)    data_armors.sort_ids!(Custom_Sort::SORTED_ARMORS)    @data.concat(data_weapons)    @data.concat(data_armors)    # Add blank page    @data.push(nil)    # Make a bit map and draw all items    @item_max = @data.size    self.contents = Bitmap.new(width - 32, row_max * 32)    for i in 0...@item_max-1      draw_item(i)    endend#--------------------------------------------------------------------------# * Draw Item#   index : item number#--------------------------------------------------------------------------def draw_item(index)    item = @data    x = 4 + index % 2 * (288 + 32)    y = index / 2 * 32    case item    when RPG::Weapon      number = $game_party.weapon_number(item.id)    when RPG::Armor      number = $game_party.armor_number(item.id)    end    bitmap = RPG::Cache.icon(item.icon_name)    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24))    self.contents.font.color = normal_color    self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)    self.contents.draw_text(x + 240, y, 16, 32, ":", 1)    self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)end#--------------------------------------------------------------------------# * Help Text Update#--------------------------------------------------------------------------def update_help    @help_window.set_text(self.item == nil ? "" : self.item.description)endend#==============================================================================# ** Window_ShopBuy#==============================================================================class Window_ShopBuy < Window_Selectable#--------------------------------------------------------------------------# * Refresh#--------------------------------------------------------------------------def refresh    if self.contents != nil      self.contents.dispose      self.contents = nil    end    @data = []    data_items   = []    data_weapons = []    data_armors= []    for goods_item in @shop_goods      case goods_item      when 0 # Item      item = $data_items]      data_items.push(item) if item != nil      when 1 # Weapon      item = $data_weapons]      data_weapons.push(item) if item != nil      when 2 # Armor      item = $data_armors]      data_armors.push(item) if item != nil      end    end    data_items.sort_ids!(Custom_Sort::SORTED_ITEMS)    data_weapons.sort_ids!(Custom_Sort::SORTED_WEAPONS)    data_armors.sort_ids!(Custom_Sort::SORTED_ARMORS)    @data.concat(data_items)    @data.concat(data_weapons)    @data.concat(data_armors)    # If item count is not 0, make a bit map and draw all items    @item_max = @data.size    if @item_max > 0      self.contents = Bitmap.new(width - 32, row_max * 32)      for i in 0...@item_max      draw_item(i)      end    endendend#==============================================================================# ** Window_ShopSell#==============================================================================class Window_ShopSell < Window_Selectable#--------------------------------------------------------------------------# * Refresh#--------------------------------------------------------------------------def refresh    if self.contents != nil      self.contents.dispose      self.contents = nil    end    @data = []    data_items   = []    data_weapons = []    data_armors= []    for i in 1...$data_items.size      if $game_party.item_number(i) > 0      data_items.push($data_items)      end    end    for i in 1...$data_weapons.size      if $game_party.weapon_number(i) > 0      data_weapons.push($data_weapons)      end    end    for i in 1...$data_armors.size      if $game_party.armor_number(i) > 0      data_armors.push($data_armors)      end    end    data_items.sort_ids!(Custom_Sort::SORTED_ITEMS)    data_weapons.sort_ids!(Custom_Sort::SORTED_WEAPONS)    data_armors.sort_ids!(Custom_Sort::SORTED_ARMORS)    @data.concat(data_items)    @data.concat(data_weapons)    @data.concat(data_armors)    # If item count is not 0, make a bitmap and draw all items    @item_max = @data.size    if @item_max > 0      self.contents = Bitmap.new(width - 32, row_max * 32)      for i in 0...@item_max      draw_item(i)      end    endendend


Spoiler: Related script - Sort items as the player wantsI made some time ago a script that allows the players to sort the items as they want. That one is a bit rudimentary but it may interest you as well, so I include it here with the same terms of use. Since they modify the same they're not compatible, but it shouldn't be very hard to combine them.

To use this script you must be in Scene Item (the items menu of the game) and while pressing Z, trigger the directional arrows to move the selected item arround. The new sorting gets saved. This way if the player uses the same kind of potions very often they doesn't need to search it each time.

You can find it here:
- Sort items as the player wants
- Expanded version with both items and skills
Spoiler: FAQCan this be done with other lists of the Database? (skills, actors...)
Yes, it would be basically the same, but this script only works with items. If someone needs a different type ask it here and I'll try to provide it. That being said, some lists are more compatible to change than others, it depends on other custom scripts you might be using. Feel free to take my script as a reference to make your own changes in the game.

I never know what to say at the end, I think I need a catchphrase.


本贴来自国际rpgmaker官方论坛作者:Wecoc处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/sort-items-manually-instead-of-by-id.138364/
页: [1]
查看完整版本: Sort items manually instead of by ID