Help with scripted after-damage effects

Hello, so my Game Battler 3 is getting quite large from the after-damage effects, so I decided to add them to a method that I can call. Issue I ran into is the user of a skill is "user" in the code and the user of the normal attack is "attacker" in the code. I already have a global variable that checks the ID of the skill used (called $SkillUseID) that equals 0 when using the normal attack.

I could simply just check the SkillUseID to see if it equals 0 and then just copy/pasta everything on both sides (aka make a "attacker" version of each of the conditional branches below.

The code is as follows:


Ruby:
  #-------------------------------------
  # After DMG effects
  #-------------------------------------
  def after_dmg
    if $SkillUseID == 0
      @jesus = attacker
    else
      @jesus = user
    end
   
      if $SkillUseID == 97 #Earth Fist's SP Damage
        self.sp -= Integer(self.damage / 4)
        @jesus.hp -= @jesus.hp
      end
   
      if $SkillUseID == 114 #Polearm Sweep
        user.add_state(6)
      end
   
      if $SkillUseID == 128 #Dancing Swords
        user.add_state(55)
      end
   
      #Life Drain abilities
      if $SkillUseID == 41
        user.damage -= Integer(self.damage * 0.3)
      end
   
      if $SkillUseID == 48
        user.damage -= Integer(self.damage)
      end
   
      if user.state?(84) #Cannibal Striker
        user.damage -= Integer(self.damage * 0.35)
      end
   
      if $SkillUseID == 38 #Energize Passive (Life Spell Capstone)
        if user.state?(78)
         user.remove_state(78)
        else
         user.add_state(78)
       end
     end
   
      if $SkillUseID == 112 #Cannibal
        if user.state?(84)
          user.remove_state(84)
        else
          user.add_state(84)
        end
      end
   
      if $SkillUseID == 113 #Vital Hitter
        if user.state?(85)
          user.remove_state(85)
        else
          user.add_state(85)
        end
      end
 
  end

I'm using Skill 97 to test this (which is why it's the only one using @jesus) If you look at [@jesus = user], that gives me the error of undefined local variable being "user." I can still use [user] as normal but @jesus cannot equal to it.
(It came to my attention that it reads self just fine since self is the target in both skills and the normal attack but it does not recognize user or attacker).
Is there a way I can have a variable equate itself to user or attacker or do I just have to make a separate conditional branch for each. So will I have to put SkillUseID == 0 and under that, have the attacker versions, and then under Else, have the user versions or is it possible for @jesus to equate as one of them to make it cleaner and shorter?

EDIT: How do I get this new method to recognize the terms, user and attacker?
Where exactly in your code are you calling after_dmg? If you're calling it within attack_effect and skill_effect like I think you are, then I'd suggest you just pass the same parameters those methods take in into after_dmg.
Code:
# define your method like so
def after_dmg(user, skill_id = 0)

# in attack_effect
after_dmg(attacker) # don't need to pass in a second parameter as `skill_id` will default to 0

# in skill_effect
after_dmg(user, skill.id)
This removes the need for your "jesus" instance variable, and probably your $SkillUseID, assuming you're not using that variable somewhere else too.

Also, it's easier to read and configure if you use a case-statement instead of a bunch of if-statements.
Code:
case skill_id
when 97
  self.sp -= (self.damage / 4).to_i
  user.hp -= user.hp # I think you typo'd something wrong here...
when 114
  user.add_state(6)
when 128
  user.add_state(55)
# and so on...
end
Where exactly in your code are you calling after_dmg? If you're calling it within attack_effect and skill_effect like I think you are...
Code:
case skill_id
when 97
  self.sp -= (self.damage / 4).to_i
  user.hp -= user.hp # I think you typo'd something wrong here...
when 114
  user.add_state(6)
when 128
  user.add_state(55)
# and so on...
end

Yes, I am calling it in attack_effect and skill_efect. Thank you for telling me about the case stuff (looks so much neater and easier to read). Question, though. For example, where you see this:
Ruby:
     if user.state?(84) #Cannibal Striker
        user.damage -= Integer(self.damage * 0.35)
    end

This makes it so any damage you do heals you equal to 35% of the damage dealt if you have this state but is supposed to work for both for skills and the normal attack. Do I need to create a duplicate of this for attacker?

Lastly, I have this piece of code which is an edit of the above code (forgot to add the element set condition):
Ruby:
    if user.state?(84) and skill.element_set.include?(18)#Cannibal (life steal effect)
        user.damage -= Integer(self.damage * 0.35)
    end

Do I need to put skill_element_set.include?(18) instead? I was using skill.id (didn't realize you put an underscore), and it was giving me an error that skill was undefined. In the times I would use "skill.", do I instead use "skill_" and why does the underscore work but the dot doesn't?
Oh, didn't see that initially. In that case, you can pass in the whole skill itself rather than only its ID.
Code:
# define your method like so
def after_dmg(user, skill = nil)

# in attack_effect
after_dmg(attacker) # don't need to pass in a second parameter as `skill` will default to nil

# in skill_effect
after_dmg(user, skill)
Which will end up looking like this
Ruby:
  def after_dmg(user, skill = nil)
    case skill.id
    when 97 #Earth Fist's SP Damage
      self.sp -= Integer(self.damage / 4)
      user.hp -= user.hp
    when 114 #Polearm Sweep
      user.add_state(6)
    when 128 #Dancing Swords
      user.add_state(55)
    #Life Drain abilities
    when 41
      user.damage -= Integer(self.damage * 0.3)
    when 48
      user.damage -= Integer(self.damage)
    when 38 #Energize Passive (Life Spell Capstone)
      if user.state?(78)
       user.remove_state(78)
      else
       user.add_state(78)
     end
    when 112 #Cannibal
      if user.state?(84)
        user.remove_state(84)
      else
        user.add_state(84)
      end
    when 113 #Vital Hitter
      if user.state?(85)
        user.remove_state(85)
      else
        user.add_state(85)
      end
    end if skill

    if user.state?(84) && skill.element_set.include?(18) #Cannibal (life steal effect)
      user.damage -= Integer(self.damage * 0.35)
    end
  end
I also advise you to read more guides/tutorials on Ruby variables.
You know, you don't need to call it as self.damage,
Code:
@damage
is good enough and shorter than the other one. XD

Code:
@damage * 30 / 100
is a lot more convenient than using a float. You know, you don't need the decimals showing up on the status window. 😅
I also advise you to read more guides/tutorials on Ruby variables.
I'll look into it. I was pretty close to what you put when I was fiddling, but I was passing the wrong parameters and not putting in the right parameters when I was calling them. It's always the tiny details that mess you up. D:

You know, you don't need to call it as self.damage,
Code:
@damage
is good enough and shorter than the other one. XD

Code:
@damage * 30 / 100
is a lot more convenient than using a float. You know, you don't need the decimals showing up on the status window. 😅
I rather keep it consistent than call it self.damage in one area then to use the following in another:
Code:
@damage
As for the float stuff, that's why I use Integer(#) because I noticed that the game will round when I don't want it to. In the code I gave, the calculations are quite straight-forward but it's also not all the effects (I was asking for help to make it easier to look at as the list got bigger) and you can have multiple at once. It's important that they all don't round until the final result is found because the more it rounds, the more the final answer deviates from the true answer.
1.2 * 2 * 3 = 7.2 but 1 * 2 * 3 = 6 for a quick example.
EDIT: It just dawned on me that some of the life drain effects that are stackable rounds on each life drain calculation. Will probably change that by making user.damage equal an integer version of itself.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-scripted-after-damage-effects.160052/