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);
    
  • Hi Cris, this line of code adds an item to the inventory side by side, if I collect 10 oranges in the inventory there are 10 oranges next to each other and I would like there to be a number of oranges. Is there another line of code for this?

    AC.KickStarter.runtimeInventory.Add (X, Y);

  • You can find all instances of the RuntimeInventory's Add function in the Scripting Guide here.

    I'm not clear on your meaning. Are you looking to stack multiple items into the same slot?

    If possible, please share screenshots that illustrate the behaviour you're getting vs the behaviour you want.

  • This is exactly what I want in the first image, using the script to add it to the AC inventory.

    https://uploaddeimagens.com.br/imagens/H8W7OLs

    In the second image, this is what happens when I use the AC code line to add items to the inventory.

    https://uploaddeimagens.com.br/imagens/3HJVORU

  • Does the item get added properly when running the Inventory: Add or remove Action, and is the item's Slot capacity set to at least 10?

    Try this instead of the above:

    int amount = 1;
    KickStarter.runtimeInventory.PlayerInvCollection.Add (new InvInstance (itemD, amount));
    
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.