How to cap the inventory size to a max amount without Ultimate Inventory?

zambii

New member
My intention is to have the player be able to obtain up to two weapons, but no more than two even though there will be more around available for pick up. I am unable to control the number of weapons the player has in their inventory. I am aware that I can control the number of item types but my intention is to control the total number of weapons regardless of their type such as to allow any combination of types but only two items in total.

Thanks in advance!
 

Justin

Administrator
Staff member
This is not currently supported, but I do have it on my list of things to add.
 

Andrew

Moderator
Staff member
Try this as a starting point:
C#:
inventory = GetComponent<InventoryBase>();
int count = inventory.GetAllItemIdentifiers().Count;
This will include items from all categories (including Body, ammo, etc), so you may want to improve it by instead iterating through the ItemIdentifiers and checking its item category via the ItemDefinition.
 

zambii

New member
Hello again, I know it took me some time but I am finally able to continue working on this. I have tried a couple of things and this one seems to be working, sort of...
1600326548388.png

The only problem I have here is that 'GetAllItems()' returns all the items under the 'ItemPlacement' object regardless of the character having them in the inventory or not.
1600326369590.png

This is not what I want. My intention is to verify the items currently in the inventory to know how many items of the types I'm looking for is the character currently carrying. The character starts with only one weapon (revolver) but the result of 'weaponCount' is always 4. This was confirmed by deleting one weapon of each type (a revolver and a pistol) and the result of 'weaponCount' was now 2 as expected.

So it is working, but I need to access the items currently being carried by the character, what am I missing? :unsure:

EDIT: My UCC Version is 2.1.10
 

Andrew

Moderator
Staff member
You can use InventoryBase.GetItemIdentifierAmount(...) to return the current amount of the specified ItemIdentifier. Item.ItemIdentifier will return that Item's ItemIdentifier.
C#:
int count = 0;
List<Item> allItems = inventory.GetAllItems();
Inventory inventory = GetComponent<Inventory>();
foreach (Item item in allItems) {
    if (inventory.GetItemIdentifierAmount(item.ItemIdentifier) > 0) {
        count++;
    }
}
 

zambii

New member
Thank you very much for your fast reply.:D

That suggestion gives me the same result as my previous post, in that it returns the total items under the 'ItemPlacement' object :(. However, I did find the solution thanks to your suggestion, as it did give me a idea, this is what works for me:
C#:
private bool ValidateWeaponsInInventory()
    {
        var maxAmmount = 2;
        var weaponCount = 0;
        var itemTypes = _itemTypes.ToList();
        var inventory = GetComponent<Inventory>();
        foreach(var item in inventory.GetAllItemTypes())
        {
            var match = itemTypes.Contains(item);
            if(!match)
                continue;

            weaponCount += (int)inventory.GetItemTypeCount(item);
        }
        return weaponCount < maxAmmount;
    }

The only difference here is that 'GetAlItemTypes()' actually returns the total items in the inventory if they really are inside the inventory, whereas 'GetAllItem()' returns the previously stated. My concern here is that 'GetAllItemTypes()' is for editor only. Before I take any action I would like to ask, what would be the consequences of allowing 'GetAllItemTypes()' to be used at all times (since this is meant only for Editor use), if any?
 

Andrew

Moderator
Staff member
The script I posted above definitely doesn't do what you want? For example, if I use it in the demo scene with Nolan carrying only the AssaultRifle + AssaultRifleBullets, it returns 2, even though there are several more object under his ItemPlacement parent.

I'm not exactly sure on if GetAllItemTypes() is safe for runtime use, but since it's flagged as being used only by the inspector I would avoid it if possible.
 

zambii

New member
The script I posted above definitely doesn't do what you want? For example, if I use it in the demo scene with Nolan carrying only the AssaultRifle + AssaultRifleBullets, it returns 2, even though there are several more object under his ItemPlacement parent.

I'm not exactly sure on if GetAllItemTypes() is safe for runtime use, but since it's flagged as being used only by the inspector I would avoid it if possible.

That is exactly what I want but it does not do that, it just returns me everything under the ItemPlacement parent. Now I did mention that my UCC version is 2.1.10 and I do know there was a refactoring of the inventory in version 2.2 so maybe that is why I do not get the correct result in this version.

But I figured it out and I made my own workaround to avoid using the list flagged as inspector only so it's all good. Thanks a lot for the response and your help, definitely guided me in the right direction!
 
Would you care to elaborate on what your function above checks for? Does it only check for the item category or does it check the ammo too?
 

zambii

New member
Would you care to elaborate on what your function above checks for? Does it only check for the item category or does it check the ammo too?

Andrew's code checks for AssaultRifle and AssaultRiffleBullet ItemIdentifiers as he mentioned in his last comment that the ItemPlacement parent had several more objects under it.
 

zambii

New member
This works anywhere as it is pretty generic, I personally have this on a separate script that is accessible to all others.
 
Okay, i'm using a modified version of the pickup script in the doc for UCC(2.2.4). It doesn't use "item" and uses Itemtype instead. How would I get it to work with the doc pick up script?
 

Andrew

Moderator
Staff member
Hi Opticgamer, if you have a new question could you create a new thread about it please, I'm not really following what you're trying to fix here.
 
Top