Jump to content

Custom Character Special Shovel Durability


Recommended Posts

Hello everyone, I'm trying have a character be more efficient with gold tools specifically shovels.

 

Basically I want regular shovels to have less durability and gold shovels to have more durability

 

AddPrefabPostInit("shovel", function(inst)
    if inst.components.finiteuses and inst.components.inventoryitem then
        local durab = inst.components.finiteuses
        local old = durab.Use
        function durab:Use(num)
            local owner = inst.components.inventoryitem.owner
            if owner.prefab == "mycharacter" then
                old(durab, (TUNING.SHOVEL_USES / 2))
            else
                old(durab, num)
            end
        end
    end
end)
 
so when I try to use this instead of the shovel having 12.5 uses down from 25, it only has 2?
 
also how do I do the nice little code box i've seen some other use on here?
Link to comment
Share on other sites

Because game execute Use(1) when you use shovel.

But you rewrote function Use and now it's like Use(TUNING.SHOVEL_USES / 2). It's possible to do this only 2 times.

 

Right code:

AddPrefabPostInit("shovel", function(inst)	if inst.components.finiteuses then		inst.components.finiteuses:SetMaxUses(TUNING.SHOVEL_USES / 2)		inst.components.finiteuses:SetUses(TUNING.SHOVEL_USES / 2)	endend)

Edited by Maris
Link to comment
Share on other sites

@Maris, the problem with that is that all characters will be able to use the improved shovel.

 

@SkullRave, for code, press the <> button in the post reply options.

A shovel has 25 uses, so when your character uses one, it will take two hits (of 12.5 uses) to consume it.

 

You want

AddPrefabPostInit("shovel", function(inst)    if inst.components.finiteuses and inst.components.inventoryitem then        local durab = inst.components.finiteuses        local old = durab.Use        function durab:Use(num)            local owner = inst.components.inventoryitem.owner            if owner.prefab == "mycharacter" then                num = num or 1                old(durab, num * 2)            else                old(durab, num)            end        end    endend)

so instead of consuming 12.5 uses, it consumes 2, effectively halving the durability of the shovel.

Link to comment
Share on other sites

@darkxero I ended up using something like that before you posted and it seemed to work correctly, but there are some issues that I couldn't figure out. Also, thanks for letting me know how to add the code box :grin:

AddPrefabPostInit("shovel", function(inst)   if inst.components.finiteuses and inst.components.inventoryitem then      local durab = inst.components.finiteuses      local old = durab.Use      function durab:Use(num)          local owner = inst.components.inventoryitem.owner          if owner.prefab == "mycharacter" then              old(durab, 2)          else              old(durab, num)          end      end  endend)

^ This is what I ended up using, but when I try to do the opposite effect, e.g. make a golden shovel have double uses it doesn't work with this particular codeand I think that needs something more like Maris's code

AddPrefabPostInit("goldenshovel", function(inst)    if inst.components.finiteuses and inst.components.inventoryitem then		local owner = inst.components.inventoryitem.owner		if owner.prefab == "mycharacter" then			inst.components.finiteuses:SetMaxUses(TUNING.SHOVEL_USES * 2)			inst.components.finiteuses:SetUses(TUNING.SHOVEL_USES * 2)		end    endend)

^ when I use this I get a error saying "attempt to index local 'owner' (a nil value)"

So, TLDR. How do I use Maris's code, but check for an owner prefab?

Link to comment
Share on other sites

 Fixed a bug:

AddPrefabPostInit("shovel", function(inst)   if inst.components.finiteuses and inst.components.inventoryitem then      local durab = inst.components.finiteuses      local old = durab.Use      function durab:Use(num)          local owner = inst.components.inventoryitem.owner          if owner.prefab == "mycharacter" then              old(self, 2)          else              old(self, num)          end      end  endend)

​Second code is totally wrong because there is no owner on AddPrefabPostInit stage.

Use almost the same code: 

AddPrefabPostInit("goldenshovel", function(inst)   if inst.components.finiteuses and inst.components.inventoryitem then      local durab = inst.components.finiteuses      local old = durab.Use      function durab:Use(num)          local owner = inst.components.inventoryitem.owner          if owner.prefab == "mycharacter" then              old(self, 0.5)          else              old(self, num)          end      end  endend)

Edited by Maris
Link to comment
Share on other sites

AddPrefabPostInit("goldenshovel", function(inst)   if inst.components.finiteuses and inst.components.inventoryitem then      local durab = inst.components.finiteuses      local old = durab.Use      function durab:Use(num)          local owner = inst.components.inventoryitem.owner          if owner.prefab == "mycharacter" then              old(self, 0.5)          else              old(self, num)          end      end  endend)

^ funny enough this still reduces the use count by half at least for the golden shovel, just went and tested it and it makes the shovel have 50(default 100 for golden shovel) uses instead of 200.

 

Link to comment
Share on other sites

The code works fine.

 

The durability of the shovel is decreased by making a use consume two.

The durability of the golden shovel is increased by making a use consume half.

 

Or maybe you just said that and I'm reading wrong.

Link to comment
Share on other sites

Yeah I'm not saying your wrong or anything, and I agree the code should work like that, but it seemingly doesn't at least when I tried it.

 

So in the default game the golden shovel has 100 uses and it takes away 1% every time you dig something up with it.

 

With that code it should remove 0.5% with every use and 1% with every 2 uses right? When I used the code it was removing 2% with every use meaning the durability was actually going to be 50 uses.

 

I test it again to make absolute sure i'm correct/wrong, but this is definitely leaving me confused.

Regardless though, setting the max uses * 2 was getting the expected result only problem being I couldn't figure out how to get that to only affect the one character.

 

 

Link to comment
Share on other sites

The code was wrong after all.

 

A shovel has 25 uses. A golden shovel also has 25 uses.

 

When you use a shovel, you consume 1 use. When you consume 1 use, you lose 4%.

When you use a shovel, you consume 1 / TUNING.GOLDENTOOLFACTOR, so you consume 1/4 use. You lose 1%.

 

But the goldenshovel tool had 0.5, which is the double of 1/4.

 

Use this:

AddPrefabPostInit("shovel", function(inst)   if inst.components.finiteuses and inst.components.inventoryitem then      local durab = inst.components.finiteuses      local old = durab.Use      function durab:Use(num)          local owner = inst.components.inventoryitem.owner          if owner.prefab == "mycharacter" then              num = num or 1			  old(self, num * 2)          else              old(self, num)          end      end  endend)AddPrefabPostInit("goldenshovel", function(inst)   if inst.components.finiteuses and inst.components.inventoryitem then      local durab = inst.components.finiteuses      local old = durab.Use      function durab:Use(num)          local owner = inst.components.inventoryitem.owner          if owner.prefab == "mycharacter" then			  num = num or 1              old(self, num / 2)          else              old(self, num)          end      end  endend)

Now it works.

Link to comment
Share on other sites

Figured it out.

So it was only giving back 50 uses with 0.5 so I figured I would use a smaller number I halved 0.5 to get it to normal 100 uses then halved it again to 0.125 and it is working as I wanted it to. I think it has something to do with how golden tools are handled in the game making the math behind this a little extra complicated.

So, for future reference here is what it looks like now working as intended

AddPrefabPostInit("goldenshovel", function(inst)   if inst.components.finiteuses and inst.components.inventoryitem then      local durab = inst.components.finiteuses      local old = durab.Use      function durab:Use(num)          local owner = inst.components.inventoryitem.owner          if owner.prefab == "mycharacter" then              old(durab, 0.125)          else              old(durab, num)          end      end  endend)

Thank you both so much for helping me out here.

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