Sign in to follow this  
bullbro

(UNSOLVED) How can I make the durability effect the damage of my characters weapon?

Recommended Posts

bullbro    3

I have already got the item set for tuning, I just want to know how to make the durability (out of 100) affect the damage (out of 40). At the moment the weapon just does the default damage 40.

My config options:

configuration_options = {

{
        name = "wmwbasedamage",
        label = "wand's Damage:",
        options =
        {
            {description = "low", data = 20,hover = "20 Damage"},
            {description = "normal", data = 25,hover = "25 Damage"},
            {description = "high", data = 30,hover = "30 Damage"},
            {description = "higher", data = 35,hover = "35 Damage"},
            {description = "highest", data = 40,hover = "40 Damage"},
        },
        default = 40,
    },

}

 

Much appreciated!

Woscar.zip

Edited by bullbro

Share this post


Link to post
Share on other sites
thomas4846    859

if the item breaks when it hits 0 or can't be repaired then just have it that when it's used the damage should decrease a bit or how much you want

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(100 - inst.dura)
    inst.components.weapon.onattack = onattack

    inst.dura = 0

local function onattack(inst)
       inst.dura = inst.dura + 1
end

 

Edited by thomas4846

Share this post


Link to post
Share on other sites
Mr. Tiddles    1125

The solution to this is in the finiteuses component:

 

function FiniteUses:SetUses(val)
    local was_positive = self.current > 0
    self.current = val
    self.inst:PushEvent("percentusedchange", {percent = self:GetPercent()}) < This is the important line
    if self.current <= 0 then
        self.current = 0
        if was_positive and self.onfinished ~= nil then
            self.onfinished(self.inst)
        end
    end
end

function FiniteUses:GetPercent()  <This is also important
    return self.current / self.total
end

When the item holding the component is used, it pushes an event the item can listen out for. And so all you need is to make it listen:

    inst:ListenForEvent("percentusedchange",  function(inst)
    inst.components.weapon:SetDamage(DAMAGE * inst.components.finiteuses:GetPercent())
end)

Replace "DAMAGE" with whatever you want. the GetPercent just checks what percent of durability the item has left, and the * multiplies the damage by that percentage. So full durability means a 1x modifier, whereas half is a 0.5x modifier. That function belongs right beneath where you state the weapon's damage in its prefab.

Share this post


Link to post
Share on other sites
Mr. Tiddles    1125
5 minutes ago, thomas4846 said:

is the percentage normally 1

(just asking)

Yup! I tested it all to make sure. The game doesn't really do percentages as percentages - rather as decimals. So 1 is 100%, 0.5 is 50%, etc

Edited by Mr. Tiddles

Share this post


Link to post
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
Sign in to follow this