RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Help with code [KGC_CategorizeItem port attempt]

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 72
RMRK Junior
I've decided to try to understand some coding so that I can help myself a bit instead of making a request.

I'll be doing this one step at a time and I appreciate anyone's help.

[#1]
Can someone please explain what the below things are?
:item
:armor
:weapon

Does :armor mean "an array of all the armor listed in the armor tab in the database"? Or are they like flags that are used to identify something?

If the things are arrays, I want to create my own :string
If not, then what does Vocab::string mean?


EDIT:
Okay, I found out that the :item is a symbol and I have a fair idea of what it is.
I also found out that Vocab::item is just the string for item defined in the database.

I'm researching on how to create a symbol etc. I'm trying to store a list of things in a symbol. Any help would be appreciated.
« Last Edit: December 29, 2011, 01:19:40 AM by Seiryuki »

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
You are looking at symbols. They are basically unique immutable strings.
Check this little code snippet:
Code: [Select]
def p(*args); msgbox_p(*args) end # Because I am too lazy to write msgbox_p each time and so the code below will also work in XP and VX

# Symbol
p :foobar.object_id == :foobar.object_id
# Strings
p "foobar".object_id == "foobar".object_id
p "foo" << "bar"
p :foo << :bar # Throws an error

I also suggest reading http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/


 
I'm afraid you may not understand what calling Vocab::item means so I'll go through it just in case. (Should you know, then maybe it'll be useful to someone else ^_^
Let's first look at the definition of RPG::System::Terms as it is specified in the help file:
Code: [Select]
class RPG::System::Terms
  def initialize
    @basic = Array.new(8) {''}
    @params = Array.new(8) {''}
    @etypes = Array.new(5) {''}
    @commands = Array.new(23) {''}
  end
  attr_accessor :basic
  attr_accessor :params
  attr_accessor :etypes
  attr_accessor :commands
end
An RPG::System object has one RPG::System::Terms object you can retrieve by using the .terms method.
We typically have that an RPG::System object is read from Data/System.rvdata2 and stored in $data_system.
Once the data is loaded we can retrieve the terms by using $data_system.terms. Before that we cannot.

Let's head back and look at Vocab::item which is a method defined as the following: (context of module Vocab)
Code: [Select]
# item definition
def self.item
  command(4)
end
# Command method definition
def self.command(command_id)
  $data_system.terms.commands[command_id]
end
So there you have it. Vocab::item is basically short for $data_system.terms.commands[4]

What about Vocab::string then?
Well, try it out.

*hugs*
 - Zeriab

**
Rep:
Level 72
RMRK Junior
Hey, thanks Zeriab. That really helped a lot, especially the part about the Data/System.rvdata2.

See below for the script that explains what I'm trying to do. I think I've reached pretty far but I'm not getting a couple of core mechanics to work. I do appreciate it if anyone can take a look at the code and see where I'm going wrong.\
I'm trying to port KGC CategorizeItem script.
Spoiler for:
Code: [Select]
# RMVX Script by KGC
# http://f44.aaa.livedoor.jp/~ytomy/tkool/rpgtech/php/tech.php?tool=VX&cat=tech_vx/item&tech=categorize_item
# Ported to RMVXAce by Seiryuki on 2011.12.28


$data_system = load_data("Data/System.rvdata2") if $data_system == nil

module Seiryuki
module CategorizeItem
  #Automatically Categorize Items
  ENABLE_AUTO_CATEGORIZE = true
 
  #Duplicate Category Entries
  #Set to false, items can have multiple categories.
  #Set to true, items will be classified under the last tag (In the item
  #database "Notes")
  NOT_ALLOW_DUPLICATE = false
 
  # ? Coordinates of item category window. [ x, y ]
  CATEGORY_WINDOW_POS  = [0, 0]
 
  # ? Number of columns in the item category window.
  # Column width auto-adjusts to fit amount of columns specified.
  CATEGORY_WINDOW_COLUMNS   = 6
 
  # ? category window column spacer width.
  CATEGORY_WINDOW_COL_SPACE = 2
 
  # ? category window number of lines/rows of text.
  #Not working :(
#~   CATEGORY_WINDOW_LINE_NUMBER = 2
 
  # ? help window coordinates.
  HELP_WINDOW_POS = [0, 0]
 
  # ? help window number of lines/rows of text.
  HELP_WINDOW_LINE_NUMBER = 2
 
 
 
 
 
  #Set up all item categories here:
  CATEGORY_IDENTIFIER = [
    "Item",
    "Weapons",
    "Armor",
    "Key-Items",
    "HELP",
    "All Items",
  ]
 
  #Choose one from the CATEGORY_IDENTIFIER above,
  #but it's text-string must be present in the CATEGORY_NAME below.
  ITEM_DEFAULT_CATEGORY = "Item"
 
  #Set up visible item categories here:
  CATEGORY_NAME = {
    "Item"       =>:item,
    "Weapons"    =>:weapon,
    "Armor"      =>:armor,
    "Key-Items"  =>:key_item,
    "HELP"       =>:helpme,
    "All Items"  =>:all_items,
  }
 
  #Set up item category descriptions here:
  CATEGORY_DESCRIPTION = [
    "All items in possession.",
    "All weapons in possession.",
    "All armor in possession.",
    "All special and important items in possession.",
    "NEED HELP!!!!", 
    "ALL ITEMS",
  ]
 
 
end
end


$imported = {} if $imported == nil
$imported["CategorizeItem"] = true



module Seiryuki::CategorizeItem
 
  ITEM_DEFAULT_CATEGORY_INDEX = CATEGORY_IDENTIFIER.index(ITEM_DEFAULT_CATEGORY)
 
  RESERVED_CATEGORY_INDEX = {
    "All Items" => CATEGORY_IDENTIFIER.index("All Items"),
    "Items"     => CATEGORY_IDENTIFIER.index("Items"),
    "Weapons"   => CATEGORY_IDENTIFIER.index("Weapons"),
    "Armor"     => CATEGORY_IDENTIFIER.index("Armor"),
    "Key-Items" => CATEGORY_IDENTIFIER.index("Key-Items"),
  }
 
  #Whatever word(s) are after the separator ( | ) in the following lines are
  #what are used to determine what is searched for in the "Notes" section.
  module Regexp
    module BaseItem
      # Item Category tag string
      CATEGORY = /^<(?:CATEGORY|classification|category?)[ ]*(.*)>/i
    end
  end
end


class RPG::BaseItem
 
  def create_categorize_item_cache
    if @__item_category == nil || !Seiryuki::CategorizeItem::ENABLE_AUTO_CATEGORIZE
      @__item_category = []
    else
      @__item_category.compact!
    end

    self.note.split(/[\r\n]+/).each { |line|
      if line =~ Seiryuki::CategorizeItem::Regexp::BaseItem::CATEGORY
        #Category
        c = Seiryuki::CategorizeItem::CATEGORY_IDENTIFIER.index($1)
        @__item_category << c if c != nil
      end
    }
    if @__item_category.empty?
      @__item_category << Seiryuki::CategorizeItem::ITEM_DEFAULT_CATEGORY_INDEX
    elsif Seiryuki::CategorizeItem::NOT_ALLOW_DUPLICATE
      #Placed in the last specified category
      @__item_category = [@__item_category.pop]
    end
  end

  def item_category
    create_categorize_item_cache if @__item_category == nil
    return @__item_category
  end
 
end



class RPG::UsableItem < RPG::BaseItem
 
  def create_categorize_item_cache
    @__item_category = []
    if self.price == 0
      @__item_category << Seiryuki::CategorizeItem::RESERVED_CATEGORY_INDEX["Key-Items"]
    end
    super
  end
 
end



class RPG::Weapon < RPG::EquipItem
 
  def create_categorize_item_cache
    @__item_category = []
    @__item_category << Seiryuki::CategorizeItem::RESERVED_CATEGORY_INDEX["Weapons"]
    super
  end
 
end


class RPG::Armor < RPG::EquipItem
 
  def create_categorize_item_cache
    @__item_category = []
    @__item_category << Seiryuki::CategorizeItem::RESERVED_CATEGORY_INDEX["Armor"]
    type = nil
    case self.kind
    when 0
      type = "Shields"
    when 1
      type = "Head Gear"
    when 2
      type = "Body Gear"
    when 3
      type = "Accessories"
    end
    if type != nil
      @__item_category << Seiryuki::CategorizeItem::RESERVED_CATEGORY_INDEX[type]
    end
    super
  end
 
end



class Window_ItemList < Window_Selectable
 
  attr_reader   :category
 
  alias initialize_CategorizeItem initialize
  def initialize(x, y, width, height)
    @category = 0
    initialize_CategorizeItem(x, y, width, height)
  end

  def category=(value)
    @category = value
    refresh
  end

  alias include_CategorizeItem? include?
  def include?(item)
    return false if item == nil

    if @category == Seiryuki::CategorizeItem::RESERVED_CATEGORY_INDEX["All Items"]
      return true
    end

    result = include_CategorizeItem?(item)

    unless result
      if $imported["UsableEquipment"] && $game_party.item_can_use?(item)
        result = true
      end
    end

#~     unless $game_temp.in_battle
#~       result &= (item.item_category.include?(@category))
#~     end

    return result
  end
end




##################################################################
#================================================================#
#Window_ItemCategory ============================================#
#================================================================#
##################################################################
class Window_ItemCategory < Window_HorzCommand
 
  #This sets the max columns for the category window.
  alias item_category_col_max col_max
  def col_max
      return Seiryuki::CategorizeItem::CATEGORY_WINDOW_COLUMNS
  end
 
  #This sets the coordinates for the category window.
  def initialize
    super(Seiryuki::CategorizeItem::CATEGORY_WINDOW_POS[0],
    Seiryuki::CategorizeItem::CATEGORY_WINDOW_POS[1])
  end
 
  def make_command_list
    Seiryuki::CategorizeItem::CATEGORY_NAME.each { |name, symb|
    add_command(name, symb)
    }
  end
 
  def update_help
    @help_window.set_text(Seiryuki::CategorizeItem::CATEGORY_DESCRIPTION[self.index])
  end
end #class
#================================================================#





#================================================================#
#Window_HorzCommand
#================================================================#
class Window_HorzCommand < Window_Command
 
  #This overwrites spacing method
  alias item_category_spacing spacing
  def spacing
    return Seiryuki::CategorizeItem::CATEGORY_WINDOW_COL_SPACE
  end
 
  #This overwrites spacing method. Not working.
#~   alias item_category_visible_line_number visible_line_number
#~   def visible_line_number
#~     return Seiryuki::CategorizeItem::CATEGORY_WINDOW_LINE_NUMBER
#~   end
end #class
#================================================================#





#================================================================#
#Scene_Item
#================================================================#
class Scene_Item < Scene_ItemBase
 
 
  alias item_category_create_item_window create_item_window
  def create_item_window
    wy = @category_window.y + @category_window.height
    wh = Graphics.height - wy
    @item_window = Window_ItemList.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

  alias update_CategorizeItem update
  def update
    @category_window.update
    update_CategorizeItem

    if @category_window.active
      update_category_selection
    end
  end

  def update_category_selection
    unless @category_activated
      @category_activated = true
      return
    end

    if @last_category_index != @category_window.index
      @item_window.category = @category_window.index
      @item_window.refresh
      @last_category_index = @category_window.index
    end

    if Input.trigger?(Input::B)
      Sound.play_cancel
      return_scene
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      hide_category_window
    end
  end
 
end #class
#================================================================#






#================================================================#
#Window_Help
#================================================================#
class Window_Help < Window_Base

  alias item_category_help_initialize initialize
  def initialize(line_number = Seiryuki::CategorizeItem::HELP_WINDOW_LINE_NUMBER)
    super(Seiryuki::CategorizeItem::HELP_WINDOW_POS[0],
    Seiryuki::CategorizeItem::HELP_WINDOW_POS[1],
    Graphics.width, fitting_height(line_number))
  end

end #class
#================================================================#


For some reason, when navigating through categories in the Item Scene in the game, the categories sort of skip one (only the odd-numbered index of the category listing can be selected). Also, I'm not getting the notetags to work.
Using the script as it is above, I had added <category HELP> for the potion item in the RMVXAce database to see if the potion would show up in the HELP category. That is how I know the notetags are not working.

Thanks for any help from anyone.
Btw, although I'm a programmer, I know very little Ruby and so a few of the methods in the KGC script was copied without me knowing much about what they do, but they seemed important.
« Last Edit: December 29, 2011, 01:17:11 AM by Seiryuki »

**
Rep:
Level 72
RMRK Junior
IMPORTANT UPDATE:

I managed to do the port the script.
See here:
http://rmrk.net/index.php/topic,44753.0.html