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.