设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 114|回复: 0

[转载发布] Simple Notetag Config v1.3

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    9071

    主题

    864

    回帖

    6万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    58883
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    68848

    灌水之王

    发表于 2026-8-3 14:43:44 | 显示全部楼层 |阅读模式
    Hello,

    If you are a scripter in your project and add many attributes in your database but are tired of writing the same stuff many times, this script might help you.
    I originally wrote it by adapting an older script from Clarabel from GTBS and realized that it might help others, so I share this here.

    It aims to reduce the code needed to add attributes to objects from the database, with this, you can simply add lines of codes like this (either before loading the database or inside method
    1. prepare_metadata
    复制代码
    ):
                    Ruby:       
    1. nd = Notetag_Data.new(:weight, 0, /^<item_weight\s*=\s*(\d+)\s*>/i, 0)
    2. DATA_ITEMS.concat([nd])
    复制代码

    This will create an attribute called
    1. weight
    复制代码
    for all objects in
    1. $data_items
    复制代码
    with default value 0, and that can be modified by putting the corresponding note in the database, for instance if for item number 3 you put
    1. <item_weight = 2>
    复制代码
    in the notes, then:
    1. $data_items[3].weight
    复制代码
    will be 2 while
    1. $data_items[2].weight
    复制代码
    will be the default value.

    The syntax is:
                    Ruby:       
    1. Notetag_Data.new(:attribute_name, default_value, regexp, assignation_mode_id)
    复制代码

    With
    1. assignation_mode_id
    复制代码
    an integer between 0 and 3:
    - 0 means that the part inside the parenthesis of the regexp is computed (with
    1. eval
    复制代码
    method) and stored
    - 1 means that the string itself is stored (useful for file names or plain text)
    - 2 means that when the regexp is found, the value is set to
    1. true
    复制代码
    - 3 is for hash tables, the first value is the key and the second is the data stored in it, both values are evaluated with eval first

    The supported data and notetags are actors, classes, skills, items, weapons, armors,  enemies, troops, states, maps and tilesets.
    In the case of troops, the notetags are taken from any comment inside the troop's events, regardless of their page conditions.

    Note that this script requires an understanding of regular expressions and simple code, therefore if you don't understand how they work, I suggest you don't use this script.
    Also, since the script adds attributes 'dynamically', please choose wisely their names to avoid conflict with preexisting attributes and methods. Finally, the script only modifies classes from the RPG module, so you will need to write more code if you plan on using them in your scripts.

    Here is the script:
                    Ruby:       
    1. #==============================================================================
    2. # Simple Notetag Config v1.3
    3. #------------------------------------------------------------------------------
    4. # Author: Timtrack
    5. # date: 03/03/2026
    6. # inspired by Clarabel's notetags reading in GTBS
    7. #==============================================================================
    8. $imported = {} if $imported.nil?
    9. $imported["TIM-SimpleNotetagConfig"] = true
    10. #==============================================================================
    11. # Version History
    12. #------------------------------------------------------------------------------
    13. # 08/09/2025 - Original release
    14. # 03/11/2025 - v1.1 added tileset notetags support, fixed crash for battle test
    15. # 06/11/2025 - v1.2 added hash support, extended the matching support to cover
    16. #                   more complicated objects
    17. # 03/03/2026 - v1.3 moved the methods creations of the class to be used once
    18. #==============================================================================
    19. # Description: a utility script for those tired of writing the same multiple
    20. # lines of code to setup data from notetags. This script will allow, with a few
    21. # line of code, to create attributes to objects from the database with a default
    22. # value and a basic assignation value through notetag reading.
    23. #
    24. # This script is for advanced users with enough knowledge in notetags setup,
    25. # you must understand basic code and regexp.
    26. #==============================================================================
    27. # Term of use: Free to use in free or commercial games, please give credit.
    28. #==============================================================================
    29. # Installation & Configuration: put the script above main, set your own code in
    30. # method self.prepare_metadata to create attributes to be read in notetags
    31. #
    32. # Add to DATA_ arrays Notetag_Data objects that will be turned into attributes
    33. # inside rpg maker database objects.
    34. # DATA can be DATA_SKILLS, DATA_ITEMS, DATA_WEAPONS, DATA_ARMORS, DATA_TILESETS,
    35. # DATA_STATES, DATA_ENEMIES, DATA_ACTORS, DATA_CLASSES, DATA_MAPS or DATA_TROOPS
    36. #
    37. # For each DATA_X, the attribute will be read from the notetags regexp and will
    38. # be stored inside $data_x at the launch of the game when loading the database.
    39. # There are two exceptions:
    40. # For DATA_MAPS, the data will be stored in $data_mapinfos
    41. # For DATA_TROOPS, the data will be read from comments in every page of the
    42. # troop (regardless of their conditions)
    43. #
    44. # To add an entry, put in the corresponding(s) arrays:
    45. #   Notetag_Data.new(symbol, default, regexp, type)
    46. #
    47. # symb    is the name of the attribute added to the database, it will be set as
    48. #         an accessor, for instance:
    49. #         symb=:range means that obj.range will be accessible from the database
    50. # default is the value set by default when no matching line to the regexp was
    51. #         found
    52. # regexp  is a regular expression set to find the value, it will be interpreted
    53. #         with type
    54. # type    is an integer related to AssignationModes indices
    55. #         0/none provided -> the data is read and evaluated (int, bool, float,
    56. #                            symbols, arrays)
    57. #         1               -> the string is stored as is (files, texts)
    58. #         2               -> will replace the default value by true if the
    59. #                            regexp is found
    60. #         3               -> to deal with hash tables, will eval the two values
    61. #                            set ($1,$2) and do hash[eval($1)] = eval($2)
    62. #
    63. # You can put a Notetag_Data object x to multiple arrays by calling
    64. #   x.add_to(a1,a2,...)
    65. #
    66. # Example:
    67. #  n = Notetag_Data.new(:required_level, 1, /^<min_level\s*=\s*(\d+)\s*>/i, 0)
    68. #  n.add_to(DATA_WEAPONS,DATA_ARMORS)
    69. # If the weapon with id 5 has in a notetag <min_level = 3>, then
    70. # $data_weapons[5].required_level will be 3,
    71. # If no such notetag was given, the default value will be stored instead (ex: 1)
    72. #
    73. # If for some reason you need to perform some specific postprocessing action to
    74. # the added attributes, you can redefine method post_metadata_notetags_reading
    75. # in RPG::BaseItem, RPG::MapInfo, RPG::Tileset, RPG::Troop and their subclasses
    76. #============================================================================
    77. if $imported["TIM-SimpleNotetagConfig"]
    78. #============================================================================
    79. # SNC/SimpleNotetagConfig -> module that you must configure
    80. #============================================================================
    81. module SNC
    82.   #--------------------------------------------------------------------------
    83.   # method: prepare_metadata -> called while loading the database at the start
    84.   # of the game, fill the arrays with Notetag_Data objects here
    85.   #--------------------------------------------------------------------------
    86.   def self.prepare_metadata
    87.     #You modify the arrays like this:
    88.     # DATA_SKILLS.concat([
    89.     #   Notetag_Data.new(:var_symb, default_value, regexp, regexp_read_type_id),
    90.     #   Notetag_Data.new(:v2, default_value2, regexp2, regexp_read_type_id2),
    91.     # ])
    92.     #
    93.     #You can also add the same notetag object to multiple data arrays:
    94.     # obj.add_to(DATA_WEAPONS,DATA_ARMORS,DATA_STATES)
    95.     #
    96.     #example code:
    97.     #n = Notetag_Data.new(:required_level, 1, /^<min_level\s*=\s*(\d+)\s*>/i, 0)
    98.     #n.add_to(DATA_WEAPONS,DATA_ARMORS,DATA_MAPS,DATA_TROOPS)
    99.   end
    100. #============================================================================
    101. # Editing anything past this point is not recommended
    102. #============================================================================
    103.   #DATA_SKILLS Config (modify RPG::Skill)
    104.   DATA_SKILLS = []
    105.   #DATA_ITEMS Config (modify RPG::Item)
    106.   DATA_ITEMS = []
    107.   #DATA_WEAPONS Config (modify RPG::Weapon)
    108.   DATA_WEAPONS = []
    109.   #DATA_ARMORS Config (modify RPG::Armor)
    110.   DATA_ARMORS = []
    111.   #DATA_STATES Config (modify RPG::State)
    112.   DATA_STATES = []
    113.   #DATA_ENEMIES Config (modify RPG::Enemy)
    114.   DATA_ENEMIES = []
    115.   #DATA_ACTORS Config (modify RPG::Actor)
    116.   DATA_ACTORS = []
    117.   #DATA_CLASSES Config (modify RPG::Class)
    118.   DATA_CLASSES = []
    119.   #DATA_MAPS Config (modify RPG::MapInfo)
    120.   DATA_MAPS = []
    121.   #DATA_TROOPS Config (modify RPG::Troop)
    122.   DATA_TROOPS = []
    123.   #DATA_TILESETS Config (modify RPG::Tileset)
    124.   DATA_TILESETS = []
    125.   
    126.   #--------------------------------------------------------------------------
    127.   # create_class_metadata_methods -> creates accessor to the object's class
    128.   #--------------------------------------------------------------------------
    129.   DATA_META_CLASSES = [] #remembers the classes handled
    130.   def self.create_class_metadata_methods(obj_class, data_list)
    131.     return if DATA_META_CLASSES.include?(obj_class)
    132.     data_list.each{|d| obj_class.send(:attr_accessor, d.symbol)}
    133.     DATA_META_CLASSES.push(obj_class)
    134.   end
    135.   
    136.   #--------------------------------------------------------------------------
    137.   # load_notetags_metadata -> reads notes and creates attributes
    138.   # data_list is an array of Notetag_Data
    139.   #--------------------------------------------------------------------------
    140.   def self.load_notetags_metadata(obj, data_list, note)
    141.     create_class_metadata_methods(obj.class, data_list)
    142.     data_list.each do |d|
    143.       obj.instance_variable_set(d.attribute_name, d.default_value)
    144.     end
    145.     note.split(/[\r\n]+/).each do |line|
    146.       data_list.each do |d|
    147.         matches = line.scan(d.regexp)
    148.         matches.each do |str_values|
    149.           original_v = obj.instance_variable_get(d.attribute_name)
    150.           new_val = d.value(original_v, str_values)
    151.           obj.instance_variable_set(d.attribute_name, new_val)
    152.         end
    153.       end
    154.     end # self.note.split
    155.     obj.post_metadata_notetags_reading
    156.   end
    157. end #SNC
    158. #============================================================================
    159. # Notetag_Data -> represents how notetags are read for most items
    160. #============================================================================
    161. class Notetag_Data
    162.   #eval: to save int, floats or arrays
    163.   #string: to save the evaluated data as a string
    164.   #activate: to put a value to true when data matches the regexp
    165.   #hash: to put inside the hash the second value with key the first value
    166.   AssignationModes = [:eval, :string, :activate, :hash]
    167.   #accessors:
    168.   attr_accessor :symbol, :default_value, :regexp, :assignation_mode
    169.   #--------------------------------------------------------------------------
    170.   # method: initialize
    171.   #--------------------------------------------------------------------------
    172.   def initialize(symb,default,regexp,assign_mode_id=0)
    173.     @symbol = symb
    174.     @default_value = default
    175.     @regexp = regexp
    176.     @assignation_mode = AssignationModes[assign_mode_id]
    177.   end
    178.   
    179.   #--------------------------------------------------------------------------
    180.   # method: attribute_name -> turns symb :s into @s for instance get/set
    181.   #--------------------------------------------------------------------------
    182.   def attribute_name
    183.     return "@"+@symbol.id2name
    184.   end
    185.   
    186.   #--------------------------------------------------------------------------
    187.   # method: value -> returns a value to store into the object
    188.   # parameters:
    189.   # -original_value : the current object's value
    190.   # -str_l a list of string identified by the regexp
    191.   #--------------------------------------------------------------------------
    192.   def value(original_value, str_l)
    193.     case @assignation_mode
    194.     when :eval then eval(str_l[0])
    195.     when :string then str_l[0]
    196.     when :activate then true
    197.     when :hash
    198.       key  = eval(str_l[0])
    199.       value = eval(str_l[1])
    200.       original_value = original_value.dup
    201.       original_value[key] = value
    202.       return original_value
    203.     end
    204.   end
    205.   
    206.   #--------------------------------------------------------------------------
    207.   # method: add_to -> add itself to each given arrays
    208.   #--------------------------------------------------------------------------
    209.   def add_to(*args)
    210.     args.each{|a| a.push(self)}
    211.   end
    212. end #Notetag_Data
    213. #============================================================================
    214. # DataManager
    215. #============================================================================
    216. module DataManager
    217.   class << self
    218.     alias load_database_metadata load_database
    219.     alias load_battle_test_database_metadata load_battle_test_database
    220.   end
    221.   #--------------------------------------------------------------------------
    222.   # alias method: load_database
    223.   #--------------------------------------------------------------------------
    224.   def self.load_database
    225.     load_database_metadata
    226.     load_notetags_metadata
    227.   end
    228.   
    229.   #--------------------------------------------------------------------------
    230.   # alias method: load_battle_test_database -> loads map data
    231.   #--------------------------------------------------------------------------
    232.   def self.load_battle_test_database
    233.     load_battle_test_database_metadata
    234.     $data_mapinfos = load_data("Data/MapInfos.rvdata2")
    235.   end
    236.   #--------------------------------------------------------------------------
    237.   # new method: load_notetags_metadata
    238.   #--------------------------------------------------------------------------
    239.   def self.load_notetags_metadata
    240.     SNC.prepare_metadata
    241.     pairs = [
    242.       [$data_actors,  SNC::DATA_ACTORS],
    243.       [$data_classes, SNC::DATA_CLASSES],
    244.       [$data_weapons, SNC::DATA_WEAPONS],
    245.       [$data_armors,  SNC::DATA_ARMORS],
    246.       [$data_enemies, SNC::DATA_ENEMIES],
    247.       [$data_states,  SNC::DATA_STATES],
    248.       [$data_items,   SNC::DATA_ITEMS],
    249.       [$data_skills,  SNC::DATA_SKILLS],
    250.       [$data_troops,  SNC::DATA_TROOPS],
    251.       [$data_tilesets,SNC::DATA_TILESETS],
    252.     ]
    253.     #for any RPG::BaseItem or RPG::Troop
    254.     pairs.each do |group,data_l|
    255.       group.each do |obj|
    256.         SNC.load_notetags_metadata(obj,data_l,obj.note) if obj
    257.       end
    258.     end
    259.     #load map data, mapinfo will read RPG::Map notetags:
    260.     $data_mapinfos.each_pair do |key,mapinfo|
    261.       next unless mapinfo
    262.       filename = sprintf("Data/Map%03d.rvdata2", key)
    263.       #next unless File.exist?(filename)
    264.       note = load_data(filename).note
    265.       SNC.load_notetags_metadata(mapinfo, SNC::DATA_MAPS,note)
    266.     end
    267.   end
    268. end # DataManager
    269. #============================================================================
    270. # RPG::BaseItem -> parent class of actor, class, skill, item, weapon, armor,
    271. # enemy, and state.
    272. #============================================================================
    273. class RPG::BaseItem
    274.   #--------------------------------------------------------------------------
    275.   # new method: post_metadata_notetags_reading -> postprocessing
    276.   #--------------------------------------------------------------------------
    277.   def post_metadata_notetags_reading
    278.   end
    279. end #RPG::BaseItem
    280. #============================================================================
    281. # RPG::MapInfo -> The data class for map information
    282. #============================================================================
    283. class RPG::MapInfo
    284.   #--------------------------------------------------------------------------
    285.   # new method: post_metadata_notetags_reading -> postprocessing
    286.   #--------------------------------------------------------------------------
    287.   def post_metadata_notetags_reading
    288.   end
    289. end #RPG::MapInfo
    290. #============================================================================
    291. # RPG::Tileset -> The data class for tilesets
    292. #============================================================================
    293. class RPG::Tileset
    294.   #--------------------------------------------------------------------------
    295.   # new method: post_metadata_notetags_reading -> postprocessing
    296.   #--------------------------------------------------------------------------
    297.   def post_metadata_notetags_reading
    298.   end
    299. end #RPG::Tileset
    300. #============================================================================
    301. # RPG::Troop -> The data class for enemy troop
    302. #============================================================================
    303. class RPG::Troop
    304.   #--------------------------------------------------------------------------
    305.   # new method: note -> reads all comments in all pages and returns them as
    306.   # notes (from Victor Core Engine)
    307.   #--------------------------------------------------------------------------
    308.   def note
    309.     comment_list = []
    310.     return @notes if @notes
    311.     @pages.each do |page|
    312.       next unless (page && page.list && page.list.size > 0)
    313.       note_page = page.list.dup
    314.       note_page.each do |item|
    315.         next unless item && (item.code == 108 || item.code == 408)
    316.         comment_list.push(item.parameters[0])
    317.       end
    318.     end
    319.     @notes = comment_list.join("\r\n")
    320.     return @notes
    321.   end
    322.   
    323.   #--------------------------------------------------------------------------
    324.   # new method: post_metadata_notetags_reading -> postprocessing
    325.   #--------------------------------------------------------------------------
    326.   def post_metadata_notetags_reading
    327.   end
    328. end #RPG::Troop
    329. end #$imported["TIM-SimpleNotetagConfig"]
    复制代码


    Terms and Credits
    Free for commercial and non-commercial projects as long as you credit me.

    Edit: updated to version 1.1, fixing test battle crash and handling tileset notetags.
    Edit: updated to version 1.2, bringing hash table storage.
    Edit: updated to version 1.3, the methods created by notetags should now be set once per class, this does not change anything aside from maybe performances at launch.


    本贴来自国际rpgmaker官方论坛作者:Timtrack处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/simple-notetag-config-v1-3.179996/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-8-17 06:20 , Processed in 0.115760 second(s), 52 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表