Jump to content

Can't remove a tag using SetOnDroppedFn


Recommended Posts

Sorry for my english!

I created an item (a journal). I want to add a tag to player who puts this item in inventory and then remove this tag when player drops the item on the ground.

So, the 1st part works well. I add a tag by this local function:

local function journal3owner(inst, owner)
    if owner:HasTag("journal3_owner") then
    return
    end
        if owner:HasTag("player") then
        owner:AddTag("journal3_owner")
        end
end

and then add this into "local function fn ()" section:

inst.components.inventoryitem:SetOnPutInInventoryFn(journal3owner)

and everithing works fine. Player gets the tag when he puts the item in inventory (i know it by appearing the recipe tab that available only for those who has that certain tag)

And i can even Remove this Tag (i recognize it by desappearing the recipe tab that I mentioned before) by adding this local function:

local function journal3desowner(inst, observer)
    if observer:HasTag("journal3_owner") then
        observer:RemoveTag("journal3_owner")
        end
end

and by adding this to "local function fn ()" section:

inst.components.inventoryitem:SetOnActiveItemFn(journal3desowner)

And again everithing works fine. I pick the item by clicking on it (SetOnActiveItemFn stage) and my tag removes. Then i drop it on the ground and of course there is still no tag XD Purfect!

But there is no such stage as "SetOnActiveItemFn" when you use a gamepad!!! Therefore there is no tag removing (when using a gamepad). So i deside to change "SetOnActiveItemFn" on "SetOnDroppedFn" in my "local function fn()" section:

inst.components.inventoryitem:SetOnDroppedFn(journal3desowner)

but it doesn't work...when i try to drop the item on the ground it starts to levitate awkwardly and then the game crashes after several seconds(((

I'm not good in coding and do it all instinctively with the aid of this forum and existing mods... if somebody would help me, I'll be so glad! XD

Thanks!

 

 

Link to comment
Share on other sites

If I could make a suggestion and instead of trying to add/remove a tag to the player based on when they pick up/drop the the book, I would add a function to the inventory/container components to check the items your carrying for a tag instead. Something like this:

function ContainerCompPostInit(comp)
	comp.ContainerHasTag = function(self, tag)
		local items = self.inst.replica.container:GetItems()
		for k = 1, #items do
			local v = items[k]
			if v ~= nil then
				if v:HasTag(tag) then
					return true
				end
			end
		end
	end
end
AddComponentPostInit("container", ContainerCompPostInit)

function InventoryCompPostInit(comp)
	comp.InventoryHasTag = function(self, tag)
		local items = self.inst.replica.inventory:GetItems()
		local equip = self.inst.replica.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
		
		for k = 1, #items do
			local v = items[k]
			if v ~= nil then
				if v:HasTag(tag) then
					return true
				end
			end
		end

		if equip and equip.components.container then
			return equip.replica.container:ContainerHasTag(tag)
		end
	end
end
AddComponentPostInit("inventory", InventoryCompPostInit)

Then all you have to do is instead of checking if the player has the specific tag, run the "components.inventory:InventoryHasTag(tag)" function.

That way it keeps you from having to deal with any asynchronous behavior with the pickup/ondrop functions (you'd have to check when the game loads if you had the item in your inventory, if so apply the tag, etc)

Hope this helps :) 

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