Need Help Creating a Custom Drain HP/MP Script

I've been working on a custom script for a couple of days now (I'm new to scripting, but not to RPG Maker) and I got something to work, but I've run into a couple of problems.

I have a character who is a sort of twisted healer/necromancer, who can drain life from his enemies to restore life to his entire party. So far, I've gotten the following to work:

----Begin Custom Script-----
class Game_Battler < Game_BattlerBase

def execute_damage(user)
on_damage(@result.hp_damage) if @result.hp_damage > 0
self.hp -= @result.hp_damage
self.mp -= @result.mp_damage
if $game_actors[3].skills.include?($data_skills[77])
$game_party.alive_members.each { |actor| actor.hp += @result.hp_drain }
else
user.hp += @result.hp_drain
end
user.mp += @result.mp_drain
end

end
-----End Custom Script-----

I know this particular script as it is now poses a major problem: Because Actor 3 (my necromancer) knows the skill, any time the life drain is used, even by an enemy, it restores health to the party regardless. What I'm struggling with, at this point, is what the ruby script call is for if my necromancer actually uses the skill rather than merely knowing it. I've been searching all over the internet for a couple of days and I can't seem to find an answer to that question.

Another tweak I'd like to make is that if a party member is inflicted with a certain state (ie KO), that member does not benefit from the life drain effect either. The reason for this is because I have a "custom death state" I'm using for a game over system I created using a combination of battle and common events, and I really don't want to have to mess with with the default Game Over script to fix what isn't broken, per se.

Any help on this would be much appreciated. Thank you for your time.
What you scripted says "if your necromancer has this skill, draining will heal the party". Hm, I'll have to look into skill-usage, but the workaround is:

1. user is the caster of the skill, so you need to check if (user.actor? and user.id == 3)
2. For now, turn a switch on in your skill's formula box and turn it off in the script. You should probably check for this switch instead of skills.include?

---

To my knowledge, alive_members only count those alive, aka those without the KO state. However, if you want to be technical, perhaps this would have the effect you're looking for:

$game_party.alive_members.each { |actor|
if !actor.state?(DEAD)
actor.hp += @result.hp_drain
end }
How would I turn a switch on using the formula? That would be a decent workaround, yes. I'm familiar with switches. In fact, my custom game over system makes great use of them.
$game_party.alive_members.each { |actor|
if !actor.state?(DEAD)
actor.hp += @result.hp_drain
end }

That snippet worked! It totally ignored my test actor in the "Defeated" state! Thank you! Now if we can get the conditional part to work it would be awesome!

Okay, here are the edits:

Code:
class Game_Battler < Game_BattlerBase

  def execute_damage(user)
    on_damage(@result.hp_damage) if @result.hp_damage > 0
    self.hp -= @result.hp_damage
    self.mp -= @result.mp_damage
    if (user.actor? and user.id == 3)
        $game_party.alive_members.each { |actor|
          if !actor.state?(26)
          actor.hp += @result.hp_drain
          end }
    else
        user.hp += @result.hp_drain
    end
    user.mp += @result.mp_drain
  end

end

It actually works now that when my Necromancer uses the drain, regardless of the skill, he will drain life to all party members, except the one who has been thrown into a coma. Now all I need is for it to check to see if actor 3 used Skill 77 and not Skill 79, which is the same thing only the benefit goes to himself.

Thank you so much, Harosata. I've been pulling my hair out by the roots over this. <3
In script form, a Game Switch is $game_switches[x]. So the workaround is that for skill 77's formula:

$game_switches[20] = true; damage formula

and for the snippet:

Code:
class Game_Battler < Game_BattlerBase

  def execute_damage(user)
    on_damage(@result.hp_damage) if @result.hp_damage > 0
    self.hp -= @result.hp_damage
    self.mp -= @result.mp_damage
    if user.actor? and user.id == 3 and $game_switches[20]
        $game_party.alive_members.each { |actor|
          if !actor.state?(26)
          actor.hp += @result.hp_drain
          end }
        $game_switches[20] = false
    else
        user.hp += @result.hp_drain
    end
    user.mp += @result.mp_drain
  end

end

This is assuming you're using Switch 20, so change that if you want. So pretty much if the user is the necromancer (proven) and the switch is on (turned on only in that skill), then the party heal only activates during those conditions and the switch will turn off afterwards to keep other drains normal.
Okay, how do I put the "$game_switches[20] = true; damage formula" line into my skill 77 and not have it override the actual damage?
Here is my formula thus far (Keep in mind this is an endgame attack to be used against a pretty nasty final boss)

Type: HP Drain
Element: Dark
Formula (Thus far): 130 + a.atk * 4 + a.mat * 2

Oh wait... do I change the "Damage formula" part to what I have? Is that how it works?


Forgive me, I'm new to all this. <3

Edit: I figured it out... I put it like this: $game_switches[38] = true; 130 + a.atk * 4 + a.mat * 2

And now it works as intended. Only Skill 77 will drain life to all allies and the enemy will drain life to himself as intended. Thank you so, so much! I can live with the switch for now, but if you can figure out the skill id check I'd be grateful. <3

I didn't know you could turn switches on with a skill. That will be useful knowledge in the future <3

My script result:

Code:
class Game_Battler < Game_BattlerBase

  def execute_damage(user)
    on_damage(@result.hp_damage) if @result.hp_damage > 0
    self.hp -= @result.hp_damage
    self.mp -= @result.mp_damage
    if user.actor? and user.id == 3 and $game_switches[38]
        $game_party.alive_members.each { |actor|
          if !actor.state?(26)
          actor.hp += @result.hp_drain
        end }
        $game_switches[38] = false
    else
        user.hp += @result.hp_drain
    end
    user.mp += @result.mp_drain
  end

end
Yeah, generally the last number in a formula or method is the one that gets used. In our case, we should have something like this:

$game_switches[20] = true; 130 + a.atk * 4 + a.mat * 2
Yeah, As I said in my edited post above I figured it out. I honestly didn't know I could call scripts in the formula line. I wonder what else I could call using formulas, but that's a different topic for a different day <3.

Update: Here is my Script, which now includes the MP drain portion (cake-walk once I figured out the drain hp mess <3)

Code:
class Game_Battler < Game_BattlerBase

  def execute_damage(user)
    on_damage(@result.hp_damage) if @result.hp_damage > 0
    self.hp -= @result.hp_damage
    self.mp -= @result.mp_damage
    if user.actor? and user.id == 3 and $game_switches[38]
        $game_party.alive_members.each { |actor|
          if !actor.state?(26)
          actor.hp += @result.hp_drain
        end }
        $game_switches[38] = false
    else
        user.hp += @result.hp_drain
    end
    if user.actor? and user.id == 3 and $game_switches[39]
       $game_party.alive_members.each { |actor|
          if !actor.state?(26)
          actor.mp += @result.mp_drain
        end }
        $game_switches[39] = false
      else
        user.mp += @result.mp_drain
    end
  end

end

Looking good, huh? <3

Original thread: https://forums.rpgmakerweb.com/threads/need-help-creating-a-custom-drain-hp-mp-script.84154/