Weapon is Empty, Picking Up Ammo for that Weapon Doesn't Reload it Even if AutoReloadType = Empty

airoll

Member
Hi, I am currently using UCC + UIS, and have set it up such that I have a weapon item AssaultRifle and an ammo item AssaultRifleBullet (ID = 1 for this example) in my main inventory. Once I deplete all my AssaultRifleBullets, it disappears from the inventory. My ammo item category is not unique and not mutable.

When I then go to pick up new AssaultRifleBullet ammo items in an InventoryPickup in the game world, that AssaultRifleBullet gets created with a brand new ID (ID = 2) (e.g. from ObjectPool.Spawn(...)). Therefore, when the Reload ability checks if it ShouldReload(...), it just does a naive check:

Code:
reloadableItem.GetReloadableItemIdentifier() != itemIdentifier

This check fails because the AssaultRifleBullet that was picked up has a brand new ID (ID = 2), and it does not match the old ID (ID = 1). My question is: How can I fix this problem? A few things come to mind but it's not clear how to do this.
  1. ItemCollection actually deals with this okay if the pre-existing AssaultRifleBullet (ID = 1) is still in the inventory. That's because ItemCollection.CanItemStack(...) checks to see if the AssaultRifleBullets are similar and then it adds the new amount to the old amount. The problem is that when the pre-existing AssaultRifleBullet (ID = 1) is no longer in the inventory. This could theoretically be addressed by not removing the item definition even if the amount is zero. But I have no idea how to do this and how much it would break other logic.
  2. Override any instance where we are doing a naive comparison (e.g. Reload) and force cast it to an Item so that we can call Item.StackableEquivalentTo(...).
  3. I'm not coming up with any other ideas. Any more elegant way to fix this?
 
Just a quick update, (2) doesn't quite work because Shootable Weapon tries to reduce the item identifier amount using:

Code:
m_Inventory.AdjustItemIdentifierAmount(m_ConsumableItemIdentifier, -reloadAmount)
 
Interesting.

Normally an Item that is both Immutable and Common should always have the same ID, so I find it it odd that it is not the case for you.
During Edit time it won't have the same ID, but as soon as the game starts the items are initialized and replaced with the correct reference and ID.
I just tested it in my demo scene and it that is indeed the case. As soon as I press play the ID gets replaced by the correct ID and it is constant (always the same no matter how many times I press play). That's because the Item is Immutable and Common, so they are point to the same "Default" Item that is attached to the ItemDefinition.

Could you double check the IDs at runtime (You can see them in the inspector)?
1632988038263.png

We made a lot of changes to the integration and the ammo in particular during the past month, but we haven't made any of these improvements public yet. So I'm inclined to ask you to wait for the new release coming out next week (or the week after). Then if the issue persists even after that update I'll examine it further and make a quick update.
 
Ok sounds good to me. To outline a little bit more when this happens: If I insert a player into the scene with the ammo in its loadout, and press play, and then in the game an item pickup is generated via ObjectPool.Instantiate(...), then the IDs will be different. If all objects were in the scene to start with when I press play, then they share the same ID as you mentioned.
 
Ah... that's very interesting. I will try to replicate this, it might be an obscure bug.

EDIT: I can't seem to replicate your issue even spawning item pickups at runtime both manually or using a Dropper (it uses ObjectPool internally).

What type of pickup are you using for the one that gets spawned? Is it a custom pickup script, if so are you sure you initialized the item correctly?
Normally ItemObject and Inventories initialize the item on awake, so I don't see how your item would have the wrong ID.
Try removing the ammo item and add it again in the inspector to see if it changes anything. And double check you haven't modified any of the Item Attributes on the ammo (since it is Immutable and Common you shouldn't really have item attribute on it, just definition and category attributes).

If it is still a problem after the major update i will ask you to send me your project such that I can reproduce the issue.
 
Last edited:
Ah I see, I think it's related to the fact that my Ammo actually has Item Attributes. I noticed that when it had an Item Attribute, when I equipped my character with the loadout, it would change the AssaultRifleBullet from "default" to "custom". Once I removed the Item Attribute, it stayed at "default".

However, I actually do want to make my ammo mutable, since I have mechanics in my game that I want to use to modify the projectile lifetime (to control how far the projectile can go), which I can bind to the Projectile using ItemObject. Therefore I have a single Item Attribute called Lifetime in my Ammo category. However, this brings up another issue, which is that:

Code:
Equals and == will say whether two items are the same (by reference for mutable, by value for immutable) (from Item.cs)

I suppose I could make Lifetime an attribute of my weapon (which is mutable) instead of my ammo, and make ammo have no item attributes, and then set the lifetime of the projectile from ShootableWeapon. Does that sound like the right approach from a design perspective (at least the part about keeping the ammo immutable)?
 
Quick update here - I ended up adopting the proposal I suggested, which is to make Lifespan an attribute of the weapon instead of the ammo. It works now! Sorry for having to do the investigation work!
 
I'm glad you found the issue and a good solution on how to fix it.
I think the solution you came up with was the right one.
 
Top