Jump to content

Quick question about different damage code

Recommended Posts

Hey just wondering, I have a weapon that does different damage to different enemies:



local function ApplyExtraDamage(weapon, attacker, target)
    if target.prefab == "pigman" then
    AddPrefabPostInit("chainsword", function(inst)
If my character has a damage multiplier, of 1.25, will that be applied to the '70' damage that the pigman takes? For a total of 87.5 damage? Or does the code set the damage as 70?
Edited by dave9664
Link to comment
Share on other sites

When somebody attacks something with a chainsword, if the target is a pigman, then the pigman gets his health reduced by 70. 70 true damage that bypasses armor and is unaffected by multipliers.


There is no damage multiplier or armor reduction involved (with should be, if enemies with armor are the target or you want to make a weapon for pvp).


If you want to apply your weapon's damage and your damage multiplier, to pve enemies (that none of them have armor), you can use:

local multiplier = attacker.components.combat.damagemultiplier or 1local damage = weapon.components.weapon.damagetarget.components.health:DoDelta(damage * multiplier)

If you want to make a weapon for pvp to acknowledge for armor, you will have to either copy some code and apply the damage reduction yourself or to toy with functions.

Link to comment
Share on other sites

@dave9664, As DarkXero suggested at the end, modifying some existing functions is probably a more robust way to do this. For example, I might do something like this (in the modmain):

bonus_targets = {    pigman = true,    rabbit = true,}AddComponentPostInit("combat", function(Combat)    local OldCalcDamage = Combat.CalcDamage    Combat.CalcDamage = function(self, target, weapon, ...)        local old_damage = nil        if weapon and weapon.prefab == "myfancyweapon" and target and bonus_targets[target.prefab] then            old_damage = weapon.components.weapon.damage            weapon.components.weapon.damage = old_damage + 70        end        local ret = OldCalcDamage(self, target, weapon, ...)        if old_damage and weapon then            weapon.components.weapon.damage = old_damage        end        return ret    endend)

What this does is it intercepts the function that calculates damage, checking if the target and weapon are right for the bonus damage, then sets the weapon's damage to have the bonus, runs the normal calculation, then resets the damage to normal.


By taking this approach you don't have to worry about damage multipliers, armor, etc, everything is still handled by the normal damage calculation function.

Edited by rezecib
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...