How do I print the number of bullets on the screen but without the need to equip a weapon

I already checked the documentation and until recently I was using the code mentioned here about the number of bullets:
But it only prints the number of bullets when I have a weapon equipped, since I have an inventory, I would like to see the number of bullets my weapon has, without equipping it. I was looking for some methods in "InventoryBase" but couldn't find a solution, I should mention that I don't really use UCC inventory, but I need the data, after all I just want to print it.
 
You can use the Item Amounts section of that page, except instead of getting the active item you can use GetItem.
 
You can use the Item Amounts section of that page, except instead of getting the active item you can use GetItem.
I don't understand how to implement GetItem.
What I want to do is manually with a variable or a constant to define the weapon that I'm using, so that it shows me the amount of ammunition it has.
Although it has a small description it is abstract to me, "GetItem (int slotID, IItemIdentifier itemIdentifier)"
 
GetItem(itemIdentifier, slotID) will simply return the Item of the ItemIdentifier you pass in. So once you've got the ItemIdentifier of this weapon, pass it in to GetItem (along with the slot ID). You can then use the "Item Amounts" script from that page, but replacing inventory.GetActiveItem(i) with the Item reference you just got.
 
GetItem(itemIdentifier, slotID) will simply return the Item of the ItemIdentifier you pass in. So once you've got the ItemIdentifier of this weapon, pass it in to GetItem (along with the slot ID). You can then use the "Item Amounts" script from that page, but replacing inventory.GetActiveItem(i) with the Item reference you just got.
Honestly, I do not understand, conceptually it is fine, but in practice ... Could you give me an example in the implementation(code)?
 
Try this to start with:
C#:
using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items;
using Opsive.UltimateCharacterController.Items.Actions;
public class GetAmmoTest : MonoBehaviour
{
    Inventory inventory;
    
    void Start() {
        inventory = GetComponent<Inventory>();
        
        // Get the item.
        Item item = null;
        foreach (Item i in inventory.GetAllItems()) {
            var itemType = (ItemType)i.ItemIdentifier;
            if (itemType.ToString() == "Pistol") { // You could also use the ItemType's ID if you know that in advance.
                item = inventory.GetItem(itemType.CreateItemIdentifier(), 0); // Returns the Item from the ItemIdentifier.
            }
        }
        
        // (From the example script) Get the item's consumable amounts by iterating through its ItemActions.
        if (item != null) {
            var itemActions = item.ItemActions;
            for (int j = 0; j < itemActions.Length; ++j) {
                var usableItem = itemActions[j] as IUsableItem;
                if (usableItem != null) {
                    var consumableItemIdentifier = usableItem.GetConsumableItemIdentifier();
                    if (consumableItemIdentifier != null) {
                        // The loaded amount is retrived from the UsableItem and the unloaded amount is retrieved from the inventory. 
                        int loadedConsumableAmount = usableItem.GetConsumableItemIdentifierAmount();
                        int inventoryConsumableAmount = inventory.GetItemIdentifierAmount(consumableItemIdentifier);
                        int totalConsumableAmount = loadedConsumableAmount + inventoryConsumableAmount;
                        Debug.Log($"Total consumable amount: {totalConsumableAmount}");
                    }
                }
            }
        }
    }
}
A couple of things worth noting:
- Inventory.GetAllItems() returns every possible inventory item, not only the ones in the character's current inventory. So you may want to add a check to see if the character currently holds that item at all.
- You could potentially use Inventory.GetAllItemIdentifiers instead of Inventory.GetAllItems() to make this a little simpler (it would only return ItemIdentifiers in the character's current inventor, and it would return consumable ItemIdentifiers meaning you could check their amount directly rather than having to go via the ItemActions), but since that method is only used by the inventory inspector I'm not 100% sure how reliable it would be. But feel free to try it out.
 
Try this to start with:
C#:
using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items;
using Opsive.UltimateCharacterController.Items.Actions;
public class GetAmmoTest : MonoBehaviour
{
    Inventory inventory;
   
    void Start() {
        inventory = GetComponent<Inventory>();
       
        // Get the item.
        Item item = null;
        foreach (Item i in inventory.GetAllItems()) {
            var itemType = (ItemType)i.ItemIdentifier;
            if (itemType.ToString() == "Pistol") { // You could also use the ItemType's ID if you know that in advance.
                item = inventory.GetItem(itemType.CreateItemIdentifier(), 0); // Returns the Item from the ItemIdentifier.
            }
        }
       
        // (From the example script) Get the item's consumable amounts by iterating through its ItemActions.
        if (item != null) {
            var itemActions = item.ItemActions;
            for (int j = 0; j < itemActions.Length; ++j) {
                var usableItem = itemActions[j] as IUsableItem;
                if (usableItem != null) {
                    var consumableItemIdentifier = usableItem.GetConsumableItemIdentifier();
                    if (consumableItemIdentifier != null) {
                        // The loaded amount is retrived from the UsableItem and the unloaded amount is retrieved from the inventory.
                        int loadedConsumableAmount = usableItem.GetConsumableItemIdentifierAmount();
                        int inventoryConsumableAmount = inventory.GetItemIdentifierAmount(consumableItemIdentifier);
                        int totalConsumableAmount = loadedConsumableAmount + inventoryConsumableAmount;
                        Debug.Log($"Total consumable amount: {totalConsumableAmount}");
                    }
                }
            }
        }
    }
}
A couple of things worth noting:
- Inventory.GetAllItems() returns every possible inventory item, not only the ones in the character's current inventory. So you may want to add a check to see if the character currently holds that item at all.
- You could potentially use Inventory.GetAllItemIdentifiers instead of Inventory.GetAllItems() to make this a little simpler (it would only return ItemIdentifiers in the character's current inventor, and it would return consumable ItemIdentifiers meaning you could check their amount directly rather than having to go via the ItemActions), but since that method is only used by the inventory inspector I'm not 100% sure how reliable it would be. But feel free to try it out.

That worked for me, thank you so much for your time.
 
Top