/*============================================================================= VariableCommon.js ---------------------------------------------------------------------------- (C)2024 Triacontane This software is released under the MIT License. http://opensource.org/licenses/mit-license.php ---------------------------------------------------------------------------- Version 1.0.0 2024/04/30 初版 ---------------------------------------------------------------------------- [Blog] : https://triacontane.blogspot.jp/ [Twitter]: https://twitter.com/triacontane/ [GitHub] : https://github.com/triacontane/ =============================================================================*/ /*: * @plugindesc 共有変数スイッチプラグイン * @target MZ * @url https://github.com/triacontane/RPGMakerMV/tree/mz_master/VariableCommon.js * @base PluginCommonBase * @orderAfter PluginCommonBase * @author トリアコンタン * * @param startVariableId * @text 共有変数ID(開始) * @desc 指定した範囲内の変数が共有変数として扱われます。 * @default 0 * @type variable * * @param endVariableId * @text 共有変数ID(終了) * @desc 指定した範囲内の変数が共有変数として扱われます。 * @default 0 * @type variable * * @param startSwitchId * @text 共有スイッチID(開始) * @desc 指定した範囲内のスイッチが共有スイッチとして扱われます。 * @default 0 * @type switch * * @param endSwitchId * @text 共有スイッチID(終了) * @desc 指定した範囲内のスイッチが共有スイッチとして扱われます。 * @default 0 * @type switch * * @command SAVE_COMMON_VARIABLE * @text 共有変数の保存 * @desc 変更した共有変数を保存します。 * * @help VariableCommon.js * * 異なるセーブデータ間で共有できる変数とスイッチを定義できます。 * 指定した範囲内の変数、スイッチは別のデータをロードしたり、 * タイトルに戻っても値が引き継がれます。 * パフォーマンス維持のため、保存の際は明示的にコマンドを実行してください。 * * 共有変数のデータはconfig.rmmzsaveに保存されます。 * 当該ファイルを削除すると共有変数のデータも消失します。 * * このプラグインの利用にはベースプラグイン『PluginCommonBase.js』が必要です。 * 『PluginCommonBase.js』は、RPGツクールMZのインストールフォルダ配下の * 以下のフォルダに格納されています。 * dlc/BasicResources/plugins/official * * 利用規約: * 作者に無断で改変、再配布が可能で、利用形態(商用、18禁利用等) * についても制限はありません。 * このプラグインはもうあなたのものです。 */ (() => { "use strict"; const script = document.currentScript; const param = PluginManagerEx.createParameter(script); PluginManagerEx.registerCommand(script, "SAVE_COMMON_VARIABLE", () => { ConfigManager.saveCommonVariables(); }); Game_Variables.prototype.findCommonVariables = function () { const map = {}; for (let i = param.startVariableId; i <= param.endVariableId; i++) { map[i] = this.value(i); } return map; }; Game_Variables.prototype.applyCommonVariables = function (map = {}) { for (const key in map) { if (key >= param.startVariableId && key <= param.endVariableId) { this.setValue(key, map[key]); } } }; //if ([502, 503, 504, 505, 506, 507].includes(switchId)) { // Game_Variables.prototype.setValueShare = function (variableId, value) { // if (variableId > 0 && variableId < $dataSystem.variables.length) { // if (typeof value === "number") { // value = Math.floor(value); // } // this._data[variableId] = value; // this.onChange(); // } // }; Game_Switches.prototype.findCommonSwitches = function () { const map = {}; for (let i = param.startSwitchId; i <= param.endSwitchId; i++) { map[i] = this.value(i); } return map; }; Game_Switches.prototype.applyCommonSwitches = function (map = {}) { for (const key in map) { if (key >= param.startSwitchId && key <= param.endSwitchId) { this.setValue(key, map[key]); } } }; // 保存が必要な場合 // Game_Switches.prototype.setValueShare = function (switchId, value) { // if (switchId > 0 && switchId < $dataSystem.switches.length) { // this._data[switchId] = value; // if (500 < switchId && switchId < 801) { // ConfigManager.saveCommonVariables(); // } // this.onChange(); // } // }; const _ConfigManager_makeData = ConfigManager.makeData; ConfigManager.makeData = function () { const config = _ConfigManager_makeData.apply(this, arguments); config.commonVariables = this._commonVariables; config.commonSwitches = this._commonSwitches; return config; }; const _ConfigManager_applyData = ConfigManager.applyData; ConfigManager.applyData = function (config) { _ConfigManager_applyData.apply(this, arguments); this._commonVariables = config.commonVariables; this._commonSwitches = config.commonSwitches; }; // コンフィグ閉じるとき自動的にセーブ ConfigManager.saveCommonVariables = function () { this.makeData(); this._commonVariables = $gameVariables.findCommonVariables(); this._commonSwitches = $gameSwitches.findCommonSwitches(); this.save(); }; var Scene_Save_executeSave = Scene_Save.prototype.executeSave; Scene_Save.prototype.executeSave = function (savefileId) { Scene_Save_executeSave.call(this, savefileId); ConfigManager.saveCommonVariables(); }; ConfigManager.loadCommonVariables = function () { $gameVariables.applyCommonVariables(this._commonVariables); $gameSwitches.applyCommonSwitches(this._commonSwitches); }; const _DataManager_createGameObjects = DataManager.createGameObjects; DataManager.createGameObjects = function () { _DataManager_createGameObjects.apply(this, arguments); ConfigManager.loadCommonVariables(); }; const _DataManager_extractSaveContents = DataManager.extractSaveContents; DataManager.extractSaveContents = function (contents) { _DataManager_extractSaveContents.apply(this, arguments); ConfigManager.loadCommonVariables(); }; })();