Jackkel's Persistent Switches

Discussion in 'JS Plugin Releases (RMMV)' started by Jackkel Dragon, Oct 27, 2015.

  1. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV

    Persistent Data v5
    RPG Maker MV Plugin
    Jackkel Dragon




    Introduction
    A quick and simple plugin to allow the use of persistent switches (which do not reset their values between saved games).

    Features
    The ability to set switches/variables to be saved to a "persistent" save file, allowing them to persist across save games.
    An example of using this could be creating an ending list, or creating New Game+ elements that don't require a clear save file.

    How to Use
    - Copy the plugin code into a .js file and add it to your project's "js/plugins/" folder. (Be sure to use the name given in the code's header!)
    - Load the plugin in RPG Maker MV's plugin manager for your project.
    - Modify the persistent switch/variable IDs as needed. More information can be found in the help text.

    Notes
    - I created this plugin out of necessity for my own project, so it is very bare-bones and probably won't be updated often. Still, I hope it helps until someone comes up with a more expansive and user-friendly way to store persistent data.

    Updates
    - v1: First release. October 26th, 2015.
    - v2: (Hopefully) fixed a problem with deleting MV's default global save data and overwriting default functions. Changed the file persistent data gets saved to from "global" to "filePersistent". (I'll try to change the name later... I couldn't get a custom filename function to work properly.) October 26th, 2015.
    - v2 hotfix: Added a function to rename the persistent data file to "persistent". October 26th, 2015.
    - v2 hotfix 2: Fixed a problem where loading a saved game would reset persistent switches in some cases. October 26th, 2015.
    - v3: Changed the loading code so that it wouldn't re-write the persistent file multiple times on loading a game. October 27th, 2015.
    - v4: Added a parameter to turn off automatic saving of persistent data. A general expression parser was provided by ArcherBanish to allow for an undefined number of persistent switches. ArcherBanish also provided two plugin commands that allow forcing a persistent save/load. October 28th, 2015.
    - v5: Added support for persistent variables. Note that this requires deleting your persistent save data from earlier versions of the plugin to work.


    Plugin Code
    v5

    //=============================================================================
    // JKL_PersistentData.js
    //=============================================================================

    /*:
    * @plugindesc Jackkel's Persistent Data Plugin (v5)
    *
    * @author Jackkel Dragon
    *
    * @param Persistent Switches
    * @desc The ID of the persistent switches. Formatting example: 1-3,5-6,8,10-11,13
    * @Default 1-5
    *
    * @param Save Switches when Set
    * @desc Whether the persistent switches will be saved to the file the moment any switch is set. [true/false]
    * @Default true
    *
    * @param Persistent Variables
    * @desc The ID of the persistent variables. Formatting example: 1-3,5-6,8,10-11,13
    * @Default 1-5
    *
    * @param Save Variables when Set
    * @desc Whether the persistent variables will be saved to the file the moment any variable is set. [true/false]
    * @Default true
    *
    * @Help
    *
    * This plugin provides persistent switches and variables that are saved to the
    * "Persistent" save file, allowing you to set switches and variables that are
    * not tied to a save game. Uses for this could include designing an ending list
    * or creating New Game+ elements without requiring the use of a clear save
    * file.
    *
    * The value for each switch/variable in configuration is the switch ID in the
    * editor, without any leading zeroes ("10" instead of "0010").
    *
    * Example: Setting the persistent switches to "1-5,7" will set the switches with
    * IDs 1, 2, 3, 4, 5, and 7 to be saved to the persistent data file.
    *
    * Plugin Command: Persistent Save
    * Forces a save of the persistent data file.
    *
    * Plugin Command: Persistent Load
    * Forces the game to load from the persistent data file.
    *
    * Thanks to "ArcherBanish" for writing the general expression parser
    * to allow an undefined number of persistent switches and providing
    * the plugin commands.
    *
    * This plugin is free to use, even in commercial projects. Credit "Jackkel
    * Dragon" or "John Cooley".
    *
    */

    (function() {

    var parameters = PluginManager.parameters('JKL_PersistentData');

    var _Game_Switches_setValue = Game_Switches.prototype.setValue;
    Game_Switches.prototype.setValue = function(switchId, value, save) {
    save = typeof save !== 'undefined' ? save : true;
    _Game_Switches_setValue.call(this, switchId, value);
    if (save && eval(parameters['Save Switches when Set']))
    PersistManager.save();
    };

    var _Game_Variables_setValue = Game_Variables.prototype.setValue;
    Game_Variables.prototype.setValue = function(varId, value, save) {
    save = typeof save !== 'undefined' ? save : true;
    _Game_Variables_setValue.call(this, varId, value);
    if (save && eval(parameters['Save Variables when Set']))
    PersistManager.save();
    };

    var _DataManager_createGameObjects = DataManager.createGameObjects;
    DataManager.createGameObjects = function() {
    _DataManager_createGameObjects();
    PersistManager.load();
    };

    var _StorageManager_localFilePath = StorageManager.localFilePath;
    StorageManager.localFilePath = function(savefileId) {
    var name;
    if (savefileId === "Persistent")
    name = StorageManager.localFileDirectoryPath() + "persistent.rpgsave";
    else
    name = _StorageManager_localFilePath.call(this,savefileId);

    return name;
    };

    var _Scene_Load_onLoadSuccess = Scene_Load.prototype.onLoadSuccess;
    Scene_Load.prototype.onLoadSuccess = function() {
    _Scene_Load_onLoadSuccess.call(this);
    PersistManager.load();
    }

    //-----------------------------------------------------------------------------
    // Persistent Data Manager
    //
    // The static class that manages the persistent data.

    function PersistManager() {
    throw new Error('This is a static class');
    }

    PersistManager.load = function() {
    var json;
    var persist = {};
    try {
    json = StorageManager.load("Persistent", json);
    } catch (e) {
    console.error(e);
    }
    if (json) {
    persist = JSON.parse(json);
    }
    this.applyData(persist);
    };

    PersistManager.save = function() {
    StorageManager.save("Persistent", JSON.stringify(this.makeData()));
    };


    PersistManager.makeData = function() {
    var persistswexp = parameters['Persistent Switches'];
    var switches = XUtil.resolve(persistswexp);
    var persistvarexp = parameters['Persistent Variables'];
    var variables = XUtil.resolve(persistvarexp);
    var persist = {};
    persist.switches = [];
    persist.variables = [];
    for(var x of switches){
    persist.switches[x] = $gameSwitches.value(x);
    }
    for(var y of variables){
    persist.variables[y] = $gameVariables.value(y);
    }
    return persist;
    };

    PersistManager.applyData = function(persist) {
    var persistswexp = parameters['Persistent Switches'];
    var switches = XUtil.resolve(persistswexp);
    var persistvarexp = parameters['Persistent Variables'];
    var variables = XUtil.resolve(persistvarexp);
    if(typeof switches !== 'undefined' && typeof persist.switches !== 'undefined'){
    for(var x of switches){
    if(typeof x !== 'undefined'){
    $gameSwitches.setValue(x, persist.switches[x], false);
    }
    }
    }
    if(typeof variables !== 'undefined' && typeof persist.variables !== 'undefined'){
    for(var y of variables){
    if(typeof y !== 'undefined'){
    $gameVariables.setValue(y, persist.variables[y], false);
    }
    }
    }
    };

    PersistManager.readFlag = function(persist, name) {
    return !!persist[name];
    };

    function XUtil() {
    throw new Error('This is a static class');
    }

    XUtil.resolve = function(regex) {
    var allsemiexp = regex.split(',');
    var semiexp = null;
    var semisemiexp = null;
    var indexes = new Array();
    var curentindex = 0;
    for(semiexp of allsemiexp){
    semisemiexp = semiexp.split('-');
    if(semisemiexp.length > 1){
    y = Number(semisemiexp[1]);
    for(var x = Number(semisemiexp[0]); x <= y; x++){
    indexes[curentindex] = x;
    curentindex++;
    }
    }else{
    indexes[curentindex] = Number(semisemiexp[0]);
    curentindex++;
    }
    }
    return indexes;
    }

    var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
    _Game_Interpreter_pluginCommand.call(this, command, args);
    if (command === 'Persistent') {
    switch (args[0]) {
    case 'Save':
    PersistManager.save(parameters);
    break;
    case 'Load':
    PersistManager.load(parameters);
    break;
    }
    }
    }
    })();


    v4 (Formatting broken from forum changes)

    //=============================================================================// JKL_PersistentData.js//=============================================================================/*: * @plugindesc Jackkel's Persistent Switches Plugin (v4) * * @author Jackkel Dragon * * @param Persistent Switches * @desc The ID of the persistent switches. Formatting example: 1-3,5-6,8,10-11,13 * @Default 1-5 * * @param Save Switches when Set * @desc Whether the persistent switches will be saved to the file the moment any switch is set. [true/false] * @Default true * * @Help * * This plugin provides persistent switches that are saved to the * "Persistent" save file, allowing you to set switches that are not tied to a * save game. Uses for this could include designing an ending list or * creating New Game+ elements without requiring the use of a clear save * file. * * The switch numbers in the plugin configuration are for internal use only: * you don't need to remember which switch is assigned to which persistent * slot. The value for each switch in configuration is the switch ID in the * editor, without any leading zeroes ("10" instead of "0010"). * * Example: Setting the persistent switches to "1-5,7" will set the switches with * IDs 1, 2, 3, 4, 5, and 7 to be saved to the persistent data file. * * Plugin Command: Persistent Save * Forces a save of the persistent data file. * * Plugin Command: Persistent Load * Forces the game to load from the persistent data file. * * Thanks to "ArcherBanish" for writing the general expression parser * to allow an undefined number of persistent switches and providing * the plugin commands. * * This plugin is free to use, even in commercial projects. Credit "Jackkel * Dragon" or "John Cooley". * */(function() {var parameters = PluginManager.parameters('JKL_PersistentData');var _Game_Switches_setValue = Game_Switches.prototype.setValue;Game_Switches.prototype.setValue = function(switchId, value, save) { save = typeof save !== 'undefined' ? save : true; _Game_Switches_setValue.call(this, switchId, value); if (save && eval(parameters['Save Switches when Set'])) PersistManager.save(parameters);};var _DataManager_createGameObjects = DataManager.createGameObjects;DataManager.createGameObjects = function() { _DataManager_createGameObjects(); PersistManager.load(parameters);};var _StorageManager_localFilePath = StorageManager.localFilePath;StorageManager.localFilePath = function(savefileId) { var name; if (savefileId === "Persistent") name = StorageManager.localFileDirectoryPath() + "persistent.rpgsave"; else name = _StorageManager_localFilePath.call(this,savefileId); return name;};var _Scene_Load_onLoadSuccess = Scene_Load.prototype.onLoadSuccess;Scene_Load.prototype.onLoadSuccess = function() { _Scene_Load_onLoadSuccess.call(this); PersistManager.load(parameters);} //-----------------------------------------------------------------------------// Persistent Data Manager//// The static class that manages the persistent data.function PersistManager() { throw new Error('This is a static class');}PersistManager.load = function(parameters) { var json; var persist = {}; try { json = StorageManager.load("Persistent", json); } catch (e) { console.error(e); } if (json) { persist = JSON.parse(json); } this.applyData(persist, parameters);};PersistManager.save = function(parameters) { StorageManager.save("Persistent", JSON.stringify(this.makeData(parameters)));};PersistManager.makeData = function(parameters) { var persistexp = parameters['Persistent Switches']; var switches = XUtil.resolve(persistexp); var persist = []; for(var x of switches){ persist[x] = $gameSwitches.value(x); } return persist;};PersistManager.applyData = function(persist, parameters) { var persistexp = parameters['Persistent Switches']; var switches = XUtil.resolve(persistexp); for(var x of switches){ $gameSwitches.setValue(x, persist[x], false); }};PersistManager.readFlag = function(persist, name) { return !!persist[name];};function XUtil() { throw new Error('This is a static class');}XUtil.resolve = function(regex) { var allsemiexp = regex.split(','); var semiexp = null; var semisemiexp = null; var indexes = new Array(); var curentindex = 0; for(semiexp of allsemiexp){ semisemiexp = semiexp.split('-'); if(semisemiexp.length > 1){ y = Number(semisemiexp[1]); for(var x = Number(semisemiexp[0]); x <= y; x++){ indexes[curentindex] = x; curentindex++; } }else{ indexes[curentindex] = Number(semisemiexp[0]); curentindex++; } } return indexes;}var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;Game_Interpreter.prototype.pluginCommand = function(command, args) { _Game_Interpreter_pluginCommand.call(this, command, args); if (command === 'Persistent') { switch (args[0]) { case 'Save': PersistManager.save(parameters); break; case 'Load': PersistManager.load(parameters); break; } }}})();


    v3 (Formatting broken from forum changes)

    //=============================================================================// JKL_PersistentData.js//=============================================================================/*: * @plugindesc Jackkel's Persistent Switches Plugin (v3) * * @author Jackkel Dragon * * @param Persistent Switch #01 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #02 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #03 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #04 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #05 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #06 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #07 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #08 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #09 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #10 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #11 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #12 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #13 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #14 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #15 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #16 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #17 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #18 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #19 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #20 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @Help * * This plugin provides up to 20 persistent switches that are saved to the * "Persistent" save file, allowing you to set switches that are not tied to a * save game. Uses for this could include designing an ending list or * creating New Game+ elements without requiring the use of a clear save * file. * * The switch numbers in the plugin configuration are for internal use only: * you don't need to remember which switch is assigned to which persistent * slot. The value for each switch in configuration is the switch ID in the * editor. * * Example: Setting the first persistent switch to be 15 will cause the switch * with ID 15 to be saved to the persistent data whenever it is changed. * * Note: If you don't need all the switches, you *should* be able to set the * plugin configuration for those switches to '-1'. (Needs more testing.) * * This plugin is free to use, even in commercial projects. Credit "Jackkel * Dragon" or "John Cooley". * */(function() {var parameters = PluginManager.parameters('JKL_PersistentData'); var _Game_Switches_setValue = Game_Switches.prototype.setValue;Game_Switches.prototype.setValue = function(switchId, value, save) { save = typeof save !== 'undefined' ? save : true; _Game_Switches_setValue.call(this, switchId, value); if (save) PersistManager.save(parameters);};var _Scene_Title_create = Scene_Title.prototype.create;Scene_Title.prototype.create = function() { PersistManager.load(parameters); _Scene_Title_create.call(this);};var _DataManager_createGameObjects = DataManager.createGameObjects;DataManager.createGameObjects = function() { _DataManager_createGameObjects(); PersistManager.load(parameters);};var _StorageManager_localFilePath = StorageManager.localFilePath;StorageManager.localFilePath = function(savefileId) { var name; if (savefileId === "Persistent") name = StorageManager.localFileDirectoryPath() + "persistent.rpgsave"; else name = _StorageManager_localFilePath.call(this,savefileId); return name;};var _Scene_Load_onLoadSuccess = Scene_Load.prototype.onLoadSuccess;Scene_Load.prototype.onLoadSuccess = function() { _Scene_Load_onLoadSuccess.call(this); PersistManager.load(parameters);};//-----------------------------------------------------------------------------// Persistent Data Manager//// The static class that manages the persistent data.function PersistManager() { throw new Error('This is a static class');}PersistManager.load = function(parameters) { var json; var persist = {}; try { json = StorageManager.load("Persistent", json); } catch (e) { console.error(e); } if (json) { persist = JSON.parse(json); } this.applyData(persist, parameters);};PersistManager.save = function(parameters) { StorageManager.save("Persistent", JSON.stringify(this.makeData(parameters))); };PersistManager.makeData = function(parameters) { var persist = {}; persist.Persist01 = $gameSwitches.value(parameters['Persistent Switch #01']); persist.Persist02 = $gameSwitches.value(parameters['Persistent Switch #02']); persist.Persist03 = $gameSwitches.value(parameters['Persistent Switch #03']); persist.Persist04 = $gameSwitches.value(parameters['Persistent Switch #04']); persist.Persist05 = $gameSwitches.value(parameters['Persistent Switch #05']); persist.Persist06 = $gameSwitches.value(parameters['Persistent Switch #06']); persist.Persist07 = $gameSwitches.value(parameters['Persistent Switch #07']); persist.Persist08 = $gameSwitches.value(parameters['Persistent Switch #08']); persist.Persist09 = $gameSwitches.value(parameters['Persistent Switch #09']); persist.Persist10 = $gameSwitches.value(parameters['Persistent Switch #10']); persist.Persist11 = $gameSwitches.value(parameters['Persistent Switch #11']); persist.Persist12 = $gameSwitches.value(parameters['Persistent Switch #12']); persist.Persist13 = $gameSwitches.value(parameters['Persistent Switch #13']); persist.Persist14 = $gameSwitches.value(parameters['Persistent Switch #14']); persist.Persist15 = $gameSwitches.value(parameters['Persistent Switch #15']); persist.Persist16 = $gameSwitches.value(parameters['Persistent Switch #16']); persist.Persist17 = $gameSwitches.value(parameters['Persistent Switch #17']); persist.Persist18 = $gameSwitches.value(parameters['Persistent Switch #18']); persist.Persist19 = $gameSwitches.value(parameters['Persistent Switch #19']); persist.Persist20 = $gameSwitches.value(parameters['Persistent Switch #20']); return persist;};PersistManager.applyData = function(persist, parameters) { // read from the file this.Persist01 = this.readFlag(persist, 'Persist01'); this.Persist02 = this.readFlag(persist, 'Persist02'); this.Persist03 = this.readFlag(persist, 'Persist03'); this.Persist04 = this.readFlag(persist, 'Persist04'); this.Persist05 = this.readFlag(persist, 'Persist05'); this.Persist06 = this.readFlag(persist, 'Persist06'); this.Persist07 = this.readFlag(persist, 'Persist07'); this.Persist08 = this.readFlag(persist, 'Persist08'); this.Persist09 = this.readFlag(persist, 'Persist09'); this.Persist10 = this.readFlag(persist, 'Persist10'); this.Persist11 = this.readFlag(persist, 'Persist11'); this.Persist12 = this.readFlag(persist, 'Persist12'); this.Persist13 = this.readFlag(persist, 'Persist13'); this.Persist14 = this.readFlag(persist, 'Persist14'); this.Persist15 = this.readFlag(persist, 'Persist15'); this.Persist16 = this.readFlag(persist, 'Persist16'); this.Persist17 = this.readFlag(persist, 'Persist17'); this.Persist18 = this.readFlag(persist, 'Persist18'); this.Persist19 = this.readFlag(persist, 'Persist19'); this.Persist20 = this.readFlag(persist, 'Persist20'); // apply to the game itself $gameSwitches.setValue(parameters['Persistent Switch #01'], this.Persist01, false); $gameSwitches.setValue(parameters['Persistent Switch #02'], this.Persist02, false); $gameSwitches.setValue(parameters['Persistent Switch #03'], this.Persist03, false); $gameSwitches.setValue(parameters['Persistent Switch #04'], this.Persist04, false); $gameSwitches.setValue(parameters['Persistent Switch #05'], this.Persist05, false); $gameSwitches.setValue(parameters['Persistent Switch #06'], this.Persist06, false); $gameSwitches.setValue(parameters['Persistent Switch #07'], this.Persist07, false); $gameSwitches.setValue(parameters['Persistent Switch #08'], this.Persist08, false); $gameSwitches.setValue(parameters['Persistent Switch #09'], this.Persist09, false); $gameSwitches.setValue(parameters['Persistent Switch #10'], this.Persist10, false); $gameSwitches.setValue(parameters['Persistent Switch #11'], this.Persist11, false); $gameSwitches.setValue(parameters['Persistent Switch #12'], this.Persist12, false); $gameSwitches.setValue(parameters['Persistent Switch #13'], this.Persist13, false); $gameSwitches.setValue(parameters['Persistent Switch #14'], this.Persist14, false); $gameSwitches.setValue(parameters['Persistent Switch #15'], this.Persist15, false); $gameSwitches.setValue(parameters['Persistent Switch #16'], this.Persist16, false); $gameSwitches.setValue(parameters['Persistent Switch #17'], this.Persist17, false); $gameSwitches.setValue(parameters['Persistent Switch #18'], this.Persist18, false); $gameSwitches.setValue(parameters['Persistent Switch #19'], this.Persist19, false); $gameSwitches.setValue(parameters['Persistent Switch #20'], this.Persist20, false);};PersistManager.readFlag = function(persist, name) { return !!persist[name];};})();


    v2 (Formatting broken from forum changes)

    //=============================================================================// JKL_PersistentData.js//=============================================================================/*: * @plugindesc Jackkel's Persistent Switches Plugin (v2) * * @author Jackkel Dragon * * @param Persistent Switch #01 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #02 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #03 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #04 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #05 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #06 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #07 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #08 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #09 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #10 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #11 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #12 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #13 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #14 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #15 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #16 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #17 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #18 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #19 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @param Persistent Switch #20 * @desc The ID of the persistent switch. (-1 = unused) * @Default -1 * * @Help * * This plugin provides up to 20 persistent switches that are saved to the * "Persistent" save file, allowing you to set switches that are not tied to a * save game. Uses for this could include designing an ending list or * creating New Game+ elements without requiring the use of a clear save * file. * * The switch numbers in the plugin configuration are for internal use only: * you don't need to remember which switch is assigned to which persistent * slot. The value for each switch in configuration is the switch ID in the * editor. * * Example: Setting the first persistent switch to be 15 will cause the switch * with ID 15 to be saved to the persistent data whenever it is changed. * * Note: If you don't need all the switches, you *should* be able to set the * plugin configuration for those switches to '-1'. (Needs more testing.) * * This plugin is free to use, even in commercial projects. Credit "Jackkel *Dragon" or "John Cooley". * */(function() { var parameters = PluginManager.parameters('JKL_PersistentData'); var _Game_Switches_setValue = Game_Switches.prototype.setValue;Game_Switches.prototype.setValue = function(switchId, value) { _Game_Switches_setValue.call(this, switchId, value); PersistManager.save(parameters);};var _Scene_Title_create = Scene_Title.prototype.create;Scene_Title.prototype.create = function() { PersistManager.load(parameters); _Scene_Title_create.call(this);};var _DataManager_createGameObjects = DataManager.createGameObjects;DataManager.createGameObjects = function() { _DataManager_createGameObjects(); PersistManager.load(parameters);};var _StorageManager_localFilePath = StorageManager.localFilePath;StorageManager.localFilePath = function(savefileId) { var name; if (savefileId === "Persistent") name = StorageManager.localFileDirectoryPath() + "persistent.rpgsave"; else name = _StorageManager_localFilePath.call(this,savefileId); return name;};var _Scene_Load_onLoadSuccess = Scene_Load.prototype.onLoadSuccess;Scene_Load.prototype.onLoadSuccess = function() { _Scene_Load_onLoadSuccess.call(this); PersistManager.load(parameters);};//-----------------------------------------------------------------------------// Persistent Data Manager//// The static class that manages the persistent data.function PersistManager() { throw new Error('This is a static class');}PersistManager.load = function(parameters) { var json; var persist = {}; try { json = StorageManager.load("Persistent", json); } catch (e) { console.error(e); } if (json) { persist = JSON.parse(json); } this.applyData(persist, parameters);};PersistManager.save = function(parameters) { StorageManager.save("Persistent", JSON.stringify(this.makeData(parameters))); };PersistManager.makeData = function(parameters) { var persist = {}; persist.Persist01 = $gameSwitches.value(parameters['Persistent Switch #01']); persist.Persist02 = $gameSwitches.value(parameters['Persistent Switch #02']); persist.Persist03 = $gameSwitches.value(parameters['Persistent Switch #03']); persist.Persist04 = $gameSwitches.value(parameters['Persistent Switch #04']); persist.Persist05 = $gameSwitches.value(parameters['Persistent Switch #05']); persist.Persist06 = $gameSwitches.value(parameters['Persistent Switch #06']); persist.Persist07 = $gameSwitches.value(parameters['Persistent Switch #07']); persist.Persist08 = $gameSwitches.value(parameters['Persistent Switch #08']); persist.Persist09 = $gameSwitches.value(parameters['Persistent Switch #09']); persist.Persist10 = $gameSwitches.value(parameters['Persistent Switch #10']); persist.Persist11 = $gameSwitches.value(parameters['Persistent Switch #11']); persist.Persist12 = $gameSwitches.value(parameters['Persistent Switch #12']); persist.Persist13 = $gameSwitches.value(parameters['Persistent Switch #13']); persist.Persist14 = $gameSwitches.value(parameters['Persistent Switch #14']); persist.Persist15 = $gameSwitches.value(parameters['Persistent Switch #15']); persist.Persist16 = $gameSwitches.value(parameters['Persistent Switch #16']); persist.Persist17 = $gameSwitches.value(parameters['Persistent Switch #17']); persist.Persist18 = $gameSwitches.value(parameters['Persistent Switch #18']); persist.Persist19 = $gameSwitches.value(parameters['Persistent Switch #19']); persist.Persist20 = $gameSwitches.value(parameters['Persistent Switch #20']); return persist;};PersistManager.applyData = function(persist, parameters) { // read from the file this.Persist01 = this.readFlag(persist, 'Persist01'); this.Persist02 = this.readFlag(persist, 'Persist02'); this.Persist03 = this.readFlag(persist, 'Persist03'); this.Persist04 = this.readFlag(persist, 'Persist04'); this.Persist05 = this.readFlag(persist, 'Persist05'); this.Persist06 = this.readFlag(persist, 'Persist06'); this.Persist07 = this.readFlag(persist, 'Persist07'); this.Persist08 = this.readFlag(persist, 'Persist08'); this.Persist09 = this.readFlag(persist, 'Persist09'); this.Persist10 = this.readFlag(persist, 'Persist10'); this.Persist11 = this.readFlag(persist, 'Persist11'); this.Persist12 = this.readFlag(persist, 'Persist12'); this.Persist13 = this.readFlag(persist, 'Persist13'); this.Persist14 = this.readFlag(persist, 'Persist14'); this.Persist15 = this.readFlag(persist, 'Persist15'); this.Persist16 = this.readFlag(persist, 'Persist16'); this.Persist17 = this.readFlag(persist, 'Persist17'); this.Persist18 = this.readFlag(persist, 'Persist18'); this.Persist19 = this.readFlag(persist, 'Persist19'); this.Persist20 = this.readFlag(persist, 'Persist20'); // apply to the game itself $gameSwitches.setValue(parameters['Persistent Switch #01'], this.Persist01); $gameSwitches.setValue(parameters['Persistent Switch #02'], this.Persist02); $gameSwitches.setValue(parameters['Persistent Switch #03'], this.Persist03); $gameSwitches.setValue(parameters['Persistent Switch #04'], this.Persist04); $gameSwitches.setValue(parameters['Persistent Switch #05'], this.Persist05); $gameSwitches.setValue(parameters['Persistent Switch #06'], this.Persist06); $gameSwitches.setValue(parameters['Persistent Switch #07'], this.Persist07); $gameSwitches.setValue(parameters['Persistent Switch #08'], this.Persist08); $gameSwitches.setValue(parameters['Persistent Switch #09'], this.Persist09); $gameSwitches.setValue(parameters['Persistent Switch #10'], this.Persist10); $gameSwitches.setValue(parameters['Persistent Switch #11'], this.Persist11); $gameSwitches.setValue(parameters['Persistent Switch #12'],this.Persist12); $gameSwitches.setValue(parameters['Persistent Switch #13'], this.Persist13); $gameSwitches.setValue(parameters['Persistent Switch #14'], this.Persist14); $gameSwitches.setValue(parameters['Persistent Switch #15'], this.Persist15); $gameSwitches.setValue(parameters['Persistent Switch #16'], this.Persist16); $gameSwitches.setValue(parameters['Persistent Switch #17'], this.Persist17); $gameSwitches.setValue(parameters['Persistent Switch #18'], this.Persist18); $gameSwitches.setValue(parameters['Persistent Switch #19'], this.Persist19); $gameSwitches.setValue(parameters['Persistent Switch #20'], this.Persist20);};PersistManager.readFlag = function(persist, name) { return !!persist[name];};})();

    Potential Hotfix for problems in v5

    var _DataManager_createGameObjects = DataManager.createGameObjects;
    DataManager.createGameObjects = function() {
    _DataManager_createGameObjects();
    PersistManager.load();
    };

    // Changed to:

    var _DataManager_createGameObjects = DataManager.createGameObjects;
    DataManager.createGameObjects = function() {
    _DataManager_createGameObjects.call(this);
    PersistManager.load();
    };


    Credits
    ArcherBanish: General expression parser for the persistent switches parameter and the plugin commands.

    Terms
    Free to use, even for commercial projects. Credit "Jackkel Dragon" or "John Cooley".
     
    Last edited: Jul 12, 2017
    #1
    Verdelite, Aureus, Roguedeus and 16 others like this.
  2. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Found a nasty bug: v1 of this plugin can destroy the global data that MV uses by default to check the number of save files, making it impossible to load saved games in some cases. I'm working on a fix to make sure the data is preserved, so it might be best to wait for v2.


    Edit: I *think* I fixed the problem, and also managed to avoid overriding the default functions. Unfortunately, I had to move the persistent data into a separate file from the global data to protect the default global data. For now the file is called "filePersistent", but I'd like to be able to change that soon...


    I've updated the opening post with v2 of the script, which should now work fine. That said, let me know if you catch any bugs that I miss.


    Edit 2: Added a function to the plugin to rename the persistent data file to "persistent".
     
    Last edited by a moderator: Oct 27, 2015
    #2
  3. ArcherBanish

    ArcherBanish Veteran Veteran

    Messages:
    36
    Likes Received:
    14
    First Language:
    Portuguese
    I have not looked at this plugin allot but wouldn't it be possible to just ignore the 20 Switch limit by using some sort of regular expression "1-7,53-75" or even just a list of numbers "1 3 5 6 7 12 21 43".

    And by adding an index key to the file that would contain the string defined so you could read them afterwords.

    Example:

    IndexKey: 1-3,5-71: true2: false3: true5: false6: false7: trueJust a sugestion to make the plugin more flexible of course it would take some work to have a string parser for the expression to be used.

    Also two sugestions I have would be to set the default to -1 on all the switches or maybe just the last 17 or something otherwise someone who just wants 1 or 2 plugins will have to go to each one indiviualy to turn them off.
     
    Last edited by a moderator: Oct 27, 2015
    #3
    Prescott likes this.
  4. Dacara

    Dacara Villager Member

    Messages:
    5
    Likes Received:
    0
    First Language:
    Polish
    Hello,

    1st of all, thank you for the plugin :) It's just what I need.

    2nd of all, somehow, I can't get it to work.

    I've added it in the correct folder, it shows up in the plug-in menu and all, allows me to change variables to the ones I want and... it doesn't work.

    Example:

    I've tried to create a per switch that allows you to open a chest at the beginning of a new game if you game overed in a specific place last time. I set up the switch in the control switch menu (let's say 0181) , entered is as a per switch in the plugin and then did the new event on the chest. When the character dies in a specific place I hit  New game but the chest reacts as if the 0181 is off.

    Maybe I'm doing something wrong? If you can give any advice it would be appreciated.

    Cheers.
     
    #4
  5. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    @ArcherBanish: I'm not sure I'd be able to code something like that, since I'm not very good with regular expressions. I can look into it when I have time, but for now I'm afraid extending the plugin will involve adding more parameters.


    Your second suggestion of setting the parameters to default to unused is a good idea, so I went ahead and did that (and fixed the parameter descriptions).


    @Dacara: From my testing, it looks like you need to leave off the leading zeros in switch IDs. For instance, you'd put "1" instead of "0001", or in your case "181" instead of "0181". See if that works, and let me know if it's still broken.
     
    #5
  6. Dacara

    Dacara Villager Member

    Messages:
    5
    Likes Received:
    0
    First Language:
    Polish
    Hey, thanks for the answer :)

    I did input it without the 0 at first and then changed it when it didn't work. I checked again just now ,just to be sure, and the switch is still off when I try new game.

    And just to be absolutely sure I'm doing my part ok (since I am a noob at plugins, scripts and all things coded), I change the variables in the plugin menu in rm mv and I don't need to poke in the script file, right?
     
    #6
  7. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    That's right, you should be able to set up what you need just from the plugin manager.


    I'm not sure what the problem might be for you... I suggest re-copying the current code from the opening post first, just in case you were using one of the versions that didn't work properly. From what I understand, you might have to open the plugin and press OK in the plugin manager after changing the code (if the problem is with the plugin parameters). When you start up your game, it should create a file named "persistent.rpgsave" in your game's save folder as soon as you reach the title screen, so make sure that works.


    If re-copying the code doesn't work, I'm not sure what the problem might be. I might need to put together a demo project...


    Edit: What is the filename of the plugin .js you used? I checked the file used for storing plugin parameters, and it seems to use the filename of the .js file to store the parameters. The code is searching for "JKL_PersistentData" for its parameters, so the filename would have to be "JKL_PersistentData.js" (not case sensitive, it seems). Since this is a problem with parameters, this WOULD require re-opening the plugin in the plugin manager.
     
    Last edited by a moderator: Oct 28, 2015
    #7
  8. Dacara

    Dacara Villager Member

    Messages:
    5
    Likes Received:
    0
    First Language:
    Polish
    Hey.

    I re-copied but still no luck. On the other hand I do have the persistent.rpgsave it just doesn't seem to be doing anything :(

    Thanks for all the help, tho :)

    I still have way to go coding-wise before I really need the per save data, maybe something will happen until then :)

    Cheers and thank for the help!
     
    #8
  9. Prescott

    Prescott argggghhh Veteran

    Messages:
    423
    Likes Received:
    227
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Nowhere in here did I see that you actually saved your game after getting the chest and before dying ;) if you did, that's a bit of an issue... but I'm guessing you didn't. This plugin needs you to save your file first I think, it doesn't automatically store them when a switch is turned on.

    Now, I'm not a plugin scripter by any means, and I have no idea how to read or write JS (yet), but it would seem that that would be the case, otherwise the plugin would have to be always running checking for those switches to be on or off and it could potentially eat up a lot of resources. It would be much faster for it to only be called when you save your game, and check for the switches there.

    Hopefully that works for you :)

    Also, thank you very much for this Jackkel. This is definitely one of the things I was looking for to be used in my project!!

    Something cool about this is that you could make 20 new game pluses if you wanted to. As in, each playthrough another switch is turned on and you unlock more stuff. Sure, that's probably not the best idea design-wise, but it's still cool that it's an option :) maybe change little things, or have 5 new game pluses.
     
    #9
  10. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    @Dacara: I'm not sure what might be happening... If you continue to not be able to get it to work I'll consider uploading a demo project showing how it's done.


    @ReddElite: Actually, this plugin is set to save the persistent data every time a switch is set (persistent or not). As someone pointed out to me, this actually bloats startup a bit because it saves the persistent data 20 times before showing the title screen (when setting data from the file to the game switches), even though no changes are being made to the file. I'm going to see if I can fix this, but even if I do the persistent data will still be saved on every switch change in-game.


    Edit: I posted v3's code to the opening post, but I left v2's code just in case I missed a bug during testing. What v3 attempts to fix is the unnecessary saving of the persistent file when starting or loading a game. If the fix worked, nothing will change for using the plugin, it'll just mess with the persistent file a bit less when loading games.
     
    Last edited by a moderator: Oct 28, 2015
    #10
  11. Prescott

    Prescott argggghhh Veteran

    Messages:
    423
    Likes Received:
    227
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Interesting! That's pretty cool that it doesn't need to be saved though, actually.

    Everything seems to be working for me. Dacara, would you mind posting a picture (or a few pictures) of your events and plugin setup so we can better understand what is going wrong? One small unnoticeable thing could need to be changed, happens to the best of us x)
     
    #11
  12. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    I posted the code for v4, which removes the 20-switch limit. Credit goes to ArcherBanish, who provided the general expression parser to allow this to work.


    ArcherBanish also provided two plugin commands that allow you to force a save or load of the persistent data from an event: "Persistent Save" and "Persistent Load".


    To go with those plugin commands, I added a parameter to the plugin that allows you to turn off the automatic saving of persistent data when switches are set. With this off, you'll have to use the plugin command "Persistent Save" to save the persistent data, which allows you more control on when the switches are saved.
     
    #12
  13. Dacara

    Dacara Villager Member

    Messages:
    5
    Likes Received:
    0
    First Language:
    Polish
    Hello

    @ReddElite, As it happens, I did save the game... and did many other things that were, rationally speaking, totally unrelated with the switches but I thought may have any influence on... anything, basically.

    Here are 3 prtscr 1st one is the plugin and the variables set

    [​IMG]
    2nd one is for the switch
    [​IMG]
    3rd one is for the switch check.
    [​IMG]

    As you can see, the switches are the simplest ones possible- I just wanted to try out the plugin before doing anything more complicated. I failed at making it work thus I didn't move past the basics.

    If you can spot anything you may think would work, I'd appreciate some help. (And thanks for taking time to answer in the 1st place :) ).

    @Jackkel Dragon  As you can see I'm still stuck :p

    And I also had an error with the v4 of the code. I got an "cannot read property split of undefined" or something every time I tried the play test with the plugin On. I poked around and this may be the problem?

    XUtil.resolve = function(regex) {
        var allsemiexp = regex.split(',');
    var semiexp = null;
    var semisemiexp = null;
        var indexes = new Array();
        var curentindex = 0;
        for(semiexp of allsemiexp){
            semisemiexp = semiexp.split('-');
    Or is it only me again...?
     
    #13
  14. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    @Dacara: I'm not sure what could be going wrong. I tried opening a new project and using the same settings you have for the plugin, but everything works fine for me.


    For now, I've put together a simple demo based on your screenshots that works for me. I'll probably take the folder down eventually, but for now the basic demo can be found here. Let me know if this works properly or not, because it all checks out for me.


    The only thing I can think of is what I mentioned before, that maybe if the file of the javascript file isn't "JKL_PersistentData.js", maybe it won't find the parameters properly...
     
    #14
  15. Dacara

    Dacara Villager Member

    Messages:
    5
    Likes Received:
    0
    First Language:
    Polish
    ...it's always the simplest things, isn't it?

    @Jackkel Dragon I changed the name of the plugin and it works perfectly    TT ^ TT b

    Great thanks for your help and sorry for taking up so much of your time.
     
    #15
  16. Prescott

    Prescott argggghhh Veteran

    Messages:
    423
    Likes Received:
    227
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Hey Jackkel, I was wondering if you could make this same plugin, just using a variable instead of switches?

    I wanted to store what playthrough the player was on (i.e. how many times they had completed the game) and display it on the title screen (that is going to be evented). It could obviously be used for more than just that, and for more uses than NGP stuff, but I figured it would be very nice to have a persistent variable as well :)

    That way, instead of being limited to only 20 switches, you could have different things happening the 80th time someone plays through your game (a bit overboard I know, but still).
     
    #16
  17. Jackkel Dragon

    Jackkel Dragon Veteran Veteran

    Messages:
    45
    Likes Received:
    24
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    I updated the code to work with variables as well as switches. As ArcherBanish pointed out to me, this will break your existing "persistent.rpgsave" file, so you'll have to delete that file for this version to save variables properly. If you are replacing an older version of the plugin, you'll also have to go into the plugin manager and set the new parameters or the game will not run at all.
     
    #17
  18. Prescott

    Prescott argggghhh Veteran

    Messages:
    423
    Likes Received:
    227
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Oh awesome :D thanks :)
     
    #18
  19. DarkWolfInsanity

    DarkWolfInsanity Retro RPG Recreator Member

    Messages:
    24
    Likes Received:
    5
    Location:
    Everfrost, Southern Tetsuga
    First Language:
    English
    I can see Undertale MV versions with this, seeing how Undertale is in general with its saves. I give thumbs up.
     
    #19
  20. ookami_lord

    ookami_lord Best Lemon in the world Veteran

    Messages:
    88
    Likes Received:
    15
    Location:
    Portugal
    First Language:
    portuguese
    Primarily Uses:
    N/A
    Ah...just like your CP games uh? ;)

    Thanks for this!
     
    #20

Share This Page