赞 | 13 |
VIP | 0 |
好人卡 | 0 |
积分 | 21 |
经验 | 0 |
最后登录 | 2024-10-17 |
在线时间 | 109 小时 |
Lv3.寻梦者
- 梦石
- 1
- 星屑
- 1133
- 在线时间
- 109 小时
- 注册时间
- 2017-11-1
- 帖子
- 79
|
这个脚本应该能达到你的要求,虽然我还没有试过
- #encoding:utf-8
- =begin
- *******************************************************************************************
- * 遊戲共用變數 *
- for RGSS3
- Ver 1.2.0 2015.07.30
- 原作者:魂(Lctseng),巴哈姆特論壇ID:play123
- 轉載請保留此標籤
- 個人小屋連結:http://home.gamer.com.tw/homeindex.php?owner=play123
- 主要功能:
- 一、設定某些範圍的開關和變量將永遠在遊戲間共用
- 更新紀錄:
- Ver 1.0.0 :
- 日期:2014.06.30
- 摘要:■、最初版本
- ■、功能:
- 一、設定某些範圍的開關和變量將永遠在遊戲間共用
- Ver 1.1.0 :
- 日期:2014.08.15
- 摘要:■、錯誤修正:
- 一、打包後的遊戲專案使用會有找不到檔案的問題
- Ver 1.2.0 :
- 日期:2015.07.30
- 摘要:■、新增安全機制
- 撰寫摘要:一、此腳本修改或重新定義以下類別:
- ■ Game_Switches
- ■ Game_Variables
- 二、此腳本新定義以下類別和模組:
- ■ Lctseng::PermanentlyVariables
- ■ Game_PermanentlyVariables
- *******************************************************************************************
- =end
- #encoding:utf-8
- #==============================================================================
- # ■ Lctseng::PermanentlyVariables
- #------------------------------------------------------------------------------
- # 管理模組
- #==============================================================================
- module Lctseng
- module PermanentlyVariables
- #--------------------------------------------------------------------------
- # ● 設定永久的開關和變量範圍
- #--------------------------------------------------------------------------
- # 開關:10~20
- SWITCH_RANGE = Range.new(10,20)
- # 變量:10~20
- VARIABLE_RANGE = Range.new(10,20)
- #--------------------------------------------------------------------------
- # ● 安全設定:設定兩個很大的質數,以避免永久存檔遭到修改
- #--------------------------------------------------------------------------
- PRIME_1 = 179424779
- PRIME_2 = 179428399
- end
- end
- #*******************************************************************************************
- #
- # 請勿修改從這裡以下的程式碼,除非你知道你在做什麼!
- # DO NOT MODIFY UNLESS YOU KNOW WHAT TO DO !
- #
- #*******************************************************************************************
- #--------------------------------------------------------------------------
- # ★ 紀錄腳本資訊
- #--------------------------------------------------------------------------
- if !$lctseng_scripts
- $lctseng_scripts = {}
- end
- $lctseng_scripts[:permanently_variable] = "1.2.0"
- puts "載入腳本:Lctseng - 遊戲共用變數,版本:#{$lctseng_scripts[:permanently_variable]}"
- #encoding:utf-8
- #==============================================================================
- # ■ Lctseng::PermanentlyVariables
- #------------------------------------------------------------------------------
- # 管理模組
- #==============================================================================
- module Lctseng
- module PermanentlyVariables
- #--------------------------------------------------------------------------
- # ● 讀取共用開關
- #--------------------------------------------------------------------------
- def self.get_switch(switch_id)
- ensure_record
- $game_permanent.get_switch(switch_id)
- end
- #--------------------------------------------------------------------------
- # ● 寫入共用開關
- #--------------------------------------------------------------------------
- def self.set_switch(switch_id,value)
- ensure_record
- $game_permanent.set_switch(switch_id,value)
- sync_record
- end
- #--------------------------------------------------------------------------
- # ● 讀取共用變數
- #--------------------------------------------------------------------------
- def self.get_variable(switch_id)
- ensure_record
- $game_permanent.get_variable(switch_id)
- end
- #--------------------------------------------------------------------------
- # ● 寫入共用變數
- #--------------------------------------------------------------------------
- def self.set_variable(var_id,value)
- ensure_record
- $game_permanent.set_variable(var_id,value)
- sync_record
- end
- #--------------------------------------------------------------------------
- # ● 確保共用資訊存在於記憶體
- #--------------------------------------------------------------------------
- def self.ensure_record
- if $game_permanent.nil?
- $game_permanent = load_record
- end
- end
- #--------------------------------------------------------------------------
- # ● 讀取永久變數,若不存在,則創立新物件
- #--------------------------------------------------------------------------
- def self.load_record
- data = nil
- begin
- data = load_data("Permanent.dat")
- if data.check_checksum
- puts "已讀取存在的檔案"
- else
- puts "檔案遭到修改!"
- raise RuntimeError, "檔案損壞!"
- end
- rescue
- puts "檔案不存在或損壞!創立新物件"
- data = Game_PermanentlyVariables.new
- end
- return data
- end
- #--------------------------------------------------------------------------
- # ● 同步永久變數,將寫入檔案
- #--------------------------------------------------------------------------
- def self.sync_record
- puts "正在同步檔案"
- $game_permanent.generate_checksum
- save_data($game_permanent, "Permanent.dat")
- end
- end
- end
- #encoding:utf-8
- #==============================================================================
- # ■ Game_PermanentlyVariables
- #------------------------------------------------------------------------------
- # 儲存永久不變的開關與變數
- # 只是個套殼的Hash
- #==============================================================================
- class Game_PermanentlyVariables
- #--------------------------------------------------------------------------
- # ● 初始化
- #--------------------------------------------------------------------------
- def initialize
- clear
- end
- #--------------------------------------------------------------------------
- # ● 清除
- #--------------------------------------------------------------------------
- def clear
- @switchs = {}
- @variables = {}
- @checksum = 0
- end
- #--------------------------------------------------------------------------
- # ● 產生檢查碼
- #--------------------------------------------------------------------------
- def generate_checksum
- @checksum = calculate_checksum
- end
- #--------------------------------------------------------------------------
- # ● 計算檢查碼
- #--------------------------------------------------------------------------
- def calculate_checksum
- checksum = 0
- @switchs.each_pair do |id,val|
- checksum += (id*Lctseng::PermanentlyVariables::PRIME_1 * (val ? -1 : 1))
- end
- @variables.each_pair do |id,val|
- checksum += (id*Lctseng::PermanentlyVariables::PRIME_1 + val*Lctseng::PermanentlyVariables::PRIME_2)
- end
- puts "檢查碼:#{checksum}"
- return checksum
- end
- #--------------------------------------------------------------------------
- # ● 測試檢查碼
- #--------------------------------------------------------------------------
- def check_checksum
- @checksum == calculate_checksum
- end
- #--------------------------------------------------------------------------
- # ● 取得開關
- #--------------------------------------------------------------------------
- def get_switch(switch_id)
- @switchs[switch_id] || false
- end
- #--------------------------------------------------------------------------
- # ● 設定開關
- #--------------------------------------------------------------------------
- def set_switch(switch_id,value)
- @switchs[switch_id] = value
- end
- #--------------------------------------------------------------------------
- # ● 取得變數
- #--------------------------------------------------------------------------
- def get_variable(var_id)
- @variables[var_id] || 0
- end
- #--------------------------------------------------------------------------
- # ● 設定變數
- #--------------------------------------------------------------------------
- def set_variable(var_id,value)
- @variables[var_id] = value
- end
- end
- #encoding:utf-8
- #==============================================================================
- # ■ Game_Switches
- #------------------------------------------------------------------------------
- # 處理開關的類。本質上是套了個殼的 Array 。本類的實例請參考 $game_switches 。
- #==============================================================================
- class Game_Switches
- #--------------------------------------------------------------------------
- # ● 獲取開關 - 重新定義
- #--------------------------------------------------------------------------
- alias :lctseng_get :[]
- #--------------------------------------------------------------------------
- def [](switch_id)
- if Lctseng::PermanentlyVariables::SWITCH_RANGE.include?(switch_id)
- puts "讀取共用開關:#{switch_id}"
- return Lctseng::PermanentlyVariables.get_switch(switch_id)
- else
- puts "讀取普通開關:#{switch_id}"
- return lctseng_get(switch_id)
- end
- end
- #--------------------------------------------------------------------------
- # ● 設置開關 - 重新定義
- # value : 開啟 (true) / 關閉 (false)
- #--------------------------------------------------------------------------
- alias :lctseng_set :[]=
- #--------------------------------------------------------------------------
- def []=(switch_id, value)
- if Lctseng::PermanentlyVariables::SWITCH_RANGE.include?(switch_id)
- puts "寫入共用開關:#{switch_id}:#{value}"
- Lctseng::PermanentlyVariables.set_switch(switch_id, value)
- on_change
- else
- puts "寫入普通開關:#{switch_id}:#{value}"
- lctseng_set(switch_id, value)
- end
- end
- end
- #encoding:utf-8
- #==============================================================================
- # ■ Game_Variables
- #------------------------------------------------------------------------------
- # 處理變量的類。本質上是套了個殼的 Array 。本類的實例請參考 $game_variables 。
- #==============================================================================
- class Game_Variables
- #--------------------------------------------------------------------------
- # ● 獲取變物 - 重新定義
- #--------------------------------------------------------------------------
- alias :lctseng_get :[]
- #--------------------------------------------------------------------------
- def [](variable_id)
- if Lctseng::PermanentlyVariables::VARIABLE_RANGE.include?(variable_id)
- puts "讀取共用變數:#{variable_id}"
- return Lctseng::PermanentlyVariables.get_variable(variable_id)
- else
- puts "讀取普通變數:#{variable_id}"
- return lctseng_get(variable_id)
- end
- end
- #--------------------------------------------------------------------------
- # ● 設置變數 - 重新定義
- #--------------------------------------------------------------------------
- alias :lctseng_set :[]=
- #--------------------------------------------------------------------------
- def []=(variable_id, value)
- if Lctseng::PermanentlyVariables::VARIABLE_RANGE.include?(variable_id)
- puts "寫入共用變數:#{variable_id}:#{value}"
- Lctseng::PermanentlyVariables.set_variable(variable_id, value)
- on_change
- else
- puts "寫入普通變數:#{variable_id}:#{value}"
- lctseng_set(variable_id, value)
- end
- end
- end
复制代码 |
|