Auto equip with dropping and unequipping items

allstar602

New member
Hello,

I was wondering if there was a built-in mechanism for items that re-equips the previous item when the current item is either dropped/removed from the inventory.

My character uses a melee weapon as it's primary item and is able to pick up one other item. In some cases, the object requires two hands and simply unequips the sword before switching to the new item. The character must drop or unequip the item in order to pick up another item. The weapon can never be dropped and it remains as the primary item. This currently works as intended. What I want to do next is re-equip the primary item after dropping or "placing" the item.

I took a look at the documentation and found the "Auto Equip" enum on the EquipUnequip item ability which looks similar to what I need. However, it seems to only work for pickups after looking at references in the code. I also tried a variety of functions from the Item Set abilities, as well as the Inventory and Item Set Manager.

I appreciate your time, thanks.
 
The ItemAction class implements the methods StartUnequip and Unequip, so you could create a simple custom ItemAction which manually equips a different item when it gets unequipped.
 
The ItemAction class implements the methods StartUnequip and Unequip, so you could create a simple custom ItemAction which manually equips a different item when it gets unequipped.
Do you mean the "Drop item event()" trigger a secondary equiped item?

I cant find the Equip next ability that way...
 
Last edited:
You'd just create a custom ItemAction (subclass), and in the Unequip override you can call the EquipNext ability. You can easily get a reference to the ability from any ItemAction: m_GameObject.GetCachedParentComponent<UltimateCharacterLocomotion>().GetAbility<EquipNext>()
 
Hmm, I tried it out and it should work. But for some reason the Unequip Item Event() or the Drop Item Event() on the dropable weapon wont go off untill I manually scrolls or changes weapon with "Q" or "E"... I must be missing something on my items
 
This is what I tried so far:

public void ChangeWeapon_Sam()
{
m_GameObject.GetCachedParentComponent<UltimateCharacterLocomotion>().GetAbility<EquipPrevious>();
Debug.Log("Equping previous");
}
Tried this on both drop event and unequip event

Character when sniper rifle is equiped:
sniperequip.PNG

Character after I pressed "G" (Drop weapon bind):
sniperunequip.PNG

There is no Item equiped on the character, I cant shoot or do something. But when I manually press equip next/prev wep bind or scroll on my mouse he throws the weapon out.
 
Just calling GetAbility like that won't start it. GetAbility returns a reference to the ability. So you'd need to do something like:

C#:
var locomotion = m_GameObject.GetCachedParentComponent<UltimateCharacterLocomotion>();
var equipPrev = locomotion.GetAbility<EquipPrevious>();
locomotion.TryStartAbility(equipPrev);
 
My C# is still quite basic, I apologize for that. But I tried exactly your code above but with the same result as before... Could it be something else with some wait for animation settings etc?
 
The exact moment you call TryStartAbility may be when the ability cannot be started due to priority or other abilities being active. You can try forcing it to ignore those checks by using locomotion.TryStartAbility(equipPrev, true, true) instead - this will ignore the ability's priotrity and start check.
 
Hmm, no matter what I try the drop still always occur AFTER I call change weapon manually ?
The true, true didnt really change anything Im affraid.
 
Ah I slightly misunderstood the problem - the auto equip isn't the issue, it's the item not actually being dropped right? However I've tested putting the script I shared a couple of posts ago on the Drop event on a ShootableWeapon in the demo scene and it works fine. Did you get this issue happening when dropping an item before? Does your item have a Drop Prefab assigned?
 
I have a drop prefab attached and the drop works perfectly, except that I have to manually switch weapon for it to throw it out on the ground...
I will check out the demo scene and compare all settings to my own and come back here if I find whats wrong!

I appreciate your help Andrew!
 
Sorry for my late reply @Andrew , but I figured it out! I had to change the priority on the drop to above of the equip next stuff and that did the work!

I also changed from rigidbody to Trajectory object, highly recomend that on the dropped prefab for anyone else who reads this.
solved.PNG
 
Top