MV How to make Substitute work 100% of the time

Hello, everyone! I did some searching through older forums but could only find solutions for MZ, which didn't help me as I use MV.

One of my main characters has a "Defender" skill, which applies the substitute flag on him for the current turn. When he uses it, what I WANT is for him to be able to take every attack that an enemy uses (except multi-target attacks) that targets his allies. Basically, if he uses "Defender," any attack that an enemy uses on one of his friends should ALWAYS hit him instead. You know, take the blow for them, but everytime without fail.

However, in testing, I'm noticing that he'll only substitue attacks if their health is lower than his, and even then it doesn't always work. Somone else was able to make a very simple 2 lines of code plugin-in for MZ that edits the "substitute check" so that it will always work without fail, but as I don't know anything about JavaScript, I was hoping someone could help point me in the right direction.

(Also I'm sorry if I posted this in the wrong area, it's been a while since I've made a thread on here)

Thankies!
but could only find solutions for MZ, which didn't help me as I use MV.
Somone else was able to make a very simple 2 lines of code plugin-in for MZ that edits the "substitute check" so that it will always work without fail
Did you try that simple plugin you found anyways? I havent checked but im pretty sure the code behind substitution is the same, so any solution for MV should also work in MZ and vice versa.
Did you try that simple plugin you found anyways? I havent checked but im pretty sure the code behind substitution is the same, so any solution for MV should also work in MZ and vice versa.
I did try using that same plugin/code edit but it didn't work. I'm pretty sure that some of the code is different between the two engines.

And the last time I tried using an MZ plug-in for MV, someone here said that I was being "careless," so I try to avoid using anything that may be incompatible.
Just took a look and it is the same. Two things: can you link the plugin code youre using and can you confirm that the skill youre defending isnt a certain hit?


And the last time I tried using an MZ plug-in for MV, someone here said that I was being "careless," so I try to avoid using anything that may be incompatible
The thing is, most of the code between the engines is the same, but there are a few areas where they differ enough to require different plugins for the same goal. running with the expectation of it not working is best if youre unfamiiar with how both engines handle the original code, but doesnt hurt to try stuff just to see if it works
This part is the same for MZ and MV.
Most modifications are focused on these two sections.


Note: It only determines whether you need protection.
If you haven't added protective attributes for your other team members, this will not take effect.



Code:
const alias_SunMZ_Core_Game_BattlerBase_isDying = Game_BattlerBase.prototype.isDying;
    Game_BattlerBase.prototype.isDying = function() {
        let isDying = alias_SunMZ_Core_Game_BattlerBase_isDying.call(this);
        if (SunMZ_Core_Utils.isNotBlank(SunMZ.Core.IsDyingFormula)) {
            const a = this;
            const v = $gameVariables._data;
            try {
                isDying = eval(SunMZ.Core.IsDyingFormula);
            } catch (e) {
                isDying = false;
            }
        }
        return isDying;
    };
    const alias_SunMZ_Core_BattleManager_checkSubstitute = BattleManager.checkSubstitute;
    BattleManager.checkSubstitute = function(target) {
        let value = alias_SunMZ_Core_BattleManager_checkSubstitute.apply(this, arguments);
        if (SunMZ_Core_Utils.isNotBlank(SunMZ.Core.SubstituteFormula)) {
            let value = false;
            const a = this._action.subject();
            const b = target;
            const v = $gameVariables._data;
            const x = this._action.isCertainHit();
            try {
                value = eval(SunMZ.Core.SubstituteFormula);
            } catch (e) {
                value = false;
            }
        }
        return value;
    };
This part is the same for MZ and MV.
Most modifications are focused on these two sections.


Note: It only determines whether you need protection.
If you haven't added protective attributes for your other team members, this will not take effect.



Code:
const alias_SunMZ_Core_Game_BattlerBase_isDying = Game_BattlerBase.prototype.isDying;
    Game_BattlerBase.prototype.isDying = function() {
        let isDying = alias_SunMZ_Core_Game_BattlerBase_isDying.call(this);
        if (SunMZ_Core_Utils.isNotBlank(SunMZ.Core.IsDyingFormula)) {
            const a = this;
            const v = $gameVariables._data;
            try {
                isDying = eval(SunMZ.Core.IsDyingFormula);
            } catch (e) {
                isDying = false;
            }
        }
        return isDying;
    };
    const alias_SunMZ_Core_BattleManager_checkSubstitute = BattleManager.checkSubstitute;
    BattleManager.checkSubstitute = function(target) {
        let value = alias_SunMZ_Core_BattleManager_checkSubstitute.apply(this, arguments);
        if (SunMZ_Core_Utils.isNotBlank(SunMZ.Core.SubstituteFormula)) {
            let value = false;
            const a = this._action.subject();
            const b = target;
            const v = $gameVariables._data;
            const x = this._action.isCertainHit();
            try {
                value = eval(SunMZ.Core.SubstituteFormula);
            } catch (e) {
                value = false;
            }
        }
        return value;
    };
Okay, so what would be the protective attributes that I need to add to my other party members?
Okay, so what would be the protective attributes that I need to add to my other party members?

I mean this
actor > effects > substitute
未命名.png
I mean this
actor > effects > substitute
View attachment 341870
Right, I know how to apply the substitute flag. Are you saying that for it to work, I need to have substitue applied to all my defender's allies expect himself? Or am I misunderstanding how this works?
@JustinTheGamer So, I hit up Google to see what I could find. Found the following bit of code in a Reddit post from a little while back, which I'm guessing is what you found too. If so, I guess you need help saving it as a plugin?

JavaScript:
 BattleManager.checkSubstitute = function(target) {
        return target.isAlive() && !this._action.isCertainHit();
    };
@JustinTheGamer So, I hit up Google to see what I could find. Found the following bit of code in a Reddit post from a little while back, which I'm guessing is what you found too. If so, I guess you need help saving it as a plugin?

JavaScript:
 BattleManager.checkSubstitute = function(target) {
        return target.isAlive() && !this._action.isCertainHit();
    };
Yeah, but I already saved that as a js plug-in and tried using it. The problem is, it doesn't show up in the selectable plugins in the engine.
Yeah, but I already saved that as a js plug-in and tried using it. The problem is, it doesn't show up in the selectable plugins in the engine.
You had to have done something wrong then. I just saved it as a plugin myself and stuck it into my project's JS folder. Pops up with no problem. Walk me through what you did so we can see where it went screwy.

EDIT: Did you add it to your folder while you had your project open in the editor? I have no idea if it would show without you closing and reopening the project.
I have no idea if it would show without you closing and reopening the project.
Thats usually not an issue. My wager is that its in the wrong project. The plugin may have been saved into in some duplicate/backup like the onedrive document folder instead of the actual one that the editor is actually running on (or the reverse)
Thats usually not an issue. My wager is that its in the wrong project. The plugin may have been saved into in some duplicate/backup like the onedrive document folder instead of the actual one that the editor is actually running on (or the reverse)
Thanks for the clarification. I had never added a plugin while working in my project, so I legit did not know if that had an impact or not.
Thats usually not an issue. My wager is that its in the wrong project. The plugin may have been saved into in some duplicate/backup like the onedrive document folder instead of the actual one that the editor is actually running on (or the reverse)
You...are 100% correct. I.....quadrupedal checked my game folder. A million times. Why on earth wasn't it saving to the correct place.....(banging my head against a wall rn).

I was even writing about how I was 100% sure it was in the right place and DOUBLE quadrupedal checked mid-sentence just to be sure.

And sure enough. NOW it's in the right place. And I've tested it and it's working correctly now. I appreciate everyone's help, albeit I'm a bit embarrassed. (OneDrive can burn in a fire...)

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-substitute-work-100-of-the-time.178024/