[Ace] Sort Inventory (alphabetically, numerically, etc)

● ARCHIVED · READ-ONLY
Started by Rello 6 posts View original ↗
  1. Previously Attempts at Searching:

    • Google
    • Script Archives
    • Script Requests
    • "RPG Maker VX Ace Sorted Inventory"
    • "RPG Maker VX Ace Alphabetical Inventory"
    • "RPG Maker VX Ace Sort Items"
    • "RPG Maker VX Ace Alphabetize Items"

    Hey all,

    I'm (for the most part) using the default item script and was wondering if anyone would be willing to take a crack at writing something that would allow the player to sort the inventory, not into "categories", but by alphabetical order, ascending and descending numerical order and "default" order. The shift key would be pressed, opening the command prompt to pick from 5 options (the previous 4 and "cancel").

    Alphabetical => In alphabetical order. A,B,C,...X,Y,Z.

    Ascending Numerical => In numerical order, smallest to largest, 1,2,3...97,98,99.

    Descending Numerical => In numerical order, largest to smallest. 99,98,97...3,2,1.

    Default => In ID order, smallest to largest. ID:1,ID:2,ID:3...ID:97,ID:98,ID:99.

    In the case of a numerical order equivalence (you have 50 of two items), the order would be in alphabetical order (Alpha Potion x50, Iron Ore x50).

    As I said, I essentially use the default item scripts with some changes. Below will be my used scripts.

    Item List 3:

    Spoiler
    #==============================================================================# ** Window_ItemList

    #------------------------------------------------------------------------------

    # This window displays a list of party items on the item screen.

    #==============================================================================

    class Window_ItemList3 < Window_Selectable3

    #--------------------------------------------------------------------------

    # * Object Initialization

    #--------------------------------------------------------------------------

    def initialize(x, y, width, height)

    super

    self.windowskin = Cache.system("Window D-Side.png")

    @category = :none

    @data = []

    end

    #--------------------------------------------------------------------------

    # * Set Category

    #--------------------------------------------------------------------------

    def category=(category)

    return if @category == category

    @category = category

    refresh

    self.oy = 0

    end

    #--------------------------------------------------------------------------

    # * Get Digit Count

    #--------------------------------------------------------------------------

    def col_max

    return 8

    end

    #--------------------------------------------------------------------------

    # * Get Number of Items

    #--------------------------------------------------------------------------

    def item_max

    @data ? @data.size : 1

    end

    #--------------------------------------------------------------------------

    # * Get Item

    #--------------------------------------------------------------------------

    def item

    @data && index >= 0 ? @data[index] : nil

    end

    #--------------------------------------------------------------------------

    # * Get Activation State of Selection Item

    #--------------------------------------------------------------------------

    def current_item_enabled?

    enable?(@data[index])

    end

    #--------------------------------------------------------------------------

    # * Include in Item List?

    #--------------------------------------------------------------------------

    def include?(item)

    case @category

    when :item

    item.is_a?(RPG::Item) && !item.key_item?

    when :weapon

    item.is_a?(RPG::Weapon)

    when :armor

    item.is_a?(RPG::Armor)

    when :key_item

    item.is_a?(RPG::Item) && item.key_item?

    else

    false

    end

    end

    #--------------------------------------------------------------------------

    # * Display in Enabled State?

    #--------------------------------------------------------------------------

    def enable?(item)

    $game_party.usable?(item)

    end

    #--------------------------------------------------------------------------

    # * Create Item List

    #--------------------------------------------------------------------------

    def make_item_list

    @data = $game_party.all_items.select {|item| include?(item) }

    @data.push(nil) if include?(nil)

    end

    #--------------------------------------------------------------------------

    # * Restore Previous Selection Position

    #--------------------------------------------------------------------------

    def select_last

    select(@data.index($game_party.last_item.object) || 0)

    end

    #--------------------------------------------------------------------------

    # * Draw Item

    #--------------------------------------------------------------------------

    def draw_item(index)

    item = @data[index]

    if item

    rect = item_rect(index)

    rect.width -= 4

    draw_item_name2(item, rect.x + 12, rect.y + 2, enable?(item))

    draw_item_number2(rect, item)

    end

    end

    #--------------------------------------------------------------------------

    # * Draw Number of Items

    #--------------------------------------------------------------------------

    def draw_item_number(rect, item)

    draw_text(rect, sprintf(":%2d", $game_party.item_number(item)), 2)

    end

    def draw_item_number2(rect, item)

    self.contents.font.size = 20

    draw_text(rect.x-42, rect.y+8, 80, line_height, $game_party.item_number(item), 2)

    self.contents.font.size = 24

    end

    #--------------------------------------------------------------------------

    # * Update Help Text

    #--------------------------------------------------------------------------

    def update_help

    @help_window.set_item(item)

    end

    #--------------------------------------------------------------------------

    # * Refresh

    #--------------------------------------------------------------------------

    def refresh

    make_item_list

    create_contents

    draw_all_items

    end

    end
    Window Selectable 3:

    Spoiler
    #==============================================================================# ** Window_Selectable

    #------------------------------------------------------------------------------

    # This window class contains cursor movement and scroll functions.

    #==============================================================================

    class Window_Selectable3 < Window_Base

    #--------------------------------------------------------------------------

    # * Public Instance Variables

    #--------------------------------------------------------------------------

    attr_reader :index # cursor position

    attr_reader :help_window # help window

    attr_accessor :cursor_fix # fix cursor flag

    attr_accessor :cursor_all # select all cursors flag

    #--------------------------------------------------------------------------

    # * Object Initialization

    #-------------------------------------------------------------------------

    def initialize(x, y, width, height)

    super

    @index = -1

    @handler = {}

    @cursor_fix = false

    @cursor_all = false

    update_padding

    deactivate

    end

    #--------------------------------------------------------------------------

    # * Get Digit Count

    #--------------------------------------------------------------------------

    def col_max

    return 1

    end

    #--------------------------------------------------------------------------

    # * Get Spacing for Items Arranged Side by Side

    #--------------------------------------------------------------------------

    def spacing

    return 32

    end

    #--------------------------------------------------------------------------

    # * Get Number of Items

    #--------------------------------------------------------------------------

    def item_max

    return 0

    end

    #--------------------------------------------------------------------------

    # * Get Item Width

    #--------------------------------------------------------------------------

    def item_width

    (width - standard_padding * 2 + spacing) / col_max - spacing

    end

    #--------------------------------------------------------------------------

    # * Get Item Height

    #--------------------------------------------------------------------------

    def item_height

    32

    end

    #--------------------------------------------------------------------------

    # * Get Row Count

    #--------------------------------------------------------------------------

    def row_max

    [(item_max + col_max - 1) / col_max, 1].max

    end

    #--------------------------------------------------------------------------

    # * Calculate Height of Window Contents

    #--------------------------------------------------------------------------

    def contents_height

    [super - super % item_height, row_max * item_height].max

    end

    #--------------------------------------------------------------------------

    # * Update Padding

    #--------------------------------------------------------------------------

    def update_padding

    super

    update_padding_bottom

    end

    #--------------------------------------------------------------------------

    # * Update Bottom Padding

    #--------------------------------------------------------------------------

    def update_padding_bottom

    surplus = (height - standard_padding * 2) % item_height

    self.padding_bottom = padding + surplus

    end

    #--------------------------------------------------------------------------

    # * Set Height

    #--------------------------------------------------------------------------

    def height=(height)

    super

    update_padding

    end

    #--------------------------------------------------------------------------

    # * Change Active State

    #--------------------------------------------------------------------------

    def active=(active)

    super

    update_cursor

    call_update_help

    end

    #--------------------------------------------------------------------------

    # * Set Cursor Position

    #--------------------------------------------------------------------------

    def index=(index)

    @index = index

    update_cursor

    call_update_help

    end

    #--------------------------------------------------------------------------

    # * Select Item

    #--------------------------------------------------------------------------

    def select(index)

    self.index = index if index

    end

    #--------------------------------------------------------------------------

    # * Deselect Item

    #--------------------------------------------------------------------------

    def unselect

    self.index = -1

    end

    #--------------------------------------------------------------------------

    # * Get Current Line

    #--------------------------------------------------------------------------

    def row

    index / col_max

    end

    #--------------------------------------------------------------------------

    # * Get Top Row

    #--------------------------------------------------------------------------

    def top_row

    oy / item_height

    end

    #--------------------------------------------------------------------------

    # * Set Top Row

    #--------------------------------------------------------------------------

    def top_row=(row)

    row = 0 if row < 0

    row = row_max - 1 if row > row_max - 1

    self.oy = row * item_height

    end

    #--------------------------------------------------------------------------

    # * Get Number of Rows Displayable on 1 Page

    #--------------------------------------------------------------------------

    def page_row_max

    (height - padding - padding_bottom) / item_height

    end

    #--------------------------------------------------------------------------

    # * Get Number of Items Displayable on 1 Page

    #--------------------------------------------------------------------------

    def page_item_max

    page_row_max * col_max

    end

    #--------------------------------------------------------------------------

    # * Determine Horizontal Selection

    #--------------------------------------------------------------------------

    def horizontal?

    page_row_max == 1

    end

    #--------------------------------------------------------------------------

    # * Get Bottom Row

    #--------------------------------------------------------------------------

    def bottom_row

    top_row + page_row_max - 1

    end

    #--------------------------------------------------------------------------

    # * Set Bottom Row

    #--------------------------------------------------------------------------

    def bottom_row=(row)

    self.top_row = row - (page_row_max - 1)

    end

    #--------------------------------------------------------------------------

    # * Get Rectangle for Drawing Items

    #--------------------------------------------------------------------------

    def item_rect(index)

    rect = Rect.new

    rect.width = item_width

    rect.height = item_height

    rect.x = index % col_max * (item_width + spacing)

    rect.y = index / col_max * item_height

    rect

    end

    #--------------------------------------------------------------------------

    # * Get Rectangle for Drawing Items (for Text)

    #--------------------------------------------------------------------------

    def item_rect_for_text(index)

    rect = item_rect(index)

    rect.x += 4

    rect.width -= 8

    rect

    end

    #--------------------------------------------------------------------------

    # * Set Help Window

    #--------------------------------------------------------------------------

    def help_window=(help_window)

    @help_window = help_window

    call_update_help

    end

    #--------------------------------------------------------------------------

    # * Set Handler Corresponding to Operation

    # method : Method set as a handler (Method object)

    #--------------------------------------------------------------------------

    def set_handler(symbol, method)

    @handler[symbol] = method

    end

    #--------------------------------------------------------------------------

    # * Check for Handler Existence

    #--------------------------------------------------------------------------

    def handle?(symbol)

    @handler.include?(symbol)

    end

    #--------------------------------------------------------------------------

    # * Call Handler

    #--------------------------------------------------------------------------

    def call_handler(symbol)

    @handler[symbol].call if handle?(symbol)

    end

    #--------------------------------------------------------------------------

    # * Determine if Cursor is Moveable

    #--------------------------------------------------------------------------

    def cursor_movable?

    active && open? && !@cursor_fix && !@cursor_all && item_max > 0

    end

    #--------------------------------------------------------------------------

    # * Move Cursor Down

    #--------------------------------------------------------------------------

    def cursor_down(wrap = false)

    if index < item_max - col_max || (wrap && col_max == 1)

    select((index + col_max) % item_max)

    end

    end

    #--------------------------------------------------------------------------

    # * Move Cursor Up

    #--------------------------------------------------------------------------

    def cursor_up(wrap = false)

    if index >= col_max || (wrap && col_max == 1)

    select((index - col_max + item_max) % item_max)

    end

    end

    #--------------------------------------------------------------------------

    # * Move Cursor Right

    #--------------------------------------------------------------------------

    def cursor_right(wrap = false)

    if col_max >= 2 && (index < item_max - 1 || (wrap && horizontal?))

    select((index + 1) % item_max)

    end

    end

    #--------------------------------------------------------------------------

    # * Move Cursor Left

    #--------------------------------------------------------------------------

    def cursor_left(wrap = false)

    if col_max >= 2 && (index > 0 || (wrap && horizontal?))

    select((index - 1 + item_max) % item_max)

    end

    end

    #--------------------------------------------------------------------------

    # * Move Cursor One Page Down

    #--------------------------------------------------------------------------

    def cursor_pagedown

    if top_row + page_row_max < row_max

    self.top_row += page_row_max

    select([@index + page_item_max, item_max - 1].min)

    end

    end

    #--------------------------------------------------------------------------

    # * Move Cursor One Page Up

    #--------------------------------------------------------------------------

    def cursor_pageup

    if top_row > 0

    self.top_row -= page_row_max

    select([@index - page_item_max, 0].max)

    end

    end

    #--------------------------------------------------------------------------

    # * Frame Update

    #--------------------------------------------------------------------------

    def update

    super

    process_cursor_move

    process_handling

    end

    #--------------------------------------------------------------------------

    # * Cursor Movement Processing

    #--------------------------------------------------------------------------

    def process_cursor_move

    return unless cursor_movable?

    last_index = @index

    cursor_down (Input.trigger?:)DOWN)) if Input.repeat?:)DOWN)

    cursor_up (Input.trigger?:)UP)) if Input.repeat?:)UP)

    cursor_right(Input.trigger?:)RIGHT)) if Input.repeat?:)RIGHT)

    cursor_left (Input.trigger?:)LEFT)) if Input.repeat?:)LEFT)

    cursor_pagedown if !handle?:)pagedown) && Input.trigger?:)R)

    cursor_pageup if !handle?:)pageup) && Input.trigger?:)L)

    Sound.play_cursor if @index != last_index

    end

    #--------------------------------------------------------------------------

    # * Handling Processing for OK and Cancel Etc.

    #--------------------------------------------------------------------------

    def process_handling

    return unless open? && active

    return process_ok if ok_enabled? && Input.trigger?:)C)

    return process_cancel if cancel_enabled? && Input.trigger?:) B)

    return process_pagedown if handle?:)pagedown) && Input.trigger?:)R)

    return process_pageup if handle?:)pageup) && Input.trigger?:)L)

    end

    #--------------------------------------------------------------------------

    # * Get Activation State of OK Processing

    #--------------------------------------------------------------------------

    def ok_enabled?

    handle?:)ok)

    end

    #--------------------------------------------------------------------------

    # * Get Activation State of Cancel Processing

    #--------------------------------------------------------------------------

    def cancel_enabled?

    handle?:)cancel)

    end

    #--------------------------------------------------------------------------

    # * Processing When OK Button Is Pressed

    #--------------------------------------------------------------------------

    def process_ok

    if current_item_enabled?

    Sound.play_ok

    Input.update

    deactivate

    call_ok_handler

    else

    Sound.play_buzzer

    end

    end

    #--------------------------------------------------------------------------

    # * Call OK Handler

    #--------------------------------------------------------------------------

    def call_ok_handler

    call_handler:)ok)

    end

    #--------------------------------------------------------------------------

    # * Processing When Cancel Button Is Pressed

    #--------------------------------------------------------------------------

    def process_cancel

    Sound.play_cancel

    Input.update

    deactivate

    call_cancel_handler

    end

    #--------------------------------------------------------------------------

    # * Call Cancel Handler

    #--------------------------------------------------------------------------

    def call_cancel_handler

    call_handler:)cancel)

    end

    #--------------------------------------------------------------------------

    # * Processing When L Button (Page Up) Is Pressed

    #--------------------------------------------------------------------------

    def process_pageup

    Sound.play_cursor

    Input.update

    deactivate

    call_handler:)pageup)

    end

    #--------------------------------------------------------------------------

    # * Processing When R Button (Page Down) Is Pressed

    #--------------------------------------------------------------------------

    def process_pagedown

    Sound.play_cursor

    Input.update

    deactivate

    call_handler:)pagedown)

    end

    #--------------------------------------------------------------------------

    # * Update Cursor

    #--------------------------------------------------------------------------

    def update_cursor

    if @cursor_all

    cursor_rect.set(0, 0, contents.width, row_max * item_height)

    self.top_row = 0

    elsif @index < 0

    cursor_rect.empty

    else

    ensure_cursor_visible

    cursor_rect.set(item_rect(@index))

    end

    end

    #--------------------------------------------------------------------------

    # * Scroll Cursor to Position Within Screen

    #--------------------------------------------------------------------------

    def ensure_cursor_visible

    self.top_row = row if row < top_row

    self.bottom_row = row if row > bottom_row

    end

    #--------------------------------------------------------------------------

    # * Call Help Window Update Method

    #--------------------------------------------------------------------------

    def call_update_help

    update_help if active && @help_window

    end

    #--------------------------------------------------------------------------

    # * Update Help Window

    #--------------------------------------------------------------------------

    def update_help

    @help_window.clear

    end

    #--------------------------------------------------------------------------

    # * Get Activation State of Selection Item

    #--------------------------------------------------------------------------

    def current_item_enabled?

    return true

    end

    #--------------------------------------------------------------------------

    # * Draw All Items

    #--------------------------------------------------------------------------

    def draw_all_items

    item_max.times {|i| draw_item(i) }

    end

    #--------------------------------------------------------------------------

    # * Draw Item

    #--------------------------------------------------------------------------

    def draw_item(index)

    end

    #--------------------------------------------------------------------------

    # * Erase Item

    #--------------------------------------------------------------------------

    def clear_item(index)

    contents.clear_rect(item_rect(index))

    end

    #--------------------------------------------------------------------------

    # * Redraw Item

    #--------------------------------------------------------------------------

    def redraw_item(index)

    clear_item(index) if index >= 0

    draw_item(index) if index >= 0

    end

    #--------------------------------------------------------------------------

    # * Redraw Selection Item

    #--------------------------------------------------------------------------

    def redraw_current_item

    redraw_item(@index)

    end

    #--------------------------------------------------------------------------

    # * Refresh

    #--------------------------------------------------------------------------

    def refresh

    contents.clear

    draw_all_items

    end

    end
    Scene Item:

    Spoiler
    #==============================================================================# ** Scene_Item

    #------------------------------------------------------------------------------

    # This class performs the item screen processing.

    #==============================================================================

    class Scene_Item < Scene_ItemBase

    #--------------------------------------------------------------------------

    # * Start Processing

    #--------------------------------------------------------------------------

    def start

    super

    create_help_window

    create_category_window

    create_item_window

    end

    #--------------------------------------------------------------------------

    # * Create Category Window

    #--------------------------------------------------------------------------

    def create_category_window

    @category_window = Window_ItemCategory.new

    @category_window.viewport = @viewport

    @category_window.help_window = @help_window

    @category_window.y = @help_window.height

    @category_window.set_handler:)ok, method:)on_category_ok))

    @category_window.set_handler:)cancel, method:)return_scene))

    end

    #--------------------------------------------------------------------------

    # * Create Item Window

    #--------------------------------------------------------------------------

    def create_item_window

    wy = @category_window.y + @category_window.height

    wh = Graphics.height - wy

    @item_window = Window_ItemList3.new(0, wy, Graphics.width, wh)

    @item_window.viewport = @viewport

    @item_window.help_window = @help_window

    @item_window.set_handler:)ok, method:)on_item_ok))

    @item_window.set_handler:)cancel, method:)on_item_cancel))

    @category_window.item_window = @item_window

    end

    #--------------------------------------------------------------------------

    # * Category [OK]

    #--------------------------------------------------------------------------

    def on_category_ok

    @item_window.activate

    @item_window.select_last

    end

    #--------------------------------------------------------------------------

    # * Item [OK]

    #--------------------------------------------------------------------------

    def on_item_ok

    $game_party.last_item.object = item

    determine_item

    end

    #--------------------------------------------------------------------------

    # * Item [Cancel]

    #--------------------------------------------------------------------------

    def on_item_cancel

    @item_window.unselect

    @category_window.activate

    end

    #--------------------------------------------------------------------------

    # * Play SE When Using Item

    #--------------------------------------------------------------------------

    def play_se_for_item

    Sound.play_use_item

    end

    #--------------------------------------------------------------------------

    # * Use Item

    #--------------------------------------------------------------------------

    def use_item

    super

    @item_window.redraw_current_item

    end

    end
    If additional information is needed, I'd be happy to answer further questions. As always, any and all help is appreciated. Thank you.
  2. If I write a script I won't be considering those extra changes. Would you be able to take my script and incorporate it into your own menu?

    Though really, it is just a change in the order that the `@data` array is sorted.

    Since I am very lazy about GUI-related things, I'll just assign it to a pop-up window which will prompt the user to choose a sorting scheme.

    Someone else can write a better GUI for it.
  3. If you wrote a script for the default system, I'd be more than happy to incorporate it. :]
  4. As of right now, this works beautifully! Thank you. :]
  5. I just noticed the sort order is not preserved.

    I changed sort_order to a class variable. Now when you change scenes it's still preserved.