State auto life (Resolved)

Hello,
I need help improving a condition I found on the Yanfly site.
This is the "Auto life" state which allows you to instantly resuscitate a character mit KO (or rather, it prevents him from dying).
Here is the code:

Code:
<Category: Bypass Death Removal>

<Custom Deselect Effect>
// Check if the target's HP is 0 or is currently in the death state.
if (target.hp <= 0 || target.isDead()) {
  // Play the auto-life animation on target.
  target.startAnimation(260);
  // Set the amount of HP recovered to 20%.
  var rate = 0.20;
  // Calculate the HP healed.
  var heal = Math.floor(target.mhp * rate);
  // Remove the Auto-Life state.
  target.removeState(stateId);
  // Heal the target.
  target.gainHp(heal);
  // Make the damage popup show for the heal.
  target.startDamagePopup();
  // Clear the target's results.
  target.clearResult();
}
</Custom Deselect Effect>


This effect works very well when the character's HP is reduced to 0. My problem is that it does not work on instant death effects. For example, for an attack that directly inflicts the "Dead" state. The character is KO and the "Auto life" state simply disappears.
Can anyone modify the code to correct this problem please?
no need for any code
if that is a state, then simply add resistance to death state as an effect of the state

there should be an example immortal state in the default database to show you how it is done
Thank you for your reply. I just tried your method but unfortunately it doesn't work.
The attack that inflicts "Death" is just ignored. Nothing happens.
Is adding death part of the formula by chance?

If you are using something like "b.addState(1)" in your formula it will forcibly add the state even if the target is immune to the state being inflicted.

If you are adding it via the normal Effects box in the skill tab then it should be effected by death immunity properly.
Is adding death part of the formula by chance?

If you are using something like "b.addState(1)" in your formula it will forcibly add the state even if the target is immune to the state being inflicted.

If you are adding it via the normal Effects box in the skill tab then it should be effected by death immunity properly.
In my experience, there's something in Yanfly's plugins that makes adding state(1) in the formula not work. It can apply any other state, but trying to apply death with it does nothing. It'll work on fresh projects, so I've concluded it's got something to do with Yanfly's plugins.

My workaround--and this might be useful to Jay19--is to use an <After Eval> note that sets the target's HP to 0. So instead of all your instant-death effects applying the death state, they instead simply reduce their hp to 0. It accomplishes the same thing, and while it might not have the same effect in terms of game feel, it's possible it will trigger auto life effect like you want.

Code:
<After Eval>

if (b.isStateAffected(101)) b.setHp(0);

else

if (b.isStateAffected(103)) b.setHp(0);

</After Eval>
[/CODE]
In my experience, there's something in Yanfly's plugins that makes adding state(1) in the formula not work...My workaround--and this might be useful to Jay19--is to use an <After Eval> note that sets the target's HP to 0.
I feel like this is kind of backwards from the OP's question...it states that the instant death effects do work. Maybe I'm misreading something in your post.

My problem is that it does not work on instant death effects. For example, for an attack that directly inflicts the "Dead" state. The character is KO and the "Auto life" state simply disappears.
That's a part of the default engine behavior - when you specifically add KO, it strips all other states off of the target.

To get around this, you need to make a small modification. Modify the following to replace autoLifeId with the ID of your autolife state. Then save it as a *.js file in your plugins folder and add it via your plugin manager.

Code:
Game_BattlerBase.prototype.die = function() {
    this._hp = 0;
    this.states().forEach(state => if (state.id!=autoLifeId) this.removeState(state.id));
    this.clearBuffs();
};
Sorry for the late reply, and thank you for yours.

Ahuramazda : No, death is not part of my formula. I add it as a normal effect.

Zozma : Your solution with <After Eval> doesn't work unfortunately. The character dies without triggering the Autolife state.
I even tried to modify your proposal by replacing b.gainHp(-9999);
That doesn't work either. And that made me realize that Autolife doesn't trigger with poison effects either. So it's more serious than I thought...

ATT_Turan : I did what you advised, just replacing "autoLifeId" with "70" because that's what the ID is we're interested in. The plugin is activated at the very bottom of my plugin list, but it doesn't seem to have any effect. Did I forget something?


Edit :
Is it possible that the problem comes from "<Custom Deselect Effect>"?
Doesn't that mean that the effect can only trigger after damage from an ability?
Maybe I'm wrong
While @ATT_Turan 's remark is correct, the plugin they propose is unnecessary, because the same effect will already be achieved by the <Category: Bypass Death Removal> notetag of yanfly's state categories plugin.

@Jay19 I'm currently not at home, so I can't verify, but your suspicion seems likely to me. Have you tried if your auto-life state will trigger if you add some token damage formula like 1 or even 0 to your instant-death skill?
Edit :
Is it possible that the problem comes from "<Custom Deselect Effect>"?
Doesn't that mean that the effect can only trigger after damage from an ability?
Maybe I'm wrong
i dont think so... heres the plugins description:
Code:
[...]
  <Custom Deselect Effect>
   code
   code
  </Custom Deselect Effect>
  This effect will run when the battler is selected as a target. This will
  occur after hit/miss/evade confirmation and damage execution. This effect
  will run after everything else has taken course.

  <Custom Conclude Effect>
   code
   code
  </Custom Conclude Effect>
  This is the final effect to be run after the battler selects a target and
  will occur after hit/miss/evade confirmation and damage execution. [...]

you can try the conclude effect instead...
While @ATT_Turan 's remark is correct, the plugin they propose is unnecessary, because the same effect will already be achieved by the <Category: Bypass Death Removal> notetag of yanfly's state categories plugin.
Good call. I haven't used that plugin, so I don't know all the tags present in it. One might say using my snippet makes installing an entirely other complete plugin unnecessary, but it's half a dozen of one and six of the other 🤣

Is it possible that the problem comes from "<Custom Deselect Effect>"?
Doesn't that mean that the effect can only trigger after damage from an ability?
Maybe I'm wrong
I don't see how. Looking at the code, the Deselect Effect is called after the Game_Action.apply function, which is what all skills/items use regardless of whether or not they inflict damage.

Do you have all your Yanfly plugins in the correct order, according to the Yanfly Engine Web site list? Not adhering that can cause random bugs.
eomereolsson: I haven't tried but I think if I have to come to that, I might as well put b.hp directly for the damage formula. It will be less aesthetic but if it's the only solution...
It will also force me to create an event at the end of the turn to complete the state (in case of death by poison).

oooNUKEooo: Thank you but unfortunately "Conclude" doesn't seem to work.

ATT_Turan: All my plugins are in the correct order. Maybe Yanfly hadn't foreseen any other possibility of death than damage...

Thank you for your answers
Maybe Yanfly hadn't foreseen any other possibility of death than damage...
Oh right, I think an important point that we failed to mention is that the code yanfly provided should work with a skill that inflicts the death state but doesn't deal damage already as-is. No need for modification. That's why we keep poking at stuff like your plugin order, to find out if anything else is the cause that it is not working in your case.

On a technical note:
if (target.hp <= 0 || target.isDead()) already means "do the following if the target has less than or exactly 0 hp OR if the target is afflicted by the death state"

However I think death by poison or similar DoT will actually not trigger the auto-life state as provided by yanfly. So that is a valid point we will also need to look into 😅
it could mean more work, but every killing method could be tested and, on those verified not working, the autorevive be hardcoded on them.

something like
Code:
var autolifeAffected = 0;
if(target.isStateAffected(X)){autolifeAffected = 1;} //X being the autolife state id
 
//damage thing here

if(autolife == 1 && target.hp <= 0){
   //perform animations, revive stuff
}
However I think death by poison or similar DoT will actually not trigger the auto-life state as provided by yanfly. So that is a valid point we will also need to look into 😅
That is a valid point. Not the same as this issue, I want Jay19 to understand, but valid.
ATT_Turan: All my plugins are in the correct order. Maybe Yanfly hadn't foreseen any other possibility of death than damage...
As eomere already pointed out, that's exactly what the isDead() portion is doing.

If you turn off all plugins in your project except the Buffs & States, does it work?

Also, just to make sure, every time you change something are you saving your project, hitting play test, and starting a new game? Loading a save won't work, and using battle test can produce inconsistent results as it doesn't load all plugins/settings.
You were right, something was wrong with my plugins. I feel stupid.
The "State Categories" plugin was disabled because I don't use it, but it is necessary for the "bypass death" function.
As a result, when a death state is applied directly, autolife is triggered correctly.
All that remains is to fix the problem if death occurs at the end of the turn due to a state. In my game, there are DoTs (poison, bleed, burn... but it's the same thing) but there is also a status that instantly kills after X turns if not healed (like in final fantasy).
I don't know if the correction will be the same for both situations.

oooNUKEooo: I think you offer a solution but I'm not sure I understand it.

ATT_Turan: no worries, I start the game again in "new game" for each test.

I show you the states I use if it can be useful for you.

Poison :
Code:
<DoT Formula: (b.mhp * 8) / 100>
<DoT Element: 6>
<DoT Variance: 0%>

Sentence :
Code:
<Custom Leave Effect>
// Get the death state for the user.
var deathState = user.deathStateId();
// Check if the user is not immune to death nor resistant to it.
if (user.stateRate(deathState) > 0.01 && !user.isStateResist(deathState)) {
  // Play animation on the user.
  user.startAnimation(148);
  // Set the user's HP to 0.
  user.setHp(0);
  // Check if the user is dead.
  if (user.isDead()) {
    // Make the user collapse.
    user.performCollapse();
  }
}
</Custom Leave Effect>

I think in the case of Sentence, one can use an "if" condition, but I don't know how to write it.
For the poison, I have no idea how to do it...
[...]
Sentence :
Code:
<Custom Leave Effect>
// Get the death state for the user.
var deathState = user.deathStateId();
// Check if the user is not immune to death nor resistant to it.
if (user.stateRate(deathState) > 0.01 && !user.isStateResist(deathState)) {
  // Play animation on the user.
  user.startAnimation(148);
  // Set the user's HP to 0.
  user.setHp(0);
  // Check if the user is dead.
  if (user.isDead()) {
    // Make the user collapse.
    user.performCollapse();
  }
}
</Custom Leave Effect>

[...]

Code:
<Custom Leave Effect>
   // Get the death state for the user.
   var deathState = user.deathStateId();
   // Check if the user is not immune to death nor resistant to it.
   if (user.stateRate(deathState) > 0.01 && !user.isStateResist(deathState)) {
   // Play animation on the user.
   user.startAnimation(148);
 
   //check if autorevive is present
   var autoreviveIsPresent = 0;
   if(user.isStateAfeccted(X)){autoreviveIsPresent = 1;}
 
   // i recomend to this point, having a state that when applied will
   //act as the autorevive effect, so you can play an animation when it lands.

   // Set the user's HP to 0.
   user.setHp(0);
   // Check if the user is dead.
   if (user.isDead()) {
      // Make the user collapse.
      user.performCollapse();
 
      //if autorevive was present:
      if(autoreviveIsPresent == 1){
         //apply the revive effect with animation and cure  as desired.
         //you can even, instead of killing the battler, make it so that
         //autorevive "has negated" death
         user.addState(Y);
      }
   }
</Custom Leave Effect>
Thank you for your help oooNUKEooo.
It didn't work (don't know why) but assuming you apply an effect that triggers when applied, I was able to fix the problem. At the end of each turn, a common event checks if each character is affected by AutoLife and has 0 HP. If so, I apply the effect that triggers AutoLife (with <Custom Apply Effect>) and immediately remove it. The illusion is not perfect but almost. We see the character's HP at 0 for a fraction of a second, that seems acceptable to me.

Thanks to everyone who helped me, or tried to help me =)

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/state-auto-life-resolved.149735/