ezmash

Linked Switches (MV)

Nov 10th, 2015
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Linked Switches
  3. // by Shaz
  4. // Last Updated: 2015.11.11
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows a switch to be linked to a statement to be evaluated
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  * This plugin allows you to link a switch with a command.  When the value
  13.  * of the switch is checked (in a Conditional Branch, for example), the
  14.  * command will be run, and the result will be returned as the switch value.
  15.  *
  16.  * Links can be set up in two different ways:
  17.  * You can use a plugin command to link a switch with a command.  This means
  18.  * you don't have to touch any Javascript code, but it means the commands
  19.  * will be saved in the save file, slightly increasing the size, and making
  20.  * it visible to players who could attempt to edit it.
  21.  * Or you can add the links within the plugin code.  This means you have to
  22.  * change the script, but it keeps the commands out of the save files and
  23.  * away from the player.
  24.  *
  25.  * To add a link using a plugin call, enter it in the following format:
  26.  *   LinkSwitch switchId command
  27.  * For example:
  28.  *   LinkSwitch 5 $gameParty.members().some(function(actor) { return actor.isLearnedSkill(12); })
  29.  * When switch 5 is used in a conditional branch, it will be true/ON if any
  30.  * party member has learned skill 12, and will be false/OFF if nobody has.
  31.  *
  32.  * To add a link to the script, modify the section below marked "Add switch
  33.  * commands here" as follows:
  34.  *   switchLinks[switchId] = 'command'
  35.  * For example:
  36.  *   switchLinks[5] = '$gameParty.members().some(function(actor) { return actor.isLearnedSkill(12); })'
  37.  *
  38.  * Note that these switches that are linked may not be reliable when used in
  39.  * conditions on event pages, as event page conditions are only checked when
  40.  * the map needs refreshing.  In the example above, switch 5 could not be used
  41.  * in the conditions section because an actor learning or forgetting a skill
  42.  * does not cause the map to be refreshed.
  43.  */
  44.  
  45. (function() {
  46.  
  47.   var switchLinks = []
  48.   //===========================================================================
  49.   // Add switch commands here to build with the game rather than in save file
  50.   // eg:
  51.   // switchLinks[1] = '$gameParty.members().some(function(actor) { return actor.isLearnedSkill(12); })'
  52.   //===========================================================================
  53.  
  54.   var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  55.   Game_Interpreter.prototype.pluginCommand = function(command, args) {
  56.     _Game_Interpreter_pluginCommand.call(this, command, args);
  57.  
  58.     if (command.toUpperCase() === 'LINKSWITCH') {
  59.       var switchId = parseInt(args.shift());
  60.       var cmd = args.join(' ');
  61.       switchLinks[switchId] = cmd;
  62.     }
  63.   };
  64.  
  65.   var _Game_Switches_value = Game_Switches.prototype.value;
  66.   Game_Switches.prototype.value = function(switchId) {
  67.     if (switchLinks[switchId] || null) {
  68.       return eval(switchLinks[switchId]);
  69.     } else {
  70.       return _Game_Switches_value.call(this, switchId);
  71.     }
  72.   }
  73. })();
Advertisement
Add Comment
Please, Sign In to add comment