Custom revive/heal script not working

I have a skill which has a basic and an upgrade form.

The basic version is the user attacks an enemy and also revives any dead allies with 20% HP
The upgrade version attacks 2 enemies and also revives any dead allies with 40% HP

As the damage formula is too long for the formula box, I use snippets in the script editor which I then call up from the damage formula.

The basic version looks like this:
Code:
class Game_Battler < Game_BattlerBase
  def energy_rebirth()
    $game_party.members.each do |m|
      if (m.state?(1))
        m.remove_state(1)
        m.hp += (m.mhp * 20 / 100) # You can replace 20 with a variable heal rate, e.g. $game_variables[43]
      end
    end
  end
end

The damage formula has
a.energy_rebirth();
followed by the damage formula itself.

The upgrade version has:
Code:
class Game_Battler < Game_BattlerBase
  def energy_rebirthup(b)
    $game_party.members.each do |m|
      m.hp += (m.mhp * 40 / 100) if (m.hp == 40) # change percentage here to be the variable you use to store the actual percentage
    end
  end
end

The damage formula calls it up with
a.energy_rebirthup(b);

I have been trying to test the upgrade version and it simply does not work.
I saw that it lacked the lines:
Code:
  if (m.state?(1))
        m.remove_state(1)
so tried it with those lines and an extra 'end' where needed, but still the dead ally did not revive.

I also tried it calling it up from the damage formula with
a.energy_rebirthup()
but that made no difference.

Can anyone see what the problem might be?
Thank you.
I think the issue is the if(a.hp == 40) line as that looks like it is going to call it only if your HP is exactly 40. What happens if you change that to 0?
Hey Kes, looks like this is playing around with the formula I gave you last year. Where I say "You can replace 20 with a variable heal rate, e.g. $game_variables[43]" in my comment, that means that if you replace the 20 with a variable, you can change the variable during the game (i.e. when the actor gets the upgrade to the skill - say, from 20 to 40) and then just call the same method.

If you have no way to do that, then just copy my "basic version" code, change the 20 to a 40 manually, and change the method's name (such as: def energy_rebirthup() . ) That should get you everything you need. You were correct that lacking the check for if m.state?(1) was an issue (the actor would remain KO'ed), and @bgillisp was also right that the if (m.hp == 40) line is screwing things up (it makes no sense to check the member's current HP). Including the target "(b)" as an argument also makes no sense because you don't need to check anything about the skill's target(s); you only need to look through each actor which the code body already provides.
@bgillisp and @Wavelength
Thank you both, this has sorted the problem out. Not only is that a relief in itself, but I was also becoming deeply fed up testing this. I had to play the encounter badly enough to get at least one actor killed, but not so badly that the actor with the skill went down. I discovered so many ways that the careless/unobservant player could screw up this boss battle I'm left wondering if I'll be the only one who can beat it first go.

Once again, thanks. I am grateful for your help.

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/custom-revive-heal-script-not-working.127250/