Jump to content

How do I make a character who accidently breaks their item (among other things)?


Recommended Posts

So it's my first time modding this game, and while I am looking through the code myself, I figured I should get the community's advice for this.

I'm trying to make a character with a perk that causes their items to lose durability when unequipped. How exactly do I go about doing that?

Link to comment
Share on other sites

This event is pushed every time someone unequips something:

self.inst:PushEvent("unequipped", { owner = owner })

Normally I'd say you could listen for this event, and do the change when it happens, but for some weird reason, it only sends the owner and not the weapon instance, and since this event is called after the item has become unequipped, you have no way of finding the item.

I hope someone else has a better way, but I think the only way for you to do it, is to change the onequip functions for all equippable items. Something like this:

local AddCharacterClutsinessFn = function(inst, owner)
	if not owner or not owner:HasTag("MyCharactersTag") then
		return
	end
	
	if inst.components.finiteuses then
		inst.components.finiteuses:Use(1) -- remove 1 use
	elseif inst.components.armor then
		inst.components.armor:SetPercent(inst.components.armor:GetPercent() - 0.1) -- remove 10% durability
	end
end

AddComponentPostInit("equippable", function(self)
	if self.onunequipfn == nil then
		self.onunequipfn = AddCharacterClutsinessFn
	else
		local oldunequip = self.onunequipfn
		self.onunequipfn = function(inst, owner)
			oldunequip(inst, owner)
			AddCharacterClutsinessFn(inst, owner)
		end
	end
end)

Note that I rely on you to add a unique tag to your character. Here I've just used "MyCharactersTag", but you can use whatever unique tag you want, or you can check the name of owner.prefab instead.

Edited by Ultroman
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...