Project1

标题: 【XP】全局开关和变量 [打印本页]

作者: 芯☆淡茹水    时间: 2021-7-29 08:26
标题: 【XP】全局开关和变量
本帖最后由 芯☆淡茹水 于 2021-7-29 15:58 编辑

RUBY 代码复制
  1. #==============================================================================
  2. # 〓 全局开关和变量 〓      By: 芯☆淡茹水
  3. #==============================================================================
  4. # 全局开关或变量,在取名时在名字前面加上 $ 符号
  5. #==============================================================================
  6. module XR_GlobalData
  7.   #--------------------------------------------------------------------------
  8.   File_name = "GlobalData"
  9.   #--------------------------------------------------------------------------
  10.   def self.filename
  11.     return File_name + ".rxdata"
  12.   end
  13.   #--------------------------------------------------------------------------
  14.   def self.make_empty_data
  15.     return {:switches=>{}, :variables=>{}}
  16.   end
  17.   #--------------------------------------------------------------------------
  18.   def self.on_change(type, id, value)
  19.     @data = @data || self.make_empty_data
  20.     if @data[type]
  21.       @data[type][id] = value
  22.       file = File.open(self.filename, "wb")
  23.       Marshal.dump(@data, file)
  24.       file.close
  25.     end
  26.   end
  27.   #--------------------------------------------------------------------------
  28.   def self.load
  29.     @data = self.make_empty_data
  30.     if FileTest.exist?(self.filename)
  31.       file = File.open(self.filename, "rb")
  32.       @data = Marshal.load(file)
  33.       file.close
  34.     end
  35.     $game_switches.synchro(@data[:switches])
  36.     $game_variables.synchro(@data[:variables])
  37.   end
  38. end
  39. #==============================================================================
  40. class Game_Switches
  41.   #--------------------------------------------------------------------------
  42.   def is_global_switch?(switch_id)
  43.     return false if !switch_id
  44.     name = $data_system.switches[switch_id]
  45.     return !!name && !!name.match(/^\$/)
  46.   end
  47.   #--------------------------------------------------------------------------
  48.   def synchro(hash)
  49.     hash.keys.each do |id|
  50.       @data[id] = hash[id]
  51.       $game_map.need_refresh = true
  52.     end
  53.   end
  54.   #--------------------------------------------------------------------------
  55.   alias :xr_gd_change_switch :[]=
  56.   def []=(switch_id, value)
  57.     tmp = self[switch_id]
  58.     xr_gd_change_switch(switch_id, value)
  59.     if is_global_switch?(switch_id) && tmp != self[switch_id]
  60.       XR_GlobalData.on_change(:switches, switch_id, self[switch_id])
  61.     end
  62.   end
  63. end
  64. #==============================================================================
  65. class Game_Variables
  66.   #--------------------------------------------------------------------------
  67.   def is_global_variable?(variable_id)
  68.     return false if !variable_id
  69.     name = $data_system.variables[variable_id]
  70.     return !!name && !!name.match(/^\$/)
  71.   end
  72.   #--------------------------------------------------------------------------
  73.   def synchro(hash)
  74.     hash.keys.each do |id|
  75.       @data[id] = hash[id]
  76.       $game_map.need_refresh = true
  77.     end
  78.   end
  79.   #--------------------------------------------------------------------------
  80.   alias :xr_gd_change_variable :[]=
  81.   def []=(variable_id, value)
  82.     tmp = self[variable_id]
  83.     xr_gd_change_variable(variable_id, value)
  84.     if is_global_variable?(variable_id) && tmp != self[variable_id]
  85.       XR_GlobalData.on_change(:variables, variable_id, self[variable_id])
  86.     end
  87.   end
  88. end
  89. #==============================================================================
  90. class Scene_Title
  91.   #--------------------------------------------------------------------------
  92.   alias xr_gd_command_new_game command_new_game
  93.   def command_new_game
  94.     xr_gd_command_new_game
  95.     XR_GlobalData.load
  96.   end
  97. end
  98. #==============================================================================
  99. class Scene_Load
  100.   #--------------------------------------------------------------------------
  101.   alias xr_gd_read_save_data read_save_data
  102.   def read_save_data(file)
  103.     xr_gd_read_save_data
  104.     XR_GlobalData.load
  105.   end
  106. end
  107. #==============================================================================
  108. # end
  109. #==============================================================================

作者: enghao_lim    时间: 2021-7-29 12:16
本帖最后由 enghao_lim 于 2021-7-29 12:17 编辑

对脚本党来说这个命名有一定的误导性啊,是骗我读脚本吗?
不过这样一个小巧思一下子把跨存档开关变量拉到没有0门槛了,真有意思。
作者: guoxiaomi    时间: 2021-7-29 14:04
游戏一开始就$data_system就不会有任何改变了,可以用Hash存下来名字开头是$的变量ID,避免调用XR_GlobalData.save时每次都要循环全部的变量匹配一下正则表达式。

而且每次改变一个全局变量或者开关的时候,都会对所有的变量和开关做一次遍历,匹配它们的名字。有的人做游戏时什么都不管先把变量和开关开成5000的,就会大大影响效率了。
作者: xp兔子徒弟    时间: 2021-7-29 20:30
现在大佬们做游戏都要搞多周目了吗




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1