Jump to content

Recommended Posts

Hello everyone, I am trying to write an enhanced ability about weapons. It goes like this: when a character holds a weapon of this type (with a certain tag), a certain percentage of the last damage received within 10 seconds is added to the next attack. This requires modifying the weapon's damage on the next attack (only once). I am trying to make it through a Buff, where the character gains the buff whenever they are attacked, and it should be removed when the character attacks once or after 10 seconds.

So, what is currently troubling me is how to add one-time additional damage to a type of weapons.

I have looked into the implementation of Wigfrid's battle shield, which is somewhat similar, but only works on a single weapon.

Edited by yanecc

I have indeed been unable to find a way to temporarily increase weapon damage, so I tried adding a variable to the character to store the feedback damage.
I wrote the following code, but unfortunately, it seems there are some errors preventing me from starting the server to enter the game.

 
-- buff_zzj.lua
local function OnAttached(inst, target, followsymbol, followoffset, data)
    inst.entity:SetParent(target.entity)
    inst.Transform:SetPosition(0, 0, 0) --in case of loading
    inst:ListenForEvent("death", function()
        inst.components.debuff:Stop()
    end, target)
    inst._onattackother = function(attacker, attackData)
        if attackData.weapon:HasTag("fhlzzj") then
            if attackData.projectile == nil then
                --in combat, this is when we're just launching a projectile, so don't do FX yet
                if attackData.weapon.components.projectile ~= nil then
                    return
                elseif attackData.weapon.components.complexprojectile ~= nil then
                    return
                elseif attackData.weapon.components.weapon:CanRangedAttack() then
                    return
                end
            end
            if attacker.zzjFeedBack then
                attacker.zzjFeedBack = attacker.zzjFeedBack + data.damage * attacker.level * 0.1
            end
        end
        inst.components.debuff:Stop()
    end
    inst:ListenForEvent("onattackother", inst._onattackother, target)
end

local function OnDetached(inst, target)
    if target ~= nil and target:IsValid() then
        target.zzjFeedBack = 0
    end
    inst.Remove
end

local function OnExtended(inst, target)
    -- inst.components.debuff:Stop()
    inst.components.timer:StopTimer("zzjbuffover")
    inst.components.timer:StartTimer("zzjbuffover", 10)
end

local function fn()
    local inst = CreateEntity()

    if not TheWorld.ismastersim then
        --Not meant for client!
        inst:DoTaskInTime(0, inst.Remove)
        return inst
    end

    inst.entity:AddTransform()

    --[[Non-networked entity]]
    --inst.entity:SetCanSleep(false)
    inst.entity:Hide()
    inst.persists = false

    inst:AddTag("CLASSIFIED")

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff:SetDetachedFn(OnDetached)
    inst.components.debuff:SetExtendedFn(OnExtended)
    inst.components.debuff.keepondespawn = true

    inst:AddComponent("timer")
    inst.components.timer:StartTimer("zzjbuffover", 10)
    inst:ListenForEvent("timerdone", function(inst, data)
        if data.name == "zzjbuffover" then
            inst.components.debuff:Stop()
        end
    end)

    return inst
end

return Prefab("buff_zzj", fn)

-- character
...
    inst.zzjFeedBack = 0
...

-- weapons
...
    inst.components.weapon:SetDamage(inst.owner.zzjFeedBack and inst.owner.zzjFeedBack + fixedDamage or fixedDamage)
    inst.onownerattackedfn = function(_owner, data)
        if _owner:IsValid() then
            _owner:AddDebuff("buff_zzj", "buff_zzj", { damage = data.damage })
        end
    end
    inst:ListenForEvent("attacked", inst.onownerattackedfn, owner)
...

 

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...