Forum rules - please read before posting.

Inventory Items - How to add attributes and how to add/remove items by code

I have 2 questions regarding Inventory and items

  1. So adding or removing item seems easy enough with the editor but how does it work by code. For example the code should check if I have space in inventory then add the item. To remove an item, it should check if I have the item in inventory then remove it.

  2. How can I implement attributes to these items, for example, Apple has an attribute "HealAmount" which determines how much health I can get when I use it, I would also need to know how to access that new attribute by code for that item. How can it be done?

Thanks!

Comments

  • To check how many items the Player is carrying, and add a new if there's space:

    private int maxSlotsAllowed = 10;
    
    public void AddItem (int itemID)
    {
        int slotsFilled = KickStarter.runtimeInventory.PlayerInvCollection.GetCount (false);
        if (slotsFilled < maxSlotsAllowed)
        {
            KickStarter.runtimeInventory.Add (itemID);
        }
    }
    

    Item attributes are known as "properties" in the Inventory Manager. You can create new properties in the Properties tab, and then assign a value for each item in the Items tab.

    Inventory items, and their properties, can be accessed via the ID numbers, which are listed beside their names in the Inventory Manager:

    public int GetIntegerPropertyValue (int itemID, int propertyID)
    {
        InvItem invItem = KickStarter.inventoryManager.GetItem (itemID);
        InvVar invVar = invItem.GetProperty (propertyID);
        return invVar.IntegerValue;
    }
    

    That will get the default/original property values, since you're referencing the original inventory item defined in the Inventory Manager. If you instead want to get the property values of a specific instance of an item (which can be modified at runtime for that instance only):

    public int GetIntegerPropertyValue (int itemID, int propertyID)
    {
        InvInstance invInstance = KickStarter.runtimeInventory.PlayerInvCollection.GetFirstInstance (itemID);
        InvVar invVar = invInstance.GetProperty (propertyID);
        return invVar.IntegerValue;
    }
    
  • Thank you I got most of it, but could you explain where the "PlayerInvCollection" comes from? is that the inventory Name you set in menus or? Because that is giving me an error in code, should I be referencing an inventory? if so then how can I?

    And what happens if there is two inventory, how would it know which one to check.

    Thanks

  • PlayerInvCollection is based on the overhauled inventory system, which was introduced in v1.72.0. If you're using an older version, I would recommend upgrading particularly if you're looking to access inventory through scripting.

    With the old system, property values of inventory items cannot be saved per-instance at runtime, but you can get an InvItem class for a held item with:

    InvItem invItem = KickStarter.runtimeInventory.GetItem (itemID);
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.