RUBY 代码 - #==============================================================================
- # 〓 全局开关和变量 〓 By: 芯☆淡茹水
- #==============================================================================
- # 全局开关或变量,在取名时在名字前面加上 $ 符号
- #==============================================================================
- module XR_GlobalData
- #--------------------------------------------------------------------------
- File_name = "GlobalData"
- #--------------------------------------------------------------------------
- defself.filename
- return File_name + ".rxdata"
- end
- #--------------------------------------------------------------------------
- defself.make_empty_data
- return{:switches=>{}, :variables=>{}}
- end
- #--------------------------------------------------------------------------
- defself.on_change(type, id, value)
- @data = @data || self.make_empty_data
- if@data[type]
- @data[type][id] = value
- file = File.open(self.filename, "wb")
- Marshal.dump(@data, file)
- file.close
- end
- end
- #--------------------------------------------------------------------------
- defself.load
- @data = self.make_empty_data
- ifFileTest.exist?(self.filename)
- file = File.open(self.filename, "rb")
- @data = Marshal.load(file)
- file.close
- end
- $game_switches.synchro(@data[:switches])
- $game_variables.synchro(@data[:variables])
- end
- end
- #==============================================================================
- class Game_Switches
- #--------------------------------------------------------------------------
- def is_global_switch?(switch_id)
- returnfalseif !switch_id
- name = $data_system.switches[switch_id]
- return !!name && !!name.match(/^\$/)
- end
- #--------------------------------------------------------------------------
- def synchro(hash)
- hash.keys.eachdo |id|
- @data[id] = hash[id]
- $game_map.need_refresh = true
- end
- end
- #--------------------------------------------------------------------------
- alias:xr_gd_change_switch :[]=
- def[]=(switch_id, value)
- tmp = self[switch_id]
- xr_gd_change_switch(switch_id, value)
- if is_global_switch?(switch_id) && tmp != self[switch_id]
- XR_GlobalData.on_change(:switches, switch_id, self[switch_id])
- end
- end
- end
- #==============================================================================
- class Game_Variables
- #--------------------------------------------------------------------------
- def is_global_variable?(variable_id)
- returnfalseif !variable_id
- name = $data_system.variables[variable_id]
- return !!name && !!name.match(/^\$/)
- end
- #--------------------------------------------------------------------------
- def synchro(hash)
- hash.keys.eachdo |id|
- @data[id] = hash[id]
- $game_map.need_refresh = true
- end
- end
- #--------------------------------------------------------------------------
- alias:xr_gd_change_variable :[]=
- def[]=(variable_id, value)
- tmp = self[variable_id]
- xr_gd_change_variable(variable_id, value)
- if is_global_variable?(variable_id) && tmp != self[variable_id]
- XR_GlobalData.on_change(:variables, variable_id, self[variable_id])
- end
- end
- end
- #==============================================================================
- class Scene_Title
- #--------------------------------------------------------------------------
- alias xr_gd_command_new_game command_new_game
- def command_new_game
- xr_gd_command_new_game
- XR_GlobalData.load
- end
- end
- #==============================================================================
- class Scene_Load
- #--------------------------------------------------------------------------
- alias xr_gd_read_save_data read_save_data
- def read_save_data(file)
- xr_gd_read_save_data
- XR_GlobalData.load
- end
- end
- #==============================================================================
- # end
- #==============================================================================
复制代码
本帖来自P1论坛作者芯☆淡茹水,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址: https://rpg.blue/forum.php?mod=viewthread&tid=486533 若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。 |