MZ How to Add Vars to Base Params with VS

Hey all (again). Been a minute. Got a question about a VS (VisuStella) modification. I don't particularly like how VS chose to do their coding / plugins, but it's a "necessary evil" for the game concept I have, and my limited coding knowledge / skill.

That said, in RPGM-MV, a person could use Yanfly's Base Parameter plugin (or any of the other parameter plugins) to EASILY add variables to the calculation, because the formula was listed for EACH parameter, thus, each parameter could be modified on an individual basis. For example, you could add something like the following to the ATK parameter:
(base + plus) * paramRate * buffRate + flat + v[19]
if you wanted to add variable 19 to your game's ATK calculations.

In the VS plugins of MZ, however, I see no such way to do this. I DID find the JS code section under the Core Engine plugin (Core Engine > Parameter Settings > Basic Parameters > JS: Formula), & it looks like this...
JavaScript:
// Determine the variables used in this calculation.
let paramId = arguments[0];
let base = this.paramBase(paramId);
let plus = this.paramPlus(paramId);
let paramRate = this.paramRate(paramId);
let buffRate = this.paramBuffRate(paramId);
let flatBonus = this.paramFlatBonus(paramId);

// Formula to determine total parameter value.
let value = (base + plus) * paramRate * buffRate + flatBonus;    <---

// Determine the limits
const maxValue = this.paramMax(paramId);
const minValue = this.paramMin(paramId);

// Final value
return Math.round(value.clamp(minValue, maxValue));

I see the place where I could edit the Base Parameter formula, but the way it is currently coded means that I could only add a single var to the line with the "<---" beside it, because it seems to be coded in such a way to calculate ALL of the Base Parameters with that single line, vs on an individual basis.

Herein lies my problem. I have a system that uses variables as modifiers to my Base Parameters, and I wouldn't say that what I'm trying to do falls into the realm of "custom parameters" because all I need is a way to add my variables to each parameter in the same way that the "flatBonus" is added. Thoughts?
You could add to the value of flatBonus before value is calculated. By default the basic param ID goes from 0~7: mhp, mmp, atk, def, mat, mdf, agi, luk. So maybe one of these will work:
  1. Apply the bonus case-by-case based on paramId, e.g.
    JavaScript:
    // ...
    let flatBonus = this.paramFlatBonus(paramId);
    if (this.isActor()) {
      if (paramId === 0) {         // MHP
        // Always add 50.
        flatBonus += 50;
      } else if (paramId === 2) {  // ATK
        // Add value of variable 19.
        flatBonus += $gameVariables.value(19);
      } else {      // anything else
        // code here
      }
    }
    // ...

  2. Determine the variable ID using a formula based on paramId, e.g.
    JavaScript:
    // ...
    let flatBonus = this.paramFlatBonus(paramId);
    if (this.isActor()) {
      // Variables 17 (MHP), 18 (MMP), 19 (ATK), ..., 24 (LUK).
      let varId = paramId + 17;
      // Add the value of that variable to the flat bonus.
      flatBonus += $gameVariables.value(varId);
    }
    // ...
I believe this formula will be applied to everyone, so I guess you may want to check for:
  • Actor or enemy - I've included an isActor check in both my examples for this.
  • Actor ID - unless you want to apply the bonuses equally to all actors (or only have 1 actor).
You ought to be able to get the actor ID using this.actorId(). E.g. for the above examples you could factor in actor ID by:
  1. Duplicating the cases for each actor using more ifs:
    JavaScript:
    if (this.isActor()) {
      if (this.actorId() === 1) {         // actor ID 1
        // if (paramId === 0) etc, as above
      } else if (this.actorId() === 2) {  // actor ID 2
        // etc
      } // ...
    }

  2. Adjusting the varId formula:
    JavaScript:
      // 4 actors; vars 17~24 for actor 1, 25~32 for actor 2, etc.
      let varId = 8 * this.actorId() + paramId + 9;
you should still be able to add variables to the formula for specific stats, its just not neatly split up like Yanfly's. If paramId is the id of the parameter being considered, then youd need to add conditions based on its value to add specific variables.
Code:
let value = (base + plus) * paramRate * buffRate + flatBonus;
if (paramId == 2)  //if the parameter is ATK...
  value += $gameVariables.value(1)  //then add variable 1 to its value

additionally, if every parameter has its own modifier then you can group them together and refer to them by using paramId's value
Code:
let value = (base + plus) * paramRate * buffRate + flatBonus;
value += $gameVariables.value(paramId+4)  //add variable 4 to HP, 5 to MP, 6 to ATK...

Edit: Great Ninja Cae strikes again 🤣

Edit**2: VS Core has notetags for manipulating portions of the parameter values with js. you may not need to add to the formula at all. just slip in a few notetags onto your actors if thats more comfortable

Code:
<JS MaxHP Flat: $gameVariables.value(10)>
<JS MaxMP Flat: $gameVariables.value(11)>
<JS Atk Flat: $gameVariables.value(12)>
Fantastic info @caethyril !!! That is a very informative reply. It is greatly appreciated!
However, as I only have a single actor for the player (& all other actors in the game are non-playable "summons"), @Robro33's suggestion about the JS tags will work better, I think. *Edit* Sorry, I should have mentioned that there is only a single actor in the first post (it didn't seem relevant at the time). HOWEVER.... I do have other games where your (@caethyril) suggestion may prove to be very useful.

Thank you both very much for your suggestions. I don't have time to test any of them tonight (as I have an early morning tomorrow), so I would ask the mods to wait just a bit before closing this topic, but I will be trying them tomorrow. I will post results, of course, for any others that stumble across this thread.

*Edit*
I will add this before I sign off for the night.

Couldn't I modify the default JS:Formula under VS Basic to look like this?
JavaScript:
// Determine the variables used in this calculation.
let paramId = arguments[0];
let base = this.paramBase(paramId);
let plus = this.paramPlus(paramId);
let paramRate = this.paramRate(paramId);
let buffRate = this.paramBuffRate(paramId);
let flatBonus = this.paramFlatBonus(paramId);
let varBonus = this.paramVarBonus(paramId);

// Formula to determine total parameter value.
let value = (base + plus) * paramRate * buffRate + flatBonus + varBonus;

// Determine the limits
const maxValue = this.paramMax(paramId);
const minValue = this.paramMin(paramId);

// Final value
return Math.round(value.clamp(minValue, maxValue));

... Then use @Robro33's suggestion of JS tags to add something like:
<JS MaxHP varBonus: $gameVariables.value(10)>

I only ask this because it would be nice to retain the use of the "Flat" bonus for other aspects of the game. Adding a custom var called "varBonus" (or whatever) would likely be a solution, if my thinking on how to implement it is correct, that is.
(Note: I haven't tested any of the code in this post.)

JavaScript:
let varBonus = this.paramVarBonus(paramId);
For this to work as-is, VisuStella's plugin would have to already have support for your "varBonus" notetag. I think that's very unlikely, even if you followed the same pattern as the documented tags (<JS MaxHP Var: etc>). Instead, you can fetch/process your custom tag directly.

I feel it'd be simplest to just use the tags Robro mentioned. If necessary, though, then you could implement e.g. an Actor notetag like this:

<MHP varBonus: 10>
(I.e. "add the value of variable ID 10 to this actor's Max HP parameter".)

Then modify the param formula to get the value from that tag, e.g.
JavaScript:
// ...
let flatBonus = this.paramFlatBonus(paramId);  // (as usual)

// Names for parameters used in custom varBonus tag.
const tagParamNames = ["MHP", "MMP", "ATK", "DEF", "MAT", "MDF", "AGI", "LUK"];
// Name of notetag for this parameter.
const tagName = tagParamNames[paramId] + " varBonus";
// Get variable ID based on actor notetag value (if any).
const varId = parseInt(this.isActor() ? this.actor().meta[tagName] : 0, 10) || 0;
// Bonus to this parameter from that variable.
let varBonus = $gameVariables.value(varId);

// Formula to determine total parameter value.
let value = (base + plus) * paramRate * buffRate + flatBonus + varBonus;
// ...
This specific implementation is case-sensitive, e.g. you'd have to write the tag as <MHP varBonus: X>, not <mhp VarBonus: X> or something.

If you want to expand that to apply not just to actors, but to any trait objects (class, armor, weapon, state, enemy) then you could try replacing these lines:
JavaScript:
// Get variable ID based on actor notetag value (if any).
const varId = parseInt(this.isActor() ? this.actor().meta[tagName] : 0, 10) || 0;
// Bonus to this parameter from that variable.
let varBonus = $gameVariables.value(varId);
...with something like this:
JavaScript:
// Bonus to this param from varBonus tags on trait objects.
let varBonus = this.traitObjects().reduce((a, c) => {
  const varId = parseInt(c.meta[tagName], 10) || 0;
  return a + $gameVariables.value(varId);
}, 0);
In theory this would allow each trait-bearing object to optionally contribute its own additive bonus.


Otherwise: if you only have 1 actor that receives these bonuses then only the first, simpler part of my (or Robro's) previous post applies. E.g.
JavaScript:
// ...
let flatBonus = this.paramFlatBonus(paramId);
// Actor ID 1 receives custom bonuses based on variables.
if (this.isActor() && this.actorId() === 1) {
  if (paramId === 0) {
    // Add the value of variable 10 to MHP.
    flatBonus += $gameVariables.value(10);
  } else if (paramId === 2) {
    // Add the value of variable 19 to ATK.
    flatBonus += $gameVariables.value(19);
  } // etc
}
// ...
(This adds to flatBonus in the parameter calculation, but is still separate from the "real" flat bonus, as implemented by VisuStella's notetags.)
Again. Thanks for all of your fantastic suggestions and examples. I am rusty in writing code, but I think I get the gist of what each are suggesting. That said, I think that your are correct @caethyril that @Robro33's suggestion will work best.... specifically, the suggestion below.

*If one of the contributors finds this section missing (which had content in it a while ago), it's because I deleted it as it is no longer relevant to the conversation.*

OK... yes... after a few frustrating tests, it seems as though using Robro's "simple suggestion" * is probably the best. The frustrating part is that the variable value didn't seem to be properly adding during a test play when I'd use a test switch to increase the test variable value, so initially I didn't think it was going to work.
*the "simple suggestion" being the <JS Code> tags used in the actor / class note tags.

But after tinkering with it a while, I found that when I switched scenes (say, to the Equip scene), then returned to the Status scene, the param I had associated the variable with (via that JS Code tag) had finally updated and displayed the correct amount in the Status scene.

I guess it's something screwy going on with the VS Status scene calculation refresh. It's a minor bug, and not worth getting into at this time.



Oh... and to clarify what I was trying to do, and the resolution that worked for me (of the suggestions made above), for any looking to do the same................

I wanted to have custom variables assigned to each base parameter for the purpose of setting up custom modifiers gained through random places (e.g. NPC trainers, game events, etc.)... things that the default VS Core was not designed to handle (without getting into custom options). By assigning variables to each of my base parameters under my actor(s) (added to my actor note box as shown below), I am able to modify any of the said actor's base parameters in any aspect of my game (e.g. events, etc.). Naturally, this same idea should work with other (sub) parameters, other than the base params. Just use the tags that VS already has setup for the various parameter abbreviations used in tags.

Here is how the tags should look in your actor's or class note box:
Code:
<JS param Flat: $gameVariables.value(x)>
<JS param Flat: $gameVariables.value(x)>
<JS param Flat: $gameVariables.value(x)>
etc...............
Where (x) equals the variable you want to assign to a "param" (replace this with one of the VS abbreviations). Yes... each param / variable needs to be on its own line (I think VS says this too in the tutorials). Happy gaming!

Thanks for all of your help (Sorry I over-thought this topic way too much, but also thanks for being so generous in your responses). I think this thread can be closed now, unless one of the contributors wants to add something to this.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-add-vars-to-base-params-with-vs.176816/