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

[转载发布] Automatic Updating Switches and Variables XP

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

    连续签到: 2 天

    [LV.7]常住居民III

    9015

    主题

    864

    回帖

    6万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

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

    灌水之王

    发表于 2026-7-26 12:52:22 | 显示全部楼层 |阅读模式
    Automatic Updating Switches and Variables
    By SailCat


    Introduction
    This script turns switches and variables into auto-updating by binding an expression to it.

    Features
    XP, unlike VX or VA, allows only switches, variables and self switches to be used for events. So it is difficult to set an NPC to be available when, say, Alexus is present, or a key item is held in inventory. The two conditions are available in later maker versions.
    This script makes a turnaround by setting a switch (variable as well) as always ready to represent a certain expression.
    If you want a switch to indicate whether Alexus is present, write its name as "=\P.i?(\N[1])" (remove quotation mark).
    If you want a switch to indicate whether a key is held, write its name as "=\I(29)>0"
    The first char, =, tells the plugin that the target switch is an auto-updating one. So instead of normally turning it on or off, it will instead evaluate the expression after it.

    You can far do more than what VX/VA can achieve.
    To test if Alexus is wielding mithril sword, name a switch as "=\N[1].w?(4)"
    To set an enemy to act in a way when party level is BELOW a certain value: name a switch as "=\L<15", and then set this switch as an action trigger, since default action triggers only allows party level ABOVE a certain value.

    To test whether you have collected enough items from anywhere (drops, NPCs, treasures).
    You had to:
    1. Set a common event to be parallel upon some switch
    2. Let the event judge the item number every frame, until you fulfill it
    3. Do the processes, and turn off the switch(to prevent the process from triggering a second time)
    Now with this script:
    1. Set a common event to be automatic upon some switch, and name the switch such as "=\I(x)>y && !s[z]"
    2. Do the processes, and turn on switch z(to prevent the process from triggering a second time)
    This saves the engine from updating an extra parallel common event every frame.

    To make an event like granting you Excalibur II when you reach it in less than 12 hours.
    You had to:
    1. Set the target event to be automatic in the first page, and use a second and third(empty) page.
    2. In the first page, use operate variables to load elapsed time, and turn Self Switch A on (to stop automatic processing)
    3. Set the second page to be on self switch A, and grant the reward on this page.
    4. Set the third page to be empty on "self switch A" and "variable > 43200" to hide the reward when time expires.
    Now with the script:
    1. Name a variable as "=-\F"
    2. Set the target event on "variable > -43200", and give the reward on the first page.
    3. No extra processing or pages required.

    How to Use
    Refer to the comment before plugin script.

    Terms and Credits
    Feel free to use in any kind of games.
    Please credit me by leaving the second line of the script.
    Redistributions are OK. Secondary development is not allowed.

                    Ruby:       
    1. #==============================================================================
    2. # ■ Automated Switches & Variables by SailCat
    3. #------------------------------------------------------------------------------
    4. #   Introduction:
    5. #      This script turns switches and variables into auto-updating by binding
    6. #   an expression to it.
    7. #
    8. #   Installation Guide:
    9. #      Insert this script before the "Main" section.
    10. #
    11. #   Date of release:
    12. #      Sep. 6, 2024 (version 1.0)
    13. #
    14. #   Effects:
    15. #   1. Allows auto-updating switches bound to Ruby expressions.
    16. #   2. Allows auto-updating variables bound to Ruby expressions.
    17. #   3. The auto-updating switches or variables do not update per frame (as this
    18. #      may greatly  the system frame rates). Instead, they are updated
    19. #      only when their values are used to allow optimal performance.
    20. #   4. You can not turn auto-updating switches on or off, nor can you assign
    21. #      values to auto-updating switches, either by event commands or by script.
    22. #
    23. #   How to use:
    24. #   1. Any switch or variable with its name starting with "=" char is considered
    25. #      to be auto-updating. (The char can be configured in the script)
    26. #   2. In this case, the remaining 39 chars of the name can be written as valid
    27. #      Ruby expression by which the switch or variable is evaluated upon update.
    28. #   3. To save space, abbreviations are allowed in the expression as follows:
    29. #      Abbr  Equivalent to              Meaning                  Example
    30. #      ------------------------------------------------------------------------
    31. #      \N    $game_actors               Get actor by ID          \N[1].str
    32. #      \P    $game_party.actors         Get actor by party pos   \P[0].name
    33. #      \E    $game_troop.enemies        Get enemy by troop pos   \E[2].level
    34. #      \M    $game_map.events           Get map event by ID      \M[3]
    35. #      \L    $game_party.max_level      Get party level          \L
    36. #      \G    $game_party.gold           Get gold deposit         \G
    37. #      \B    $game_party.steps          Get step count           \B
    38. #      \T    $game_system.timer /       Get timer                \T
    39. #              Graphics.frame_rate        (cast in seconds)
    40. #      \F    Graphics.frame_count /     Get elapsed time         \F
    41. #              Graphics.frame_rate        (cast in seconds)
    42. #      \C    $game_screen.weather_type  Get weather type         \C
    43. #      \I    $game_party.item_number    Get item number by ID    \I(12)
    44. #      \W    $game_party.weapon_number  Get weapon number by ID  \W(2)
    45. #      \A    $game_party.armor_number   Get armor number by ID   \A(31)
    46. #      .k?   .skill_learn?              Skill learned by actor   \N[1].k?(6)
    47. #      .w?   (omitted for complexity)   Weapon equipped by actor \N[4].w?(8)
    48. #      .a?   (omitted for complexity)   Armor equipped by actor  \P[2].a?(9)
    49. #      .s?   .state?                    State present by battler \E[5].s?(3)
    50. #      .i?   .include?                  Element in array or set  \P.i?(\N[7])
    51. #      a     (omitted for complexity)   Current active battler   a.maxhp
    52. #      b     (omitted for complexity)   Targeted battlers        b[0].dead?
    53. #      l     $game_player               The player character     l.x
    54. #      s     $game_switches             Switches                 s[23]
    55. #      v     $game_variables            Variables                v[112]
    56. #      f     $game_self_switches        Self switches            f[[1,2,"A"]]
    57. #      m     $game_map                  Map data                 m.map_id
    58. #      t     $game_temp                 Temporary data           t.in_battle
    59. #      y     $game_system               System data              y.save_count
    60. #      r     $game_screen               Screen data              r.tone.red
    61. #   4. Auto-updating switches are evaluated and forcingly cast as true or false.
    62. #      Failed expressions are considered as false.
    63. #   5. Auto-updating variables are evaluated and forcingly cast as integers.
    64. #      Failed expressions are considered as zero (0).
    65. #      If you need non-integer values, turn the setting off in configurations.
    66. #   6. This plugin also supports multiple condition referring to switches or
    67. #      variables:
    68. #      ss(range or array of switch IDs): any switch in set is true
    69. #        Ex: ss(5..7) is the same as s[5] || s[6] || s[7]
    70. #      tt(range or array of switch IDs): all switches in set are true
    71. #        Ex: tt([98,132,142]) is the same as s[98] && s[132] && s[142]
    72. #      vv(range or array of variable IDs): any variable in set is non-zero
    73. #        Ex: vv(12..14) is the same as v[12] != 0 || v[13] != 0 || v[14] != 0
    74. #      xx(range or array of variable IDs): all variables in set are non-zero
    75. #        Ex: xx([17, 28]) is the same as v[17] != 0 && v[28] != 0
    76. #      uu(range or array of variable IDs): the sum value of variables in set
    77. #        Ex: uu(1..6) is the same as v[1]+v[2]+v[3]+v[4]+v[5]+v[6]
    78. #==============================================================================
    79. #==============================================================================
    80. # ■ SailCat's XP Plugins
    81. #==============================================================================
    82. module SailCat
    83.   #--------------------------------------------------------------------------
    84.   # ● Configurations
    85.   #--------------------------------------------------------------------------
    86.   module System_Config
    87.     EVAL_PREFIX = ?=              # Prefix char for auto-updating expression
    88.     DISABLE_SUB = ?\              # Escape char to stop macro replacing
    89.     FORCE_INT_V = true            # Force variables into integers
    90.   end
    91. end
    92. #==============================================================================
    93. # ■ NameParser
    94. #------------------------------------------------------------------------------
    95. #   The module parsing switch and variable names
    96. #==============================================================================
    97. module NameParser
    98.   include SailCat::System_Config
    99.   #--------------------------------------------------------------------------
    100.   # ● Cached expressions for macro replacement
    101.   #--------------------------------------------------------------------------
    102.   @@exp_cache = Hash.new do |hash, exp|
    103.     key = exp.clone
    104.     unless exp[0] == DISABLE_SUB
    105.       exp.gsub!(/\\N/)    { "$game_actors" }
    106.       exp.gsub!(/\\P/)    { "$game_party.actors" }
    107.       exp.gsub!(/\\E/)    { "$game_troop.enemies" }
    108.       exp.gsub!(/\\M/)    { "$game_map.events" }
    109.       exp.gsub!(/\\L/)    { "$game_party.max_level" }
    110.       exp.gsub!(/\\G/)    { "$game_party.gold" }
    111.       exp.gsub!(/\\B/)    { "$game_party.steps" }
    112.       exp.gsub!(/\\T/)    { "$game_system.timer / Graphics.frame_rate" }
    113.       exp.gsub!(/\\F/)    { "Graphics.frame_count / Graphics.frame_rate" }
    114.       exp.gsub!(/\\C/)    { "$game_screen.weather_type" }
    115.       exp.gsub!(/\\I/)    { "$game_party.item_number" }
    116.       exp.gsub!(/\\W/)    { "$game_party.weapon_number" }
    117.       exp.gsub!(/\\A/)    { "$game_party.armor_number" }
    118.       exp.gsub!(/\.k\?/)  { ".skill_learn?" }
    119.       exp.gsub!(/\.w\?/)  { ".weapon_id==" }
    120.       exp.gsub!(/\.a\?/)  { ".to_a.map{|a|[a.armor1_id,a.armor2_id,"+
    121.         "a.armor3_id,a.armor4_id]}.flatten.i?" }
    122.       exp.gsub!(/\.s\?/)  { ".state?" }
    123.       exp.gsub!(/\.i\?/)  { ".include?" }
    124.     else
    125.       exp.slice!(0, 1)
    126.     end
    127.     hash[key] = exp
    128.   end
    129.   #--------------------------------------------------------------------------
    130.   # ● Parse expression
    131.   #     exp : The expression written in name
    132.   #--------------------------------------------------------------------------
    133.   def self.parse(exp)
    134.     lambda {|a, b, l, s, v, f, m, t, y, r| eval(@@exp_cache[exp])}.call(
    135.       $game_temp.in_battle ?
    136.         $scene.instance_variable_get(:@active_battler) : nil,
    137.       $game_temp.in_battle ?
    138.         $scene.instance_variable_get(:@target_battlers) : nil,
    139.       $game_player, $game_switches, $game_variables, $game_self_switches,
    140.       $game_map, $game_temp, $game_system, $game_screen
    141.     )
    142.   end
    143.   #--------------------------------------------------------------------------
    144.   # ● Set judging functions to allow simple multi-evaluation
    145.   #--------------------------------------------------------------------------
    146.   def self.ss(set);   set.to_a.any? {|id| $game_switches[id]};            end
    147.   def self.tt(set);   set.to_a.all? {|id| $game_switches[id]};            end
    148.   def self.vv(set);   set.to_a.any? {|id| $game_variables[id] != 0};      end
    149.   def self.xx(set);   set.to_a.all? {|id| $game_variables[id] != 0};      end
    150.   def self.uu(set);   set.to_a.inject(0){|a,id| a+$game_variables[id]};   end
    151. end
    152. #==============================================================================
    153. # ■ Game_Switches
    154. #==============================================================================
    155. class Game_Switches
    156.   include SailCat::System_Config
    157.   #--------------------------------------------------------------------------
    158.   # ● Retrieve auto-updating switches
    159.   #     id : Switch ID
    160.   #--------------------------------------------------------------------------
    161.   alias sailcat_autosw :[]
    162.   def [](id)
    163.     if $data_system.switches[id].to_s[0] == EVAL_PREFIX then
    164.       !!NameParser::parse($data_system.switches[id][1..-1]) rescue false
    165.     else
    166.       sailcat_autosw(id)
    167.     end
    168.   end
    169. end
    170. #==============================================================================
    171. # ■ Game_Variables
    172. #==============================================================================
    173. class Game_Variables
    174.   include SailCat::System_Config
    175.   #--------------------------------------------------------------------------
    176.   # ● Retrieve auto-updating variables
    177.   #     id : Variable ID
    178.   #--------------------------------------------------------------------------
    179.   alias sailcat_autova :[]
    180.   def [](id)
    181.     if $data_system.variables[id].to_s[0] == EVAL_PREFIX then
    182.       value = (NameParser::parse($data_system.variables[id][1..-1]) rescue 0)
    183.       FORCE_INT_V ? (value.to_i rescue 0) : value
    184.     else
    185.       sailcat_autova(id)
    186.     end
    187.   end
    188. end
    复制代码




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

    使用道具 举报

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

    本版积分规则

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

    幸运抽奖

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

    立即查看

    聊天机器人
    Loading...

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

    GMT+8, 2026-8-4 12:48 , Processed in 0.094847 second(s), 54 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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