Sign in to follow this  
Hector1324

Trying to find an item on the player's inventory

Recommended Posts

Hector1324    3

I want to know if there is any function i can call to check if an item is on the players inventory or if it's equipped and then return the item prefab, im using FindItem(), but that only checks if the item is in the inventoryslots. i want to make an object that after being eaten gives durability to a weapon

what im doing is the following


local function PushMana(inst, eater)
	if eater.components.inventory then
		local item = eater.components.inventory:FindItem(function(item) return item.prefab == "megumin_staff" end)
		if item ~= nil then
			local uses = item.components.finiteuses.current
			item.components.finiteuses:SetUses(uses+1)
		end
	end
end
--------------------------------------------------------------------------
--------------------------------------------------------------------------

so i was wondering if there is a function that do the same but for que equipslots

Share this post


Link to post
Share on other sites
local function PushMana(inst, eater)
	if eater.components.inventory then
		local item = function(item) return item.prefab == "megumin_staff" end
		local itemfound = function(item)
			for k,v in pairs(eater.components.inventory.itemslots) do
				if item(v) then
					return v
				end
			end
			for k,v in pairs(eater.components.inventory.equipslots) do
				if item(v) then
					return v
				end
			end
		end
    
		if itemfound ~= nil then
			local uses = itemfound.components.finiteuses.current
				itemfound.components.finiteuses:SetUses(uses+1)
			end
		end
end

I didn't find any function in inventory.lua that find itemslots and also equipslots(or just I don't know how), so code become very messy.

Nevertheless, it will work.

  • Thanks 1

Share this post


Link to post
Share on other sites
Hector1324    3

Thanks man, i really appreciate your help

Edit:

I tried using the code you gave me, and i got the error:

attempting to index itemfound (a funtion value)

i searched  about what it meant and found that the part of itemfound wasn't being executed, so i modified it a little bit by first creating the function, and then calling it to get the item, and i also took the final part of the function FindItem() on inventory.lua

Here is the code

local function PushMana(inst, eater)
	if eater.components.inventory then
		local itemfound = function(func)
			for k,v in pairs(eater.components.inventory.itemslots) do
				if func(v) then
					return v
				end
			end
			for k,v in pairs(eater.components.inventory.equipslots) do
				if func(v) then
					return v
				end
			end
			if eater.components.inventory.activeitem and fn(eater.components.inventory.activeitem) then
        		return eater.components.inventory.activeitem
    		end

    		local overflow = eater.components.inventory:GetOverflowContainer()
    		return overflow ~= nil and overflow:FindItem(fn) or nil
		end

		local item = itemfound(function(item) return item.prefab == "megumin_staff" end)
    
		if item ~= nil then
			local uses = item.components.finiteuses.current
				item.components.finiteuses:SetUses(uses+1)
		end
	end
end

Thanks a lot for the help Combustiblemon

Edited by Hector1324
  • Like 1

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