Jump to content

Recommended Posts

I'm having a hard time trying to figure out how do I make armor get dropped when a weapon is equipped. I could drop the armor in the OnEquip function of the weapon, but what about when the weapon is already equipped and the player tries to equip an armor?

The code is like this, inside myweapon.lua :

local function UnequipArmor(inst)
    local itemEquipado = inst.components.inventoryitem.owner.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)

    if inst:HasTag("GunEquipped") and itemEquipado and not itemEquipado:HasTag("backpack") then
        inst.components.inventoryitem.owner.components.inventory:DropItem(itemEquipado)
    end
end

and inside OnEquip, I call this function. Everything is working fine. I just need to know:

how do I make the armor drop while the weapon is equipped, not just while equipping the weapon?

Thanks in advance!

Edited by Schulliya

I think in this case it's better to go with a ListenForEvent that your character receives.

local function CheckIfUnequipArmor(inst,data)
	if data and inst.components.inventory then
      	if data.eslot == EQUIPSLOTS.BODY and not data.item:HasTag("backpack") then
        	inst.components.inventory:Unequip(EQUIPSLOTS.BODY)
      	end
    end
end

--onequip function

inst:ListenForEvent("equip",CheckIfUnequipArmor,owner)

--onunequip function

inst:RemoveEventCallback("equip",CheckIfUnequipArmor,owner)


        		

If you add this to your weapon prefab it should work as you want it to :)

By adding the argument owner at the end of the listenforevent, we add the listenforevent to the owner, so your character. That means each time you will equip something, this function will run and unequip it if it's an armor.

  • Like 1
17 hours ago, Monti18 said:

I think in this case it's better to go with a ListenForEvent that your character receives.


local function CheckIfUnequipArmor(inst,data)
	if data and inst.components.inventory then
      	if data.eslot == EQUIPSLOTS.BODY and not data.item:HasTag("backpack") then
        	inst.components.inventory:Unequip(EQUIPSLOTS.BODY)
      	end
    end
end

--onequip function

inst:ListenForEvent("equip",CheckIfUnequipArmor,owner)

--onunequip function

inst:RemoveEventCallback("equip",CheckIfUnequipArmor,owner)


        		

If you add this to your weapon prefab it should work as you want it to :)

By adding the argument owner at the end of the listenforevent, we add the listenforevent to the owner, so your character. That means each time you will equip something, this function will run and unequip it if it's an armor.

thanks!

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
×
  • Create New...