(MV) Buffs and states duration based on caster's stats

Greetings. I want to change state and buff infliction not with fixed number of turns but depending on how much stat the caster has. For example, we have a skill that add a buff to Def. Duration will be 1 turn and plus another 1 per each 50 points of caster's MAt.
Oh, this is an interesting idea.
I played with it a little and made this; from your post, this seems to fit the bill. Note, this only works with States, not Buffs. So make states for your buffs, such as 'Defense Up' state, and apply the state instead of a defense buff.

First, you need Yanfly's Buffs+States Core

Next, in your state you want to have this effect, put this in the notebox:

Spoiler: Code
//test to see if can vary state turn count
based on caster stats

<Custom Apply Effect>
var stat = origin.mat;
var turns = 0;
for (var i = 1; i < 11; ++i) {
if(stat - (5*i) >= 0) {
turns = turns + 1;
}
}
target.setStateTurns(15, 1+turns);
</Custom Apply Effect>
Change the bolded values to work for your game. There's a explaination of the code at work below
Spoiler: Code Explained
var stat : Making a variable and setting it equal to the origin's Magic Attack. Change to atk, def, mat, mdf, agi, luk for whatever stat you want
var turns : Making a variable to control the added turns
For Loop : I put a hard cap of 10 here, so a max of 10 turns can be added to the state duration.
If statement : This is running the eval. In mine, for every 5 of the stat, I'll add a turn to the state duration. I'm multiplying 5 by the loop count (i). If the origin's stat minus 5*i is more or equal to 0, add a turn to the state duration.
So if stat = 15, then it will add 3 turns. Because 5*3 is 15, and 15-15 is obviously 0. Going up to i=4, then the if eval returns false, and the turn isn't added.

After the loop, target.setStateTurns does exactly what it says. It sets the state turns to 1 plus the variable turns (as 1 is the baseline; you want the state to always have at least 1 turn). Make sure to change the 15 to the state ID of that state.

Let me know if that helps!
Oh, this is an interesting idea.
I played with it a little and made this; from your post, this seems to fit the bill. Note, this only works with States, not Buffs. So make states for your buffs, such as 'Defense Up' state, and apply the state instead of a defense buff.

First, you need Yanfly's Buffs+States Core

Next, in your state you want to have this effect, put this in the notebox:

Spoiler: Code
//test to see if can vary state turn count
based on caster stats

<Custom Apply Effect>
var stat = origin.mat;
var turns = 0;
for (var i = 1; i < 11; ++i) {
if(stat - (5*i) >= 0) {
turns = turns + 1;
}
}
target.setStateTurns(15, 1+turns);
</Custom Apply Effect>
Change the bolded values to work for your game. There's a explaination of the code at work below
Spoiler: Code Explained
var stat : Making a variable and setting it equal to the origin's Magic Attack. Change to atk, def, mat, mdf, agi, luk for whatever stat you want
var turns : Making a variable to control the added turns
For Loop : I put a hard cap of 10 here, so a max of 10 turns can be added to the state duration.
If statement : This is running the eval. In mine, for every 5 of the stat, I'll add a turn to the state duration. I'm multiplying 5 by the loop count (i). If the origin's stat minus 5*i is more or equal to 0, add a turn to the state duration.
So if stat = 15, then it will add 3 turns. Because 5*3 is 15, and 15-15 is obviously 0. Going up to i=4, then the if eval returns false, and the turn isn't added.

After the loop, target.setStateTurns does exactly what it says. It sets the state turns to 1 plus the variable turns (as 1 is the baseline; you want the state to always have at least 1 turn). Make sure to change the 15 to the state ID of that state.

Let me know if that helps!
It works but state adds without turn countdown.

Also, i've been tried Yanfly Buffs States Core before and used <custom stat buff turn>. It has no result.
What do you mean without turn countdown? Do you mean it doesn't show the turns remaining on the icon? If so, then check what other plugins you are using; I tested this in both vanilla RPG Maker MV, and in my current project which hosts dozens of plugins. If it's not showing a turn countdown on the icon, the cause there is most likely a plugin setting you have - something like a battle HUD mod.
As for the stat buff effect, that is an eval that is put in Skill or Item notetags to control buff turn duration. It won't do anything if it's in a State notebox.
Well, i've figured it out. To activate <Custom Apply Effect>, i must set remove condition "automatic remove after X - Y turns". Once i did that state turns appear and state now works in order. Thanks a lot.

One more word about <custom stat buff turn> - yes, i type it in Skill's notebox, not state, like it should be. Well, i'll make buffs in states for now.
By the way, is it possible to set a condition in Custom Apply Effect like "if one target then result 1, if all targets then result 2"?
Glad that works now.
As for your other question, I'm on mobile right now so I have no way to check this, but you could try:
If (this.isForOne ()) {
//put whatever effects you want for single target here
} else {
//for anything that isn't single target here
}
Glad that works now.
As for your other question, I'm on mobile right now so I have no way to check this, but you could try:
If (this.isForOne ()) {
//put whatever effects you want for single target here
} else {
//for anything that isn't single target here
}
No, that don't work.
Alright, I'll try to take a look into it later when I'm home.
Well, i've figured it out. To activate <Custom Apply Effect>, i must set remove condition "automatic remove after X - Y turns". Once i did that state turns appear and state now works in order. Thanks a lot.
You should double check that all your plugins are in the right order and such...you shouldn't have to do that. I have passive states (with no remove condition) that have Custom Apply Effects.
One more word about <custom stat buff turn> - yes, i type it in Skill's notebox, not state, like it should be. Well, i'll make buffs in states for now.
What is the actual code that you used for it? I don't see it anywhere to help you with.
By the way, is it possible to set a condition in Custom Apply Effect like "if one target then result 1, if all targets then result 2"?
Cythera might come up with something, but I don't think so - by the time you get into the notetags for the state, this doesn't refer to the skill, but that instance of the state, I believe. I think if you wanted to do something like this, you'd have to put it into the skill itself using action sequences.
@ATT_Turan From what I tried, yeah, you can't check if the skill is multi or single target in an apply effect, as this no longer references the skill. You can do (this.isForOne()) in a custom react/respond/establish/etc effect though, but those all have this referencing the skill itself.

However, I DO have a workaround for this issue, but it is tedious. Create a switch, and have all applicable multi-target skills turn the switch on. Then, you can reference that switch in the custom apply effect. If the switch is on, you know it's a multi-target skill and can run whatever effect you want. If the switch is off, put your single-target effects in the else bracket. Make sure you turn the switch off once the skill is complete. As someone who had to record state IDs via skills, the best way is through action sequences. In the start action, turn switch X on. In the finish action, turn switch X off. As I said. Tedious workaround. But I can't find a way to directly reference the skill used in a custom apply effect.
For my tastes, at that point, rather than having an external switch, I would define two states that are the same except one has the apply effect for multiple targets and the other has the single-target apply effect, then differentiate which one is getting applied inside the action sequence.

But that's just because I go to moderate lengths to keep stuff out of my global switches/variables/common events unless I have to...there's nothing wrong with Cythera's method 🙂
That's certainly a cleaner way to handle it haha. I was thinking, if the skill was dynamic. Such as, you can toggle between single and multi target. Or, under the right conditions, your single-target skill now becomes multi-target. Or a status effect that forces all multi-target to become single-target skills! Which state would you apply?
You could do branching statements for every skill, I suppose, but the switch method would only require turning a single object on or off; not running branching evals.
Or maybe I'm just going too far with the thought :3
Anyways, I don't think you can reference the skill in an apply effect, so you'll need some kind of workaround for that. But at least the dynamic state turn duration works!

Original thread: https://forums.rpgmakerweb.com/threads/mv-buffs-and-states-duration-based-on-casters-stats.135981/