Making States be applied on a % based on TP

Folks,

I was looking into a custom state apply where the hit % of a state to be applied in based on the TP.

So if TP is 13, states have 13% chances to hit, while TP 92 means the state has a 92% success rate.

What's the best way to do this? I was thinking of setting a common event but I dunno how to get the target's TP
as a variable to use it, I don't think target.tp is what I'm looking for here.

Any help?
I think this would be pretty awkward without a plugin, unless you want to risk bugs by adding the state in the damage formula. 😅


By default the relative luck of the user/target affects state/debuff apply chance (when not Certain Hit). So if this "TP% apply chance" thing is meant to apply to everything, it may be simplest to look for a plugin that customises that "luck effect rate". E.g. (untested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Sets luck effect rate equal to the subject's TP%.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/175516/
 * @help Free to use and/or modify for any project, no credit required.
 */
Game_Action.prototype.lukEffectRate = function(target) {
  return this.subject().tpRate();  // user's current TP%
};


Alternatively you could find a plugin that lets you run a script call just after the skill hits, e.g.
With ATT_Turan's Eval Tags plugin, you could try something like this (untested):

<Action Post-Apply Eval> if (Math.randomInt(100) < user.tp) { target.addState(123); } value; </Action Post-Apply Eval>
I.e. "if (random integer from 0~99) is less than the user's current TP, add state ID 123 to the target". I've added value at the end, since the plugin seems to take the final value of this tag as the damage value for its follow-up action tags.

(Alternatively you could use if (Math.random() < user.tpRate()). tpRate returns the current/max TP, and Math.random() goes from 0 to 0.999-ish, so the logic is the same unless you've changed the max TP.)

VisuStella Battle Core offers similar tags; the syntax might be slightly different, I haven't checked. There are probably other options available~
Mostly agree with caethyril, but would maybe suggest using the applyItemEffect function over addState here if you are going the note tag route:

This way the targets state rate is considered when calculating the success chance (and maybe other relevant plugin functions). It also prevents the skill from displaying as a "miss" in the case that adding the state is all your skill does:

JavaScript:
<Action After Eval>
if (target.result().isHit()) {
  let effect = {
    code: Game_Action.EFFECT_ADD_STATE,
    dataId: 123, // <- Replace with the State ID
    value1: target.tp / 100  // <- Chance between 0 and 1
  };
  this.applyItemEffect(target, effect);
}
</Action After Eval>


Really Late Edit: If you do want to use a common event, I would still suggest using a note tag to set the variable you plan to use in your common event:
$gameVariables.setValue(41, target.tp)

Alternatively you can use the games "last target" feature, although it would require you to know if the skill was used on an actor or enemy:
$gameActors.actor($gameTemp.lastActionData(4)).tp
$gameTroop.members()[$gameTemp.lastActionData(5)].tp

(The parameter for "last action data" is the index the value shows up in the "Last" list in "Control Variables", starting at 0)

Edit2: post-apply does not distinguish whether the hit was a success or not. Edited the code above to include a success check. I also believe the note tags were mixed up (replace with "JS post-apply" when using VisuStellas BattleCore).
I've added value at the end, since the plugin seems to take the final value of this tag as the damage value for its follow-up action tags.
It does take the final value of value, but that's both passed in and returned separately. So it shouldn't be necessary to add any extra lines to the notetag itself unless you see something I've done wrong.
@Another Fen - good point!

@ATT_Turan - oh, yep, somehow I got the idea that it returned the eval itself, but clearly it doesn't.
Thanks for the suggestions, y'all! I'll test them out later and let you know how it went.
Really Late Edit: If you do want to use a common event, I would still suggest using a note tag to set the variable you plan to use in your common event:
$gameVariables.setValue(41, target.tp)

I gave this one a quick shot and for some weird reason it's only returning the user's tp. I tested on two skills, one scope set to one ally, the other scope set to one enemy and both of them returned the user's TP even though it clearly says target.tp on the note:


<JS Pre-Start Action>

$gameVariables.setValue(1, target.tp);

</JS Pre-Start Action>
<JS Pre-Start Action>
you might want to use <JS Pre-Apply> instead then. <Pre-start> might take place before the targets have been set and might have been written to return the user in both cases
you might want to use <JS Pre-Apply> instead then. <Pre-start> might take place before the targets have been set and might have been written to return the user in both cases

Tried it too, it returns 0 =/
have you tried using another variable? is it possible that variable 1 is reserved by one of your plugins?
Does it return 0 or does it do nothing? If it is the latter, you might want to check the start- and end-tags for typos.

(Also edited the last post because I forgot a detail in the other solution)

Original thread: https://forums.rpgmakerweb.com/threads/making-states-be-applied-on-a-based-on-tp.175516/