Revival Item

Glad to help! 😂 I still think you may have issues later on as we've seen earlier thanks to my boo boos. 😢

But if you're satisfied with the results you can report your first post and tell a moderator to close this thread. You can always reopen this thread later on if you so desire.
Cool. Will do. Take care. Have a great day.

No - as I said above, I already can't figure out the correct functions to make this act the way you want.

The alternative would be to use Yanfly's Buffs & States and Auto Passive States...don't give the party members immortal, but instead adjust the incoming damage if all the circumstances are met. That would get around this current issue.

Just for the sake of correct terminology, you appear to be asking the same thing twice - RPG Maker MV doesn't use scripts, only plugins. An individual reference to a function is sometimes called a script call, like when I gave you a single line to fill into the Script box to check two actors at once.
Thank you very much for all your help as well. It is really appreciated. It's working now. Take care and have a great day.
Just a few thoughts to help clean up/optimize things a bit. 🙂
JavaScript:
var mainCharActorId = $gameVariables.value(mainCharVarId);
if (mainCharActorId === 0) mainCharActorId = 1;
I don't understand the purpose of this. Where is the game variable supposed to be set, and if it's being set, why check for it to be 0, and set it to 1 whether or not that's the main actor being used? Just check which of the possible actors is actually in the party, no need for an external variable.

If you want mainCharActorId to have the ID of the protagonist (which could be 1 or 2), you could just do: var mainCharVarId=$gameParty.members().includes($gameActors.actor(1)) ? 1 : 2;

And, of course, since the only thing you do with mainCharVarId is use it to set another variable later, there's really no reason for you to declare it at all. You could do in the first place:
var mainChar=$gameParty.members().includes($gameActors.actor(1)) ? $gameActors.actor(1) : $gameActors.actor(2);
Code:
var members = $gameParty.members();
...
  var isAnyAlive = members.some(function(actor) {return actor.hp > 0 && !actor.isDead();});
You could save yourself several lines of code by just using the built-in member:
var members=$gameParty.aliveMembers();

And then you don't need that isAnyAlive at all, you just check if (members.length)

You would lower down when you revive the entire party then use $gameParty.members() instead of this variable.
Just a few thoughts to help clean up/optimize things a bit. 🙂
No biggie. 😅
I don't understand the purpose of this. Where is the game variable supposed to be set, and if it's being set, why check for it to be 0, and set it to 1 whether or not that's the main actor being used? Just check which of the possible actors is actually in the party, no need for an external variable.
Oh, I was rushing the code at that time. It's for the case the variable value has not been set. Force of habit. 👋 That line can be removed.
If you want mainCharActorId to have the ID of the protagonist (which could be 1 or 2), you could just do: var mainCharVarId=$gameParty.members().includes($gameActors.actor(1)) ? 1 : 2;
The OP did not mention if the presence of actor 1 in the party precludes the presence of actor 2 or if they can be in the party at the same time. The code previously provided will work in either case.
JavaScript:
var mainCharVarId=$gameParty.members().includes($gameActors.actor(1)) ? 1 : 2;
If both actors 1 and 2 are allowed in the same party this code of yours will always tag actor 1 as the main character, never actor 2. The OP wanted the ability to tag either as the main character. The previously provided code will also make it very easy for him to tag other characters as the main character should he desire that behavior in the future without the need for him to touch the revival state notetags.
And, of course, since the only thing you do with mainCharVarId is use it to set another variable later, there's really no reason for you to declare it at all. You could do in the first place:
var mainChar=$gameParty.members().includes($gameActors.actor(1)) ? $gameActors.actor(1) : $gameActors.actor(2);
Since the OP is not yet proficient with scripting I wanted the code to be as easy for him to modify as possible. I made the code so that he only needs to modify three figures in total in the the first three lines. Your code will force him to modify three figures within a single line alone.
You could save yourself several lines of code by just using the built-in member:
var members=$gameParty.aliveMembers();
JavaScript:
var members=$gameParty.aliveMembers();
The above code you suggested will create an array of all party members that have not yet been affected by the death state. This isn't what we need as I'll explain below.

And then you don't need that isAnyAlive at all, you just check if (members.length)

You would lower down when you revive the entire party then use $gameParty.members() instead of this variable.
JavaScript:
if (members.length)
The code you provided above will effectively always evaluate to true. You were probably referring to the code below:
JavaScript:
if (members.length === 0)
The above conditional you suggested will not work. The OP had specified that all party members need to be dead before the revival state activates. If he were to use your code above as the conditional rather than the one previously given, he'll exprience a Game Over before any revival occurs.
Under the aliveMembers() method, it only checks for the death state; it doesn't check for the current HP. The moment all party members are affected by the death state, it's a Game Over, unless you have plugins modifying this behavior otherwise.
By checking both current hp and the death state, this will circumvent the Game Over that will occur if you were to only check if all party members had been affected by the death state through $gameParty.aliveMembers().length === 0.
The OP did not mention if the presence of actor 1 in the party precludes the presence of actor 2 or if they can be in the party at the same time.
He did previously in the thread, you're choosing one of the two protagonists 😉
JavaScript:
var members=$gameParty.aliveMembers();
The above code you suggested will create an array of all party members that have not yet been affected by the death state. ... The OP had specified that all party members need to be dead before the revival state activates. If he were to use your code above as the conditional rather than the one previously given, he'll exprience a Game Over before any revival occurs.
So you're both right and wrong. You're correct that if we let all of the party be dead before the item goes off, it'll hit a game over. However, the code you provided does exactly the same thing. The else if (hasReviveAll) triggers if no actors in the party were alive, so...we would already have hit the game over.
JavaScript:
if (members.length)
The code you provided above will effectively always evaluate to true. You were probably referring to the code below:
JavaScript:
if (members.length > 0)
That's...just wrong. In every C-adjacent language, 0 is considered to be false (or, in JavaScript, falsey). Saying if (X>0) is exactly the same as saying if (X)

Therefore, if members.length==0 (that is, there are no actors in the aliveMembers), it will evaluate as false. That has other logical problems as you pointed out above, but it's absolutely correct code.
Under the aliveMembers() method, it only checks for the death state; it doesn't check for the current HP. The moment all party members are affected by the death state, it's a Game Over, unless you have plugins modifying this behavior otherwise.
By checking both current hp and the death state, this will circumvent the Game Over that will occur if you were to only check if all party members had been affected by the death state
I don't understand what you think the difference is. If any actor was brought to 0 HP by an effect, they would already have had the death state applied at the same time. Under what circumstances would they have the death state and not 0 HP? Or vice versa?

The mere act of checking something won't prevent the game over. In fact, because this was put in a deselect, the damage was already dealt to this actor and so if the rest of the party has been knocked out, and this actor takes enough damage to die, you'll get a game over before this code executes.

As far as I know, the only way to achieve this for the party effect is to do what I mentioned before and alter the incoming damage before the current actor actually dies from it - that is, a custom confirm effect.
He did previously in the thread, you're choosing one of the two protagonists 😉
The OP did not mention anything of the sort when I asked him about the desired mechanics of his revival items so I based my coding decision on that. The code will work in either case. The code will also still work if he tags other actors as the main character. So you can't criticize my decision to code as I did since I based my coding decision on the information I had been given. And the code is flexible enough to accommodate different scenarios.
So you're both right and wrong. You're correct that if we let all of the party be dead before the item goes off, it'll hit a game over. However, the code you provided does exactly the same thing. The else if (hasReviveAll) triggers if no actors in the party were alive, so...we would already have hit the game over.
Actually, you're wrong. There's a difference.
That's...just wrong. In every C-adjacent language, 0 is considered to be false (or, in JavaScript, falsey). Saying if (X>0) is exactly the same as saying if (X)

Therefore, if members.length==0 (that is, there are no actors in the aliveMembers), it will evaluate as false. That has other logical problems as you pointed out above, but it's absolutely correct code.
Okay. You got me there.
I don't understand what you think the difference is. If any actor was brought to 0 HP by an effect, they would already have had the death state applied at the same time. Under what circumstances would they have the death state and not 0 HP? Or vice versa?

The mere act of checking something won't prevent the game over. In fact, because this was put in a deselect, the damage was already dealt to this actor and so if the rest of the party has been knocked out, and this actor takes enough damage to die, you'll get a game over before this code executes.
I don't understand why you don't think there's a difference. Do you even test your codes before assuming what they actually do? Damage is applied BEFORE the deselect phase. The death state is applied AFTER the deselect phase. The hp is checked for the last actor who is about to die DURING the deselect phase when the death state hasn't yet been applied; this is the time when the revival state is triggered.
If you really believe there's no difference then why don't you test it using $gameParty.aliveMembers().length?
As far as I know, the only way to achieve this for the party effect is to do what I mentioned before and alter the incoming damage before the current actor actually dies from it - that is, a custom confirm effect.
Actually, my code had already achieved everything the OP wanted without using any Custom Confirm Effect notetags.
Brilliant. Thank you very much for all your help! It is greatly appreciated! Everything worked the way it was supposed to.
If the code is working for the OP and not for you then you have to start your own thread since as far as the OP is concerned his problem had already been solved. This is his thread, not yours.

He had already spent the past week with you to no avail. Now you're asking someone who is clearly not yet proficient with javascript to figure out how to make this work using the Custom Confirm Effect notetag while only providing vague instructions to someone, I must emphasize, someone who is clearly not yet proficient with javascript . If he wants to learn javascript he can do that at his own pace and in another thread.

The OP can spend the next week figuring out how to make your new proposed solution work for him or he can close this thread and do something else.

TLDR:
  1. The OP already said everything he wanted had been achieved.
  2. We actually tested the code separately to confirm if the desired results are achieved.
  3. HP damage is applied BEFORE the deselect phase. The Death state is applied AFTER. There's a difference.
EDIT 1:
The reason you kept saying $gameParty.aliveMembers().length works just as well as a conditional is because you haven't actually tested the code. I did. I tested the original conditional plus this suggested one you kept insisting. Mine works; yours doesn't.
I started a new project just for the OP so that I can confidently tell him that the code ACTUALLY works rather than tell him the code SHOULD work. And he also tested the code on his device and he had already stated that it worked.
As far as I know, the only way to achieve this for the party effect is to do what I mentioned before and alter the incoming damage before the current actor actually dies from it - that is, a custom confirm effect.
As far as the OP is concerned we had already solved this problem without doing any of the sort. There are many ways for this effect to be achieved. It doesn't have to be my way. You could have spent your time preparing your code for the OP's benefit. Instead you much preferred spending that time nitpicking every imaginable flaw you could find in the code made by someone who solved the OP's problem before you did (after spending the better part of the week working with the OP to little avail). To be clear, as of posting, you still haven't yet provided your own solution that actually works as the OP requests, merely insinuating that your solution is the only one that works.

EDIT 2:
@ATT_Turan I do not understand why you have the time to nitpick every little bit of my code that already works (from both my own testing and according to the OP himself) while you can't find the same amount of time to provide an actual working code for the OP, merely vague hints and insinuations. Are you afraid if you actually provide your own code someone will nitpick your code instead of the other way around? Helping the OP is the point of this thread, is it not? I already explained in my first response to you why I did what I did. This isn't a contest on who makes better-looking code. I made my code for the convenience of the OP. That's why I made those "redundant" variables that you so eagerly pointed out.

I also based this code on Yanfly's Auto-Life Tips and Tricks which I had suggested to the OP in an earlier post. Why don't you criticize Yanfly instead? (For checking both current HP and the death state.) I'm just borrowing his code and adapting it to the OP's specifications.

I'm under the impression that I've bruised your ego by solving the OP's problem in a short time while you spent a week struggling with Troop Events with little to show for. So you've decided to criticize every flaw in my code, real or imaginary, to make it appear as though even if I've solved the OP's problem (and you haven't) you're still superior to me in some way because you can "clean" and "optimize" my "redundant" code. Just to soothe your ego. If I wounded your pride, I'm sorry. That was never my intention. But I find you rude and patronizing.
The OP did not mention anything of the sort when I asked him about the desired mechanics of his revival items...So you can't criticize my decision
You need to please calm down. You're being very defensive at this point. I did not criticize you, I offered ways to streamline what you had coded. He may not have stated it to you specifically, but it was said earlier in this thread. I didn't insult you for not reading the entire thread that carefully, but I did point out it was a fact.
I don't understand why you don't think there's a difference. Damage is applied BEFORE the deselect phase. The death state is applied AFTER the deselect phase.
Where do you see this? In Buffs & States, the deselect code is called by:
Code:
Yanfly.BSC.Game_Action_apply.call(this, target);
this.onDeselectStateEffects(target);
Deselect happens after the effects of Game_Action.apply are finished. Inside of Game_Action.apply it calls executeDamage(), which calls gainHp(), which calls setHp(), which refreshes the actor, which applies the death state if HP = 0. Even in Yanfly's Auto-Life, it checks for the target to be dead.

So any effects of an attack, including being dead, will resolve before deselect code is executed.

Besides which, you're looking at other party members than the target of this attack. Even if the deselect happened before death, how would you have other party members who have only one of 0 HP or death state?

Edit: I went farther out to look at Game Over. The reason your code will work is because game over isn't checked for immediately after damage and its effects are applied. The skill executes, any common events after it execute, and then Game Over is checked. This means that yes, your code should work, and the aliveMember optimization I offered will work - it has nothing to do with any distinction between HP and death state.
Actually, my code had already achieved everything the OP wanted without using any Custom Confirm Effect notetags.
If you say so. As far as I can read, it would only work for the single target revive, and not if the entire party dies off. If I'm incorrect, and you've tested that scenario in the game and your code works, then that's awesome and more power to you.

As far as the rest of your essay, I'm not going to bother addressing specific points because it's both ridiculous and rude. My ego was not bruised, I admitted when my attempts to help him do it through eventing failed and I told him how he could approach it through states.

At that point, I was not willing to write out the entire code for him because I'm not that invested in doing something for someone else's game - he could either look at how Yanfly's examples worked and learn enough to implement this, or ask someone else to help, which you did.

When you wrote that, I offered some civil, friendly tips on how the code could be streamlined - I didn't say any of it was wrong. You are the one who has told me that I'm wrong, without bothering to look it up first.

So please reconsider how you react to people. This is a friendly community, and we're all volunteers posting here because we want to help others. It only reflects poorly on you to be defensive and attacking like this.

I'm out of this thread. Good luck in your endeavors.

I've reopened the thread at OP's request.

Previous code revised to allow revival upon total party kill from indirect damage (e.g. poison, damage-over-time effects, etc.) as well. Code further modified to accommodate additional specifications from the OP provided through PM during this thread's temporary lockdown period.

Adapted from Yanfly's Phoenix Ring.
Required Plugin: YEP Buffs & States Core
Potential Compatibility Issues: Plugins that modify how the death state is inflicted.

Note: The coder is aware of the fact that the code can be further compacted through the removal of certain variables, the shortening of variable names, the removal of spaces and the use of arrow functions. But for the sake of readability and the convenience of the OP had opted not to do so.
Spoiler: Code
JavaScript:
<Custom Apply Effect>
// SETUP START
// replace the following as necessary
var reviveOneId = 12; // Item ID of the Revive One item
var reviveAllId = 13; // Item ID of the Revive All item
// SETUP END
if (target.isActor()) {
  var hasReviveAll = $gameParty.hasItem($dataItems[reviveAllId]);
  var hasReviveOne = $gameParty.hasItem($dataItems[reviveOneId]);
  if ((hasReviveAll || hasReviveOne) && $gameParty.isAllDead()) {
    var members = $gameParty.members();
    var heal;
    if (hasReviveOne) {
      $gameParty.loseItem($dataItems[reviveOneId], 1);
      var mainCharActorId = members.contains($gameActors.actor(1)) ? 1 : 2;
      var mainChar = $gameActors.actor(mainCharActorId);
      mainChar.startAnimation(49);
      heal = mainChar.mhp;
      mainChar.gainHp(heal);
      mainChar.startDamagePopup();
    } else if (hasReviveAll) {
      $gameParty.loseItem($dataItems[reviveAllId], 1);
      members.forEach(function (member) {
        member.startAnimation(49);
        heal = member.mhp;
        member.gainHp(heal);
        member.startDamagePopup();
      });
    }
  }
}
</Custom Apply Effect>
Previous code revised to allow revival upon total party kill from indirect damage (e.g. poison, damage-over-time effects, etc.) as well. Code further modified to accommodate additional specifications from the OP provided through PM during this thread's temporary lockdown period.

Adapted from Yanfly's Phoenix Ring.
Required Plugin: YEP Buffs & States Core
Potential Compatibility Issues: Plugins that modify how the death state is inflicted.

Note: The coder is aware of the fact that the code can be further compacted through the removal of certain variables, the shortening of variable names, the removal of spaces and the use of arrow functions. But for the sake of readability and the convenience of the OP had opted not to do so.
Spoiler: Code
JavaScript:
<Custom Apply Effect>
// SETUP START
// replace the following as necessary
var reviveOneId = 12; // Item ID of the Revive One item
var reviveAllId = 13; // Item ID of the Revive All item
// SETUP END
if (target.isActor()) {
  var hasReviveAll = $gameParty.hasItem($dataItems[reviveAllId]);
  var hasReviveOne = $gameParty.hasItem($dataItems[reviveOneId]);
  if ((hasReviveAll || hasReviveOne) && $gameParty.isAllDead()) {
    var members = $gameParty.members();
    var heal;
    if (hasReviveOne) {
      $gameParty.loseItem($dataItems[reviveOneId], 1);
      var mainCharActorId = members.contains($gameActors.actor(1)) ? 1 : 2;
      var mainChar = $gameActors.actor(mainCharActorId);
      mainChar.startAnimation(49);
      heal = mainChar.mhp;
      mainChar.gainHp(heal);
      mainChar.startDamagePopup();
    } else if (hasReviveAll) {
      $gameParty.loseItem($dataItems[reviveAllId], 1);
      members.forEach(function (member) {
        member.startAnimation(49);
        heal = member.mhp;
        member.gainHp(heal);
        member.startDamagePopup();
      });
    }
  }
}
</Custom Apply Effect>
Thank you for posting in the revival item thread. The post is very clear and easy to understand and is very much appreciated! You've been a tremendous help. I have been very fortunate to have the forum to turn to and to find people like you. Thank you. ^_^

P.S. The revival items even work when the whole party is knocked out by poison. Love it.

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/revival-item.144454/