Trouble with menu-creation-part of my script [RGSS3]

Hello ladies and gentlemen,

I feel a little bit bad about to start with this thread as my first post in this forum but I really need some help of a good scripter (because i'm not one yet xD).

Well, my situation is as follows:
I'm my actual project which I'm working on, keys are a central part of the game.
During the whole game the player has to get many different keys to open some doors and release prisoned characters to progress further in the story. However, I know it would be possible to simple solve this with usage of items in the database for the keys. The thing is, with the number of the keys i need it won't be so nice to do that like this. I don't wanna see the inventory just filled with keys. Espescially because i'm using the LimitedInventory Script and in this case every single item uses at least one slot.

Because of that I came accross with the idea to simply create a new script for that.
So I thought about a "Keysystem" script in which you first can define all the keys which you need in your game.
After that you have the option to use them with script calls. I defined methods like give, take, set (sets the amount) and posses? (checking if the party actually possess the item)

However, the main functions are working properly as of now.
I have to say this is my first "advanced" script and not just something like ten lines or something.
At the moment I'm facing a problem by coding the menu part of the script.
Obviously it would be bad if the player can't check which keys he actually has so a correspondingly menu is necessary.
I just have trouble with things like saving the position for cursor memorization, displaying all the keys (with the right type)
or better said struggle with the menu in general.

Can someone of you maybe help me out here?
This thing just keeps me completely, so I would be very grateful.

Btw: If you find uncleanliness or other issues with my code in general please let me now so I can improve 😉

Here is the full script code:
Code:
# =============================================================================
# Redclaw - Key System
# Version : 0.1 Alpha
# Contact : www.rpgmakerstudio.org [email hidden]
# (English Documentation)
# =============================================================================
($imported ||= {})[:Red_keysystem] = true
# =============================================================================
# Changelog:
# -----------------------------------------------------------------------------
# 2017.02.24 - Creation of the script
# =============================================================================
 
module SC #Module for shortcuts
BRK = "\r\n" #Shortcut for newline
end
 
module REDCLAW
module KEYSYS #REDCLAW::KEYSYS
module CONFIG
#Changeable settings for the script should be located here
MENU_BACKGROUND_IMAGE = "Titles1/Book.png" #The path (after Graphics/) of the background image
end #REDCLAW::KEYSYS::CONFIG
module VOCAB #REDCLAW::KEYSYS::VOCAB
 
MENU_BUTTON_NAME = "Keys and Cards" #The name which will be used to display the button in the main menu.
MENU_WINDOW_TITLE = MENU_BUTTON_NAME
end #VOCAB
 
TYPES = []
TYPES[0] = {
"name" => "Key",
"id" => 1
}
 
TYPES[1] = {
"name" => "Card",
"id" => 2
}
 
KEYS = []
KEYS[0] = {
"name" => "Masterkey", #The name of the key which will be displayed in the menu
"type" => 1, #The Id of TYPES
"id" => 1, #Id for this keyitem
"icon" => 1, #Icon to display (number of the icon, look it up in your database) 
"description" => "The best key at all, you are able to open any door with it.", #The description which will be used to be displayed in the menu
"consumable" => false, #Defines if the player loses the keyitem on usage
"amount" => 0 #Amount of keys the player will get in a new game. - Usually you should set this on 0 because the player shouldn't spawn with it
 }
 
 KEYS[1] = {
"name" => "Entry ticket", #The name of the key which will be displayed in the menu
"type" => 2, #The Id of TYPES
"id" => 2, #Id for this keyitem
"icon" => 1, #Icon to display (number of the icon, look it up in your database) 
"description" => "A ticket to get in the holy special entry.", #The description which will be used to be displayed in the menu
"consumable" => true, #Defines if the player loses the keyitem on usage
"amount" => 0 #Amount of keys the player will get in a new game. - Usually you should set this on 0 because the player shouldn't spawn with it
 }
end #KEYSYS
end #REDCLAW
 
class Keysystem
 
attr_accessor :last_key
def initialize()
@all_items = [] #Array including all items (from the keysystem - not regular items)
@all_types = [] #Array including all types
@last_key = {"key" => nil,"type" => nil} #hash containing id and type of the last used key for cursor memorization
 
REDCLAW::KEYSYS::KEYS.each do |item_key| #initializing all the item objects in the array
@all_items.push(Item.new(item_key["name"],item_key["type"],item_key["id"],item_key["icon"],item_key["description"],item_key["consumable"],item_key["amount"]))
end
 
REDCLAW::KEYSYS::TYPES.each do |item_type| #initializing all the type objects in the array
@all_types.push(Type.new(item_type["name"],item_type["id"]))
end
end
 
def get_items()
  return @all_items
  end
 
def get_item(id)
  return @all_items[id-1]
end
 
def get_types
return @all_types
end
 
def typename(id)
return get_types[id-1].name
end
 
def typesym(id)
typename(id).to_sym
end
 
def typesym_id(sym)
sym = sym.to_s
id = 0
get_types.each do |type|
if type.name == sym
id = type.id
end
end
return id
end
 
def possess?(id,takeitem = false) #use this method to check if the player actually possesses the item.
if @all_items[id-1].amount > 0
if @all_items[id-1].consumable && takeitem
@all_items[id-1].amount -= 1
end
return true
else
return false
end
end
 
def exhibit(id) #shortcut for check and remove an item (if the player posseses it)
possess?(id,true)
  end
 
 
def take(id,take_amount)
@all_items[id-1].amount -= take_amount if(posess?(id,true) == true)
end
 
def give(id,give_amount)
@all_items[id-1].amount += give_amount
end
 
def set(id,set_amount)
@all_items[id-1].amount = set_amount
end
 
def report(id,showmsg = false) #Report method for testing purposes
text = ""
text << ("Name: " + get_item(id).name + SC::BRK)
text << ("Type: " + typename(id) + SC::BRK)
text << ("Id: " + get_item(id).id.to_s + SC::BRK)
text << ("Iconnumber: " + get_item(id).icon.to_s + SC::BRK)
text << ("Description: " + get_item(id).description + SC::BRK)
text << ("Consumable: " + get_item(id).consumable.to_s + SC::BRK)
text << ("Amount: " + get_item(id).amount.to_s)
if !showmsg
return text
else
msgbox(text)
end
end
 
def report_all(showmsg = false) #Report_all method for testing purposes
all_text = []
get_items.each do |item_obj|
  all_text.push(report(item_obj.id))
end
 
if !showmsg
return all_text
else
all_text.each do |txt|
  msgbox(txt)
  end
end
 
end
 
end #Keysystem
 
class Type < Keysystem
attr_reader :name
attr_reader :id
 
@name = ""
@id = 0
 
def initialize(name,id)
@name = name
@id = id
end
 
end #Type
 
class Item < Type
attr_reader :name
attr_reader :type
attr_reader :id
attr_reader :icon
attr_reader :description
attr_reader :consumable
attr_accessor :amount
 
@name = ""
@type = 0
@id = 0
@icon = 0
@description = ""
@consumable = false
@amount = 0;
 
def initialize(name,type,id,icon,description,consumable,amount)
@name = name
@type = type
@id = id
@icon = icon
@description = description
@consumable = consumable
@amount = amount
end
end #Item
 
class << DataManager
 
alias keysys_create_game_objects create_game_objects
 
def create_game_objects
keysys_create_game_objects
$game_keysystem = Keysystem.new
end
 
def make_save_contents
    contents = {}
    contents[:system]        = $game_system
    contents[:timer]         = $game_timer
    contents[:message]       = $game_message
    contents[:switches]      = $game_switches
    contents[:variables]     = $game_variables
    contents[:self_switches] = $game_self_switches
    contents[:actors]        = $game_actors
    contents[:party]         = $game_party
    contents[:troop]         = $game_troop
    contents[:map]           = $game_map
    contents[:player]        = $game_player
    contents[:keysystem]     = $game_keysystem
    contents
end
 
def extract_save_contents(contents)
    $game_system        = contents[:system]
    $game_timer         = contents[:timer]
    $game_message       = contents[:message]
    $game_switches      = contents[:switches]
    $game_variables     = contents[:variables]
    $game_self_switches = contents[:self_switches]
    $game_actors        = contents[:actors]
    $game_party         = contents[:party]
    $game_troop         = contents[:troop]
    $game_map           = contents[:map]
    $game_player        = contents[:player]
    $game_keysystem     = contents[:keysystem]
  end
end #DataManager
 
class Window_KeyType < Window_HorzCommand
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :item_window
  attr_reader :type
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0)
    @type = :none
  end
  #--------------------------------------------------------------------------
  # * Get Window Width
  #--------------------------------------------------------------------------
  def window_width
    Graphics.width
  end
  #--------------------------------------------------------------------------
  # * Get Digit Count
  #--------------------------------------------------------------------------
  def col_max
    return $game_keysystem.get_types.size
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    @item_window.category = current_symbol if @item_window
    @type = @item_window.category if @item_window
  end
  #--------------------------------------------------------------------------
  # * Create Command List
  #--------------------------------------------------------------------------
  def make_command_list
  $game_keysystem.get_types.each do |type|
    add_command(type.name,     type.name.to_sym)
    end
  end
  #--------------------------------------------------------------------------
  # * Set Item Window
  #--------------------------------------------------------------------------
  def item_window=(item_window)
    @item_window = item_window
    update
  end
end
 
 
class Window_MenuCommand < Window_Command
 
 alias add_original_commands_keysys add_original_commands
  def add_original_commands
    add_original_commands_keysys
    add_command(REDCLAW::KEYSYS::VOCAB::MENU_BUTTON_NAME, :keys, true)
  end
end
class Window_Keys < Window_Selectable
  attr_accessor :category
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super
    @category = category
    @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 1
  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_id)
    #msgbox(@category)
  return $game_keysystem.typesym(item_id) == @category
end
  #--------------------------------------------------------------------------
  # * Display in Enabled State?
  #--------------------------------------------------------------------------
  def enable?(item)
    return true
  end
  #--------------------------------------------------------------------------
  # * Create Item List
  #--------------------------------------------------------------------------
  def make_item_list
    @data = []
    $game_keysystem.get_items.each do |item|
      data.push(item) if include?(item.id)
    end
    #msgbox(@data.to_s)
  end
  #--------------------------------------------------------------------------
  # * Restore Previous Selection Position
  #--------------------------------------------------------------------------
  def select_last
    #select(@data.index($game_keysystem.last_key.object) || 0)
  end
  #--------------------------------------------------------------------------
  # * Draw Item
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    if item
      rect = item_rect(index)
      rect.width -= 4
      draw_item_name(item, rect.x, rect.y, enable?(item))
      draw_item_number(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
  #--------------------------------------------------------------------------
  # * 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_Keys
 
# =============================================================================
# Scene classes goes here
# =============================================================================
# =============================================================================
# ? Scene_Keys
# =============================================================================
class Scene_Keys < Scene_Base
 
def start
    super
    create_keytype_window
    end
 
  #--------------------------------------------------------------------------
  # * Create Category Window
  #--------------------------------------------------------------------------
 
   def on_keytype_ok
   if @keytype_window == nil
    if $game_keysystem.last_key["type"] != nil
    @keytype_window.category = $game_keysystem.typesym($game_keysystem.last_key["type"])
    else
    $game_keysystem.last_key["type"] = $game_keysystem.typesym_id(@keytype_window.item_window.category)
end
end
 
    if @keyitems_window == nil
      @keyitems_window =  Window_Keys.new(0,@keytype_window.height + 1, Graphics.width, Graphics.height - @keytype_window.height)
      if @keytype_window != nil
      @keytype_window.update
      #msgbox("@keytype_window.category = " + @keytype_window.type.to_s)
      @keyitems_window.category = @keytype_window.type
      end
      @keyitems_window.refresh
      @keyitems_window.active
      end
  end
 
   def draw_background(path)
      backsprite = Sprite.new
      backsprite.bitmap = Bitmap.new(path)
    end
 
  def create_keytype_window
    @keytype_window = Window_KeyType.new
    draw_background("Graphics/" + REDCLAW::KEYSYS::CONFIG::MENU_BACKGROUND_IMAGE)
    @keytype_window.viewport = @viewport
    @keytype_window.y = 0
    @keytype_window.set_handler(:ok,     method(:on_keytype_ok))
    @keytype_window.set_handler(:cancel, method(:return_scene))
  end
 
def make_keylist
keys = []
$game_keysystem.get_items.each do |key|
  keys.push(key) unless key.amount <= 0
  end
end
end #Scene_Keys
 
# =============================================================================
# ? Scene_Menu
# =============================================================================
class Scene_Menu < Scene_MenuBase
  alias redclaw_keysys create_command_window
 
  def command_keys
    SceneManager.call(Scene_Keys)
  end
 
  def create_command_window
    redclaw_keysys
    @command_window.set_handler(:keys, method(:command_keys))
  end
end #Scene_Menu

Thank you in advance and with kind regards

Redclaw

Original thread: https://forums.rpgmakerweb.com/threads/trouble-with-menu-creation-part-of-my-script-rgss3.75809/