Auto Equip and Unequip based on Ammo

nathanj

Active member
Hello,

I'm working on a spear throw that is based off the Bow and Arrow items. I have it that the Spear (Bow) is always in the player's inventory but does not show in the UI. I have this all working where I can pick up a spear (ammo) and then equip the spear (bow) and then throw it (using a unique substate in the animator (duplicated bow substate with changed values and animations)).

1: Is there any way to have the spear (bow) only equippable when they have an ammo count of 1 or more and then automatically unquip when the ammo count is 0?

Don't mind having to write some code to do this but if you could tell me how you would approach it I would be most appreciative.

2: Noticing a weird behaviour I can't put my finger on, hoping you might have some insight. When I throw the spear and the Spear(ammo) is projected, it instantiates a Pickup item. The first time that the item is instantiated it is done so correctly. However, the second time I pick it up and throw it the the item collection is blank.


First Instantiation Second Instantiation
Correct.PNG empty.PNG

Any Idea why this would be? I tried in the demo UCC/UIS scene and added a spawnable item to the arrow and it works correctly. S obviously this is something with my set up, just hoping maybe you have an idea of why this might be.

Once again, thank you.
Nathan
 
Last edited:
Out of curiosity why are you using a Bow/Arrow setup instead of a simple Throwable (Grenade) setup?

1) You could have a script that monitors your Inventory or simply your ammo collection. You can use an event for that, find them in the "EventNames.cs" file. The you could have a new Equippable itemcollection (or use the same one as the Body if you have that) to add you SpearBow as equippable without it taking a slot from you equippable items.

That's proably the easiest solution, although there are many others, like a custom ItemSetRule, custom bridge, etc...

2) That's odd. Does it happen only after you pick it up? Perhaps the object is pooled but the value isn't being reset. On the Inventory Item Pickup try setting the pickup "Remove Items On Pickup" to false and perhaps "Pickup Copies" to true.

I hope that helps
 
I’ve used the grenade throw but it’s not satisfying or modifiable. I like being able pull back, aim and release amongst other modifiable traits.
I’ll check this out tomorrow. Thank you.
 
Hey,

OK, mostly there. Thanks

Few more questions, I'm trying to unequip my spear(bow) when ammo is 0 and equip it when 1. I'm listening to this
Code:
protected override void OnUseConsumableItemIdentifier(Item item, IItemIdentifier itemIdentifier, int amount)
for when the ammo is 0 and it works. Except I'm not sure how to Unequip the item at this point. I'm trying this
Code:
identifier = spearHolder.CreateItemIdentifier();   
        bridge.UnequipItem(identifier, 0);
but its not working. Any idea why?

Also, is there something I can call to disable the 3d objects of the spear as soon as the item is used and the projectile is instantiated?

Thanks again.
Nathan
 
I was thinking you could listen to the "c_Inventory_OnUpdate" on the UIS Inventory instead, and then check the amount of the spear (Arrow) to know if you should equip or unequip the Spear (Bow)

I just thought of another solution, would you could have the "Spear (Bow)" always soft equipped. Ans instead of unequipping completely you could simply disable/enable it depending on the amount of ammo you have.

Learn the different about soft and active equipped in the documentation:

As for Unequipping an Item, you need to be more precise than that. Items are often unique. In the code above you created a new spear item and tried unequipping that new spear. Instead you should find the currently equipped one and unequip that exact spear. Also you should unequip it using the bridge function instead of the InventoryBase function. So it would look something around those lines:

Code:
var inventory = bridge.Inventory;

//I assume that's the name you have for your ItemDefinitions.
 var spearHolder = InventorySystemManager.GetItemDefinition("SpearHolder");
 var spear = InventorySystemManager.GetItemDefinition("Spear");

//Get the ItemInfo using the ItemDefinition, the item info contains the Item (with the unique ID), itemStack, ItemCollection, etc...
 var nullableItemInfo = inventory.GetItemInfo(spearHolder);
 if (nullableItemInfo.HasValue == false) {
     //Item Not Found.
     return;
 }

 var itemInfo = nullableItemInfo.Value;
 var isActiveEquipped = bridge.IsItemActive(itemInfo.Item);
 var hasSpear = inventory.HasItem( (1, spear) );

 if (isActiveEquipped && !hasSpear) {
     //The Item is active equipped.
     
     //Unequip
     bridge.Equip(itemInfo, false);
 } else if(!isActiveEquipped && hasSpear){
     //THe Item is not active equipped.
     
     //Equip
     bridge.Equip(itemInfo, true);
 }

On the ItemSetRule for your SpearHolder you could set the "CanSwitchTo" to false, I think that should prevent you from being able to equip to it automatically.

I hope that helps
 
Yeah, the problem with check the Spear(ammo) count is when my player starts they have the Spear(bow) equipped but with 0 loaded consumable items. When they pick up the spear(ammo) item then the consumable value of the spear(bow) changes to 1 but the spear(ammo) value is still at 0.

I'll look into your suggestions and report back. I had it working with my own UI system previously, just gotta get it to work in the new system.

Thanks again for your time and help.
Nathan
 
So I tried making a new Throwable item and making the appropriate animation states and, yeah, it's much easier than the modifier bow and arrow set. I mean, there's some functionality like lock on target and velocity that aren't in the throwable by default but I might look into adding something like that eventually. But, I am more experienced with UCC now than I was when I first tried making my own custom spear throwable and am able to do a much better job than I had originally.
Originally I had written my own inventory system on top of the UCC one and I was able to handle all the item amounts and equpping internally but I just couldn't get it to work with UIS, which is totally fine because it really was a square peg in a round hole.
I've got a bunch of other things to attend to first before I work more on the Throwable item but yeah, 2 hours to set this up was def. the way to go.

Thanks again for your time,
Nathan
 
Top