Jump to content

Problem making weapon losing it damage overtime


Recommended Posts

Hello again!
I need help fixing the problem with weapon not depleting it's damage after 5 seconds if not attacking anything
It should revert back to it's original 35 damage with maximum of 120
Although part with it gaining 5 damage each hit is working fine but the one with depletion is not

 

local assets =
{
    Asset("ANIM", "anim/vegie_sword.zip"),
	Asset("ANIM", "anim/swap_vegie_sword.zip"),
    Asset("ATLAS", "images/inventoryimages/vegie_sword.xml"),
    Asset("IMAGE", "images/inventoryimages/vegie_sword.tex"),
}

local function onequip(inst, owner)
	
	owner.AnimState:OverrideSymbol("swap_object", "swap_vegiepick", "swap_vegiepick")

    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
	
end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")

end

local function onhit(inst, attacker, target)
    if attacker and attacker.components.combat and target then
        inst.components.weapon.damage = inst.components.weapon.damage + 5

        
        if inst.components.weapon.damage > 120 then
            inst.components.weapon.damage = 120
        end
    end
end

local function ondeplete(inst)
    inst.components.timer:StartTimer("deplete_timer", 5) 

    inst:ListenForEvent("timerdone", function(inst, data)
        if data.name == "deplete_timer" then
            if not inst.components.weapon:IsBusy() then
                inst.components.weapon.damage = inst.components.weapon.damage - 5

                
                if inst.components.weapon.damage < 35 then
                    inst.components.weapon.damage = 35
                end

                inst.components.timer:StartTimer("deplete_timer", 5) 
            end
        end
    end)
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("vegie_sword")
    inst.AnimState:SetBuild("vegie_sword")
    inst.AnimState:PlayAnimation("idle")

    inst:AddTag("weapon")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(35)

    inst.components.weapon:SetOnAttack(onhit)

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "vegie_sword"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/vegie_sword.xml"

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(function(inst, owner)
        owner.AnimState:OverrideSymbol("swap_object", "swap_vegie_sword", "swap_vegie_sword")
        owner.AnimState:Show("ARM_carry")
        owner.AnimState:Hide("ARM_normal")
    end)
    inst.components.equippable:SetOnUnequip(function(inst, owner)
        owner.AnimState:Hide("ARM_carry")
        owner.AnimState:Show("ARM_normal")
    end)

    inst:AddComponent("timer")

    inst:ListenForEvent("ondeplete", ondeplete)
	
	inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    return inst
end

return Prefab("vegie_sword", fn, assets)

I be glad if someone could help!

Link to comment
Share on other sites

You can always listen for a timer and reset its time when attack.

local function fn()
  ...
  inst:ListenForEvent("timerdone", function(inst, data)...end)
end

and

local function onhit(inst, attacker, target)
  ...
  if inst.components.timer:TimerExists("ondeplete") then
    inst.components.timer:SetTimeLeft(5)
  else
    inst.components.timer:StartTimer("ondeplete",5)
  end
end
Link to comment
Share on other sites

On 5/24/2023 at 8:36 AM, Rickzzs said:

You can always listen for a timer and reset its time when attack.

local function fn()
  ...
  inst:ListenForEvent("timerdone", function(inst, data)...end)
end

and

local function onhit(inst, attacker, target)
  ...
  if inst.components.timer:TimerExists("ondeplete") then
    inst.components.timer:SetTimeLeft(5)
  else
    inst.components.timer:StartTimer("ondeplete",5)
  end
end



Like this?

 

local function onhit(inst, attacker, target)
   
   if attacker and attacker.components.combat and target then
    inst.components.weapon.damage = inst.components.weapon.damage + 5
	
   if inst.components.weapon.damage > 120 then
    inst.components.weapon.damage = 120
 
 if inst.components.timer:TimerExists("ondeplete") then
    inst.components.timer:SetTimeLeft(5)
  else
    inst.components.timer:StartTimer("ondeplete",5)
  end
end

 

Link to comment
Share on other sites

2 hours ago, Rickzzs said:

You can just fix it yourself..

I wish I could
But I'll try again bit later
I'm a complete newbie in this and really hard for me getting around fixing those kind of errors because I don't know what this error is referring to. I guess it needs a damage value? But there is weapon component and "set damage" in local function fn
Thank you so much for providing me a sample of code thought, I really appreciate that!

Link to comment
Share on other sites

55 minutes ago, RexySeven said:

I wish I could
But I'll try again bit later
I'm a complete newbie in this and really hard for me getting around fixing those kind of errors because I don't know what this error is referring to. I guess it needs a damage value? But there is weapon component and "set damage" in local function fn
Thank you so much for providing me a sample of code thought, I really appreciate that!

You wrote 'component' when it should be 'components'. :)

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
 Share

×
  • Create New...