Take less damage when substituting

Discussion in 'Javascript/Plugin Support' started by mharr111, Jul 19, 2019.

  1. mharr111

    mharr111 Villager Member

    Messages:
    14
    Likes Received:
    1
    Hello,

    I am trying to find a way for a character to take less damage when substituting for another character. If there is a non-Plugin method I apologize for posting here. Otherwise, I am using Yanfly's States Core plugin.

    Basically, I have an ability that adds StateID 10 to all allies and StateID 9 to the User. StateID 9 adds the Substitute flag while StateID 10 is where I place my code snippets. I want the engine to double the defense of the actor with StateID 9 ONLY when that actor is not the original target, i.e. the actor is substituting. My code is below and is setup to assume only actor 1 can apply state 9 for testing purposes.

    Is this doable? Is my code anywhere close if it is?

    <Custom Initiate Effect>
    var defend1 = $gameActors.actor(1).param[3];
    var defend2 = $gameActors.actor(1).param[3];
    var defend3 = defend2 * 2;
    </Custom Initiate Effect>

    <Custom Select Effect>
    if (target.isStateAfftected(9)){
    }
    else {
    $gameActors.actor(1).addParam(3, defend3);
    }
    <Custom Select Effect>

    <Custom Establish Effect>
    $gameActors.actor(1).addParam(3, defend1);
    </Custom Establish Effect>
     
    #1
  2. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,047
    Likes Received:
    655
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Plugin link: http://www.yanfly.moe/wiki/Buffs_&_States_Core_(YEP)

    Your code is somewhat confusing: defend1 and defend2 are equal? Also all three of those variables are limited to the Initiate effect tag, they're not defined outside it. Note that addParam is permanent, meaning that if the variables did exist outside the Initiate tag, actor 1's DEF would triple (+200%) every time they're selected, and double (+100%) every time they complete an action...probably not what you intended. :kaoswt:

    I haven't tested, but you may want to try something like this instead:
    Code:
    <Custom Select Effect>
    var canSub = BattleManager.checkSubstitute(target);
    if (canSub && !target.isStateAffected(9)) {
      var sub = target.friendsUnit().substituteBattler();
      if (sub && target.isStateAffected(9)) {
        sub._subDef = sub.def;
        sub.addParam(3, sub._subDef);
      }
    }
    </Custom Select Effect>
    
    <Custom Deselect Effect>
    target.friendsUnit().forEach(function(member) {
      if (member._subDef) {
        member.addParam(3, -member._subDef);
        member._subDef = undefined;
      }
    });
    </Custom Deselect Effect>
    In theory, for any action targetting a battler affected by this state, this code checks whether a substitution will take place: if so it gives the potential substitute battler a permanent DEF boost equal to their current DEF (i.e. it doubles their DEF). Then, after the action has completed, it'll check through the target's allies: anyone with a _subDef value (assigned when they're given the bonus earlier) will have their DEF permanently decreased by that value before erasing the value. Again, that's in theory, hopefully I haven't messed up somewhere. :kaophew:
     
    Last edited: Jul 19, 2019
    #2
  3. mharr111

    mharr111 Villager Member

    Messages:
    14
    Likes Received:
    1
    Thank you!

    That is definitely in the right direction. There are however there are a few things that go wrong with it.

    1. If I'm not mistaken, the target, as far as the plugin is concerned, will always be the original target, not the substitute actor. To remedy this I changed the code to

    Code:
    <Custom Select Effect>
    var canSub = BattleManager.checkSubstitute(target);
    if (canSub) {
      var sub = target.friendsUnit().substituteBattler();
      if (sub) {
        sub._subDef = sub.def;
        sub.addParam(3, sub._subDef);
      }
    }
    </Custom Select Effect>
    
    <Custom Deselect Effect>
    target.friendsUnit().forEach(function(member) {
      if (member._subDef) {
        member.addParam(3, -member._subDef);
        member._subDef = undefined;
      }
    });
    </Custom Deselect Effect>
    This did make it so the substituting actor gets the buff, but that leads to:


    2. The defense goes from 19 to 304 which, while functional, would make all damage 0.

    3. The defense does not revert after the action

    I tried changing the <Custom X Effect> and added these snippets in other noteboxes, but I can't seem to get the defense to revert. If I can't make the defense buff lower I can work with making the character unstoppable for less turns, but I would like to at least find a way to remove it.
     
    Last edited: Jul 20, 2019
    #3
  4. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,047
    Likes Received:
    655
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    On further testing, it seems substitution occurs before the <Select> action phase, so I don't think you can check for substitution with Buffs & States Core alone. :kaosigh: Here are the notetags I used to test, in case you're interested:
    • "Allies" state notetags:
      Code:
      <Custom Select Effect>
      $gameMessage.add(String(BattleManager.checkSubstitute(target)) + ': ' + target.name() + ' will be protected?');
      $gameMessage.add(String(target.isDying()) + ': target is critically injured?');
      $gameMessage.add(String(BattleManager._action.isCertainHit()) + ': action is certain hit?');
      </Custom Select Effect>
      
      <Custom Deselect Effect>
      $gameMessage.add(target.name() + ' was the target.');
      </Custom Deselect Effect>
    • Defender state notetags:
      Code:
      <Custom Select Effect>
      $gameMessage.add(target.name() + ' will be the target.');
      </Custom Select Effect>
      
      <Custom Deselect Effect>
      $gameMessage.add(target.name() + '\'s work here is done.');
      </Custom Deselect Effect>
    So, I wrote a little plugin to apply a state to a battler when they substitute: save as .js file (Save As > File Type: All Files, Filename: whatever.js), import as a plugin~
    This assumes your "substituting" state ID is 28, edit the SUB_STATE_ID = 28 line as needed. :)
    Code:
    (function(alias) {
    
    'use strict';
    
    	const SUB_STATE_ID = 28;
    
    	Window_BattleLog.prototype.displaySubstitute = function(substitute, target) {
    		alias.call(this, substitute, target);
    		substitute.addState(SUB_STATE_ID);
    	};
    
    })(Window_BattleLog.prototype.displaySubstitute);
    Then I put these Buffs & States notetags in my new "substituting" state's notebox:
    Code:
    <Custom Apply Effect>
    if (user.isStateAffected(27)) {
      user._subDef = user.def;
      user.addParam(3, user._subDef);
    }
    </Custom Apply Effect>
    
    <Custom Deselect Effect>
    if (target._subDef) {
      $gameMessage.add(target.name() + ' substituting DEF is ' + target.def);
      target.addParam(3, -target._subDef);
      $gameMessage.add(target.name() + ' DEF was unbuffed to ' + target.def);
    }
    target.removeState(stateId);
    </Custom Deselect Effect>
    (Change the 27 to a 9 for your purposes.) Basically, my little plugin above adds the state when they substitute, and the state removes itself via its <Custom Deselect Effect> tag. The $gameMessage.add lines are just for debug purposes, feel free to remove them once you're OK with the effect! :kaothx:

    Hope that all makes sense! It does seem to be working for me after all that, but let me know if you're still having problems and/or I've overlooked something~ :kaophew:
     
    Last edited: Jul 20, 2019
    #4

Share This Page