Jump to content

Dropped Item from inventory


Recommended Posts

hello

this will be my last thread asking for assistance

For my custom character I wish to find a way to Transform:SetScale any item they drop from their inventory. The items on the ground behave the same as usual, they just have a different scale for cosmetic purposes. (in this case our character is slightly smaller than others)

I am positive it's about the component "inventoryitem.lua" with "InventoryItem:OnDropped". There's a ListenEvent:"ondropped" too however I just don't know how to reference any item that gets dropped. We want this to happen for this char only. Once again I don't know if I should do a AddComponentPostInit or just a function in character.lua?

help

Link to comment
Share on other sites

You only need to extend the DropItem function of your character's inventory component.

AddPrefabPostInit("melium", function(inst)
	local old = inst.components.inventory.DropItem
	inst.components.inventory.DropItem = function(self, item, wholestack, randomdir, pos)
		local droppedItem = old(self, item, wholestack, randomdir, pos)
		local scaler = droppedItem:AddComponent("scaler")
		scaler:SetScale(0.4)
	end
end)

 

Link to comment
Share on other sites

Ah yes, forgot to return the item. Also forgot to make sure to only execute the code if the item was actually dropped.

AddPrefabPostInit("melium", function(inst)
	local old = inst.components.inventory.DropItem
	inst.components.inventory.DropItem = function(self, item, wholestack, randomdir, pos)
		local droppedItem = old(self, item, wholestack, randomdir, pos)
		if droppedItem == nil then
			return
		end
		local scaler = droppedItem:AddComponent("scaler")
		scaler:SetScale(0.4)
		return droppedItem
	end
end)

 

Edited by Ultroman
Link to comment
Share on other sites

The inventory component has a function called IsItemEquipped(inst)

AddPrefabPostInit("melium", function(inst)
	local old = inst.components.inventory.DropItem
	inst.components.inventory.DropItem = function(self, item, wholestack, randomdir, pos)
		local doShrink = self:IsItemEquipped(item)
		local droppedItem = old(self, item, wholestack, randomdir, pos)
		if droppedItem == nil then
			return
		end
		if doShrink then
			local scaler = droppedItem:AddComponent("scaler")
			scaler:SetScale(0.4)
		end
		return droppedItem
	end
end)

 

Link to comment
Share on other sites

For future reference, this is probably a better method. Applies scale only to items directly unequipped from equip-slots.

-- NEW SOLUTION

if GLOBAL.TheNet and GLOBAL.TheNet:GetIsServer() then
	AddPrefabPostInit("melium", function(inst)
		-- When you unequip something, an event is pushed, with information about what happened.
		-- As such, we do not need to extend any functions. We can just listen for the event.
		-- inst is the player holding the item, and data is a bunch of data about what just happened.
		-- This is AFTER the item has been unequipped, BUT the data contains information about which
		-- slot it was taken from, which item it was, and whether it slipped out unintentionally (frogs, rain).
		inst:ListenForEvent("unequip", function(inst, data)
			-- If you want to restrict this to certain equip-slots, just check e.g.
			-- if data.eslot == EQUIPSLOTS.HANDS
			-- if data.eslot == EQUIPSLOTS.BODY
			-- if data.eslot == EQUIPSLOTS.HANDS
			-- But anyway, you know this item is equippable and everything, since it was just unequipped
			--  by the game, so no more checks needed, really.
			
			if data.item then
				data.item.Transform:SetScale(0.85,0.85,0.85)
			end
		end)
	end)
end

 

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