Jump to content

Condition to perform the function


Recommended Posts

,

local function CanTakeItemTweak(inst)
	local _old = inst.components.container.CanTakeItemInSlot
  	inst.components.container.CanTakeItemInSlot = function(self, item, slot)
    	return _old(self, item, slot) and self.canbeopened 
    end
end

And put this function in updatebackpack

Link to comment
Share on other sites

9 minutes ago, YakumoYukari said:

,


local function CanTakeItemTweak(inst)
	local _old = inst.components.container.CanTakeItemInSlot
  	inst.components.container.CanTakeItemInSlot = function(self, item, slot)
    	return _old(self, item, slot) and self.canbeopened 
    end
end

And put this function in updatebackpack

I don't know why, but it is don't work =(

Link to comment
Share on other sites

local function UpdateBackpack(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return inst
    end
  
  	CanTakeItemTweak(inst)
	inst:ListenForEvent("itemget", CheckStatus)
	inst:ListenForEvent("itemlose", CheckStatus)
end
AddPrefabPostInit("backpack", UpdateBackpack)

Did you do like this?
local function itself does not run without calling

Link to comment
Share on other sites

5 minutes ago, YakumoYukari said:

local function UpdateBackpack(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return inst
    end
  
  	CanTakeItemTweak(inst)
	inst:ListenForEvent("itemget", CheckStatus)
	inst:ListenForEvent("itemlose", CheckStatus)
end
AddPrefabPostInit("backpack", UpdateBackpack)

Did you do like this?
local function itself does not run without calling

123.thumb.png.61453fbb8018fc6e3118b8925e21c2ea.png

local function UpdateBackpack(inst)

    if not GLOBAL.TheWorld.ismastersim then
        return inst
    end
	
	local function CheckStatus(inst)
	
		if inst.components.container:IsEmpty() then
		
			if (inst.components.inventoryitem == nil) then
				inst:AddComponent("inventoryitem")
			end
			inst.components.inventoryitem.cangoincontainer = true
			
		elseif not inst.components.container:IsEmpty() then
		
			--inst:RemoveComponent("inventoryitem")
			inst.components.inventoryitem.cangoincontainer = false
      
			local function CanTakeItemTweak(inst)
				local _old = inst.components.container.CanTakeItemInSlot
				inst.components.container.CanTakeItemInSlot = function(self, item, slot)
					return _old(self, item, slot) and self.canbeopened
				end
			end
			
		end
		
	end
	
	CanTakeItemTweak(inst)
	inst:ListenForEvent("equipped", CheckStatus)
	inst:ListenForEvent("itemget", CheckStatus)
	inst:ListenForEvent("itemlose", CheckStatus)
	
end

AddPrefabPostInit("backpack", UpdateBackpack)

 

Edited by Tesumoto
Link to comment
Share on other sites

Done it. Here's the full code.

local function CanTakeItemTweak(inst)
	local _old = inst.components.container.CanTakeItemInSlot
	inst.components.container.CanTakeItemInSlot = function(self, item, slot)
		return _old(self, item, slot) and self.canbeopened
	end
end

local function CheckStatus(inst)
	if inst.components.container:IsEmpty() then
		inst.components.inventoryitem.cangoincontainer = true
		inst.components.container.canbeopened = true
	elseif not inst.components.container:IsEmpty() then
		inst.components.inventoryitem.cangoincontainer = false
		inst.components.container.canbeopened = false
	end
end

local function OnEquipTweak(inst)
	local _onequip = inst.components.equippable.onequipfn
	inst.components.equippable.onequipfn = function(inst, owner)
		_onequip(inst, owner)
		inst.components.container.canbeopened = true
	end

	local _onunequip = inst.components.equippable.onunequipfn
	inst.components.equippable.onunequipfn = function(inst, owner)
		_onunequip(inst, owner)
		inst.components.container.canbeopened = false
	end
end

local function UpdateBackpack(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return inst
    end
	inst:AddComponent("inventoryitem")

	CanTakeItemTweak(inst)
	OnEquipTweak(inst)

	inst:ListenForEvent("itemget", CheckStatus)
	inst:ListenForEvent("itemlose", CheckStatus)
end

AddPrefabPostInit("backpack", UpdateBackpack)

Don't try things like local GLOBAL. ~~~~
local is the antonym of GLOBAL...

Link to comment
Share on other sites

7 minutes ago, YakumoYukari said:

Done it. Here's the full code.


local function CanTakeItemTweak(inst)
	local _old = inst.components.container.CanTakeItemInSlot
	inst.components.container.CanTakeItemInSlot = function(self, item, slot)
		return _old(self, item, slot) and self.canbeopened
	end
end

local function CheckStatus(inst)
	if inst.components.container:IsEmpty() then
		inst.components.inventoryitem.cangoincontainer = true
		inst.components.container.canbeopened = true
	elseif not inst.components.container:IsEmpty() then
		inst.components.inventoryitem.cangoincontainer = false
		inst.components.container.canbeopened = false
	end
end

local function OnEquipTweak(inst)
	local _onequip = inst.components.equippable.onequipfn
	inst.components.equippable.onequipfn = function(inst, owner)
		_onequip(inst, owner)
		inst.components.container.canbeopened = true
	end

	local _onunequip = inst.components.equippable.onunequipfn
	inst.components.equippable.onunequipfn = function(inst, owner)
		_onunequip(inst, owner)
		inst.components.container.canbeopened = false
	end
end

local function UpdateBackpack(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return inst
    end
	inst:AddComponent("inventoryitem")

	CanTakeItemTweak(inst)
	OnEquipTweak(inst)

	inst:ListenForEvent("itemget", CheckStatus)
	inst:ListenForEvent("itemlose", CheckStatus)
end

AddPrefabPostInit("backpack", UpdateBackpack)

Don't try things like local GLOBAL. ~~~~
local is the antonym of GLOBAL...

Game crashed and I don't see any error.

I don't know why, but...
This don't work alone:

		inst:AddComponent("inventoryitem")

Need this full code:

	if (inst.components.inventoryitem == nil) then
		inst:AddComponent("inventoryitem")
	end

 

It is too difficult to write the conditions so that it is impossible to put items in the backpack only when it is in the inventory and the chest?
Now you can’t put anything in backpack when it on ground.
If this is too difficult, then you probably shouldn't try.

Link to comment
Share on other sites

1 minute ago, YakumoYukari said:

Working fine with me though,

And I add "CheckStatus(inst)" in "local function UpdateBackpack(inst)" =)

A little problem, if you put the first item in a backpack, then you can't put the second item in backpack .-.
But I think, I can fix it, maybe =)

I think this final beta code:

local function CanTakeItemTweak(inst)
	local _old = inst.components.container.CanTakeItemInSlot
	inst.components.container.CanTakeItemInSlot = function(self, item, slot)
		return _old(self, item, slot) and self.canbeopened
	end
end

local function CheckStatus(inst)
	if inst.components.container:IsEmpty() then
		inst.components.inventoryitem.cangoincontainer = true
	elseif not inst.components.container:IsEmpty() then
		inst.components.inventoryitem.cangoincontainer = false
	end
end

local function OnEquipTweak(inst)
	local _onequip = inst.components.equippable.onequipfn
	inst.components.equippable.onequipfn = function(inst, owner)
		_onequip(inst, owner)
		inst.components.container.canbeopened = true
	end

	local _onunequip = inst.components.equippable.onunequipfn
	inst.components.equippable.onunequipfn = function(inst, owner)
		_onunequip(inst, owner)
		inst.components.container.canbeopened = false
	end
end

local function UpdateBackpack(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return inst
    end
	
	if (inst.components.inventoryitem == nil) then
		inst:AddComponent("inventoryitem")
	end

	inst.components.container.canbeopened = false
	
	CanTakeItemTweak(inst)
	CheckStatus(inst)
	OnEquipTweak(inst)

	inst:ListenForEvent("itemget", CheckStatus)
	inst:ListenForEvent("itemlose", CheckStatus)
end

AddPrefabPostInit("backpack", UpdateBackpack)

This work fine.

Thank you very much, you helped me a lot.
Maybe in the future I will try to improve this code a little, but for now I think enough =)

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