Set variable value in a plugin ?

Hi everyone !

I'd like to know if it's possible to make a plugin that set variables

For exemple : $gameVariables.setValue(1, $gameActors.actor(1).hp)

The Variable number 1 = Actor number 1 hp

I can make this work with a script call, but I'd like to put this in a plugin

It would be very usefull to put /v[x] in skills descriptions
Sure. How are you planning on triggering the change? Something through gameplay? A plugin call? Should the variable in question be provided statically through plugin settings, or as an argument to something?
Sure. How are you planning on triggering the change? Something through gameplay? A plugin call? Should the variable in question be provided statically through plugin settings, or as an argument to something?

I'm planning to use this as damage values in skills descriptions

For exemple : $gameVariables.setValue(1, $gameActors.actor(1).atk)

→ For a skill which damage formula is a.atk*1

I'll put \v[1] in the description box to show damage value
Ah, so you want to set variables before you display a description box? Sounds like plugin command is probably best way to go. How do you want to configure your variables? On the plugin call or in plugin settings?
Ah, so you want to set variables before you display a description box? Sounds like plugin command is probably best way to go. How do you want to configure your variables? On the plugin call or in plugin settings?

In plugin settings I guess
Here's a very simple framework for you:
Code:
//=============================================================================
// cy_VarSetter.js
//=============================================================================

/*:
 * 
 * @plugindesc Sets variables or something
 * @author cyanic
 * 
 * @param firstVar
 * @text First Variable
 * @desc The first variable to set
 * @type variable
 * 
 */

var Imported = Imported || {};

(function() {

var pluginName = "cy_VarSetter";

Imported[pluginName] = 1;

// Load parameters
var sysParams = PluginManager.parameters(pluginName);
var pluginParams = {
    "firstVar": Number(sysParams["firstVar"] || 0)
};

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;

Game_Interpreter.prototype.pluginCommand = function(command, args) {
    _Game_Interpreter_pluginCommand.call(this, command, args);
    if (command.toLowerCase() === "setvars") {
        setVars();
    }
};

function setVars() {
    if (pluginParams.firstVar) {
        $gameVariables.setValue(pluginParams.firstVar, $gameActors.actor(1).hp);
    }
}

})();

Change your plugin settings to something meaningful. In the pluginCommand() function, you can see that it listens for "setvars" as the command, and the actual work happens in the setVars() function. To use, add the script into your plugin manager, set the variable, then when you need the values, do a plugin command for "setvars", and you can access the variables after that.
Here's a very simple framework for you:
Code:
//=============================================================================
// cy_VarSetter.js
//=============================================================================

/*:
 *
 * @plugindesc Sets variables or something
 * @author cyanic
 *
 * @param firstVar
 * @text First Variable
 * @desc The first variable to set
 * @type variable
 *
 */

var Imported = Imported || {};

(function() {

var pluginName = "cy_VarSetter";

Imported[pluginName] = 1;

// Load parameters
var sysParams = PluginManager.parameters(pluginName);
var pluginParams = {
    "firstVar": Number(sysParams["firstVar"] || 0)
};

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;

Game_Interpreter.prototype.pluginCommand = function(command, args) {
    _Game_Interpreter_pluginCommand.call(this, command, args);
    if (command.toLowerCase() === "setvars") {
        setVars();
    }
};

function setVars() {
    if (pluginParams.firstVar) {
        $gameVariables.setValue(pluginParams.firstVar, $gameActors.actor(1).hp);
    }
}

})();

Change your plugin settings to something meaningful. In the pluginCommand() function, you can see that it listens for "setvars" as the command, and the actual work happens in the setVars() function. To use, add the script into your plugin manager, set the variable, then when you need the values, do a plugin command for "setvars", and you can access the variables after that.

Well, I tested it but I don't really understand the command use.

It would be easier to simply set variables values that are always true, without a command.

A list like this for exemple :

$gameVariables.setValue(1, $gameActors.actor(1).hp);
$gameVariables.setValue(2, $gameActors.actor(1).atk);
$gameVariables.setValue(3, $gameActors.actor(2).mp);
$gameVariables.setValue(4, $gameActors.actor(3).def);

That would be perfectly fine.

EDIT : There is another problem : there is no auto-update of the value.

I'm using variables for damages values in skills descriptions.

For exemple, for skill n°1, it's "Deals \v[1] damages to the target", where \v[1] equals the damage formula, let's say actor 1 atk x 2.

If actor 1 is increasing his atk with a better weapon, then the variable value will not update. I need something that will update at any time.

I had a RGSS3 script for that (VX ACE), if it can help :

Code:
  class Window_SkillList < Window_Selectable
 
   def update_help
     if item
       if item.damage.none? != 0 then
         $game_variables[1] = (item.damage.eval(@actor, @actor, $game_variables).to_i).abs
         $game_variables[2] = $game_actors[4].mat * 4
         $game_variables[3] = $game_actors[2].atk * 5
       end
     end
     @help_window.set_item(item)
   end
  end
I think this is more or less a direct translation of your code snippet. Haven't tested it.

Code:
(function() {
  
    var _Window_SkillList_updateHelp = Window_SkillList.prototype.updateHelp;
  
    Window_SkillList.prototype.updateHelp = function() {
        var item = this.item();
        if (item && item.damage.type > 0) {
            var gameAction = new Game_Action(this._actor);
            gameAction.setItemObject(item);
            $gameVariables.setValue(1, Math.abs(gameAction.evalDamageFormula(this._actor)));
            $gameVariables.setValue(2, $gameActors.actor(4).mat * 4);
            $gameVariables.setValue(3, $gameActors.actor(2).atk * 5);
        }
        _Window_SkillList_updateHelp.apply(this, arguments);
    };
  
})();
I think this is more or less a direct translation of your code snippet. Haven't tested it.

Code:
(function() {
 
    var _Window_SkillList_updateHelp = Window_SkillList.prototype.updateHelp;
 
    Window_SkillList.prototype.updateHelp = function() {
        var item = this.item();
        if (item && item.damage.type > 0) {
            var gameAction = new Game_Action(this._actor);
            gameAction.setItemObject(item);
            $gameVariables.setValue(1, Math.abs(gameAction.evalDamageFormula(this._actor)));
            $gameVariables.setValue(2, $gameActors.actor(4).mat * 4);
            $gameVariables.setValue(3, $gameActors.actor(2).atk * 5);
        }
        _Window_SkillList_updateHelp.apply(this, arguments);
    };
 
})();

Well, that works perfectly fine !

Thank you very much for your help 🙂

Original thread: https://forums.rpgmakerweb.com/threads/set-variable-value-in-a-plugin.87645/