查看: 119|回复: 0

[转载发布] 【VA】全局开关和变量

[复制链接]
  • TA的每日心情
    开心
    昨天 10:15
  • 签到天数: 73 天

    连续签到: 1 天

    [LV.6]常住居民II

    2044

    主题

    86

    回帖

    8602

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    -2
    卡币
    6460
    OK点
    16
    积分
    8602
    发表于 2024-4-12 16:45:14 | 显示全部楼层 |阅读模式
    [Ruby] 纯文本查看 复制代码
    #==============================================================================
    # 〓 全局开关和变量 〓      By: 芯☆淡茹水
    #==============================================================================
    # 全局开关或变量,在取名时在名字前面加上 $ 符号
    #==============================================================================
    module XR_GlobalData
      #--------------------------------------------------------------------------
      File_name = "GlobalData"
      #--------------------------------------------------------------------------
      def self.filename
        return File_name + ".rxdata2"
      end
      #--------------------------------------------------------------------------
      def self.make_empty_data
        return {:switches=>{}, :variables=>{}}
      end
      #--------------------------------------------------------------------------
      def self.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
      #--------------------------------------------------------------------------
      def self.load
        @data = self.make_empty_data
        if FileTest.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)
        return false if !switch_id
        name = $data_system.switches[switch_id]
        return !!name && !!name.match(/^\$/)
      end
      #--------------------------------------------------------------------------
      def synchro(hash)
        hash.keys.each do |id|
          @data[id] = hash[id]
          on_change
        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)
        return false if !variable_id
        name = $data_system.variables[variable_id]
        return !!name && !!name.match(/^\$/)
      end
      #--------------------------------------------------------------------------
      def synchro(hash)
        hash.keys.each do |id|
          @data[id] = hash[id]
          on_change
        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 << DataManager
      #--------------------------------------------------------------------------
      alias xr_gd_create_game_objects create_game_objects
      def create_game_objects
        xr_gd_create_game_objects
        XR_GlobalData.load
      end
      #--------------------------------------------------------------------------
      alias xr_gd_load_game_without_rescue load_game_without_rescue
      def load_game_without_rescue(index)
        result = xr_gd_load_game_without_rescue(index)
        XR_GlobalData.load
        return result
      end
    end
    #==============================================================================
    # end
    #==============================================================================
    天天去同能,天天有童年!
    回复 论坛版权

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2024-9-21 16:27 , Processed in 0.053537 second(s), 43 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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