Advertisement
ezmash

Linked Variables (MV)

Nov 10th, 2015
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Linked Variables
  3. // by Shaz
  4. // Last Updated: 2015.11.11
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows a variable to be linked to a statement to be evaluated
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  * This plugin allows you to link a variable with a command.  When the value
  13.  * of the variable is checked (in a Conditional Branch, for example), the
  14.  * command will be run, and the result will be returned as the variable value.
  15.  *
  16.  * Links can be set up in two different ways:
  17.  * You can use a plugin command to link a variable 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.  *   LinkVAriable variableId command
  27.  * For example:
  28.  *   LinkVariable 8 $gameParty.leader().hp
  29.  * When variable 8 is used in a conditional branch, it will be replaced with
  30.  * the hp of the party leader.
  31.  *
  32.  * To add a link to the script, modify the section below marked "Add variable
  33.  * commands here" as follows:
  34.  *   variableLinks[variableId] = 'command'
  35.  * For example:
  36.  *   variableLinks[8] = '$gameParty.leader().hp'
  37.  *
  38.  * Note that these variables 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, variable 8 could not be used
  41.  * in the conditions section because the map is not refreshed when the party
  42.  * leader's hp changes.
  43.  */
  44.  
  45. (function() {
  46.  
  47.   var variableLinks = []
  48.   //===========================================================================
  49.   // Add variable commands here to build with the game rather than in save file
  50.   // eg:
  51.   // variableLinks[1] = '$gameParty.leader().hp'
  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() === 'LINKVARIABLE') {
  59.       var variableId = parseInt(args.shift());
  60.       var cmd = args.join(' ');
  61.       variableLinks[variableId] = cmd;
  62.     }
  63.   };
  64.  
  65.   var _Game_Variables_value = Game_Variables.prototype.value;
  66.   Game_Variables.prototype.value = function(variableId) {
  67.     if (variableLinks[variableId] || null) {
  68.       return eval(variableLinks[variableId]);
  69.     } else {
  70.       return _Game_Variables_value.call(this, variableId);
  71.     }
  72.   }
  73. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement