Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Multiple instances of Inventory items.

Discussion in 'Economy' started by stu_dious, Mar 7, 2022.

  1. stu_dious

    stu_dious

    Joined:
    Feb 19, 2013
    Posts:
    32
    Hi, in Cloud Code, grantInventoryItem has a quantity parameter allowing, for example, 3 health packs or 5 bombs to be added to the player's inventory.

    Economy.PlayerInventory.AddInventoryItemAsync appears to be for single items so to add 5 bombs I'm assuming I'd be calling this 5 times.

    Is the assumption that, for an item type that the player could hold many instances off then it would be better to class this item as a currency, allowing us to easily add or remove multiple instances at once while inventory items should be limited in number.

    I think Chilli Connect had a Catalogue of items each with a quantity value.

    Thanks, s
     
    Last edited: Mar 7, 2022
    peter385 likes this.
  2. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi Stu_dios,

    Thanks for sharing your observation. You are correct, the Economy.PlayerInventory.AddInventoryItemAsync doesn't have a quantity element, it only supports the addition of a single inventory item at a time.

    If you want to add multiple items at once you could
    • Iterate over the AddInventoryItemAsync
    • Use a Virtual Purchase with no Cost and more then 1 item in the Inventory Item Amount
    • Use Cloud Code
    The assumption is that each item added to the player's inventory will be unique and therefore a seperate instance e.g. a sword. Each one could have some custom configuration data attached to it e.g damage = 50, weight = 30. But each one can also have it's own instance data e.g. remainingDurability =5, dyeColor = #FF2299

    Inventory Instances
    Each instance of an inventory item can have different instance data. For example, two swords that are otherwise identical could have Instance data associated with the player's instance, giving each of them different “damage” values.​

    The Player Instance of an Inventory item has a reference to the configuration Custom Data and its own Instance Data.

    If your intention is to STACK multiple identical items in the player's inventory e.g. 5 x bombs, then the advice would be to use the Instance Data to hold your own quantity counter. Setting the item as a currency wouldn't be recommended as you only have a limited number of 12 currencies at your disposal.

    We have logged your interest in stackable inventory items and being able to add more than one at once. We'll be sure to share any updates on the forum.

    Thanks,
    Laurie
     
  3. stu_dious

    stu_dious

    Joined:
    Feb 19, 2013
    Posts:
    32
    Hi @Laurie-Unity, thanks for your feedback and the tip on Instance Data as a quantity counter; I'll give that a try. And yeah, the limit on currencies was a concern ... we just wanted to avoid multiple calls to simply add 3 of an item, for example. I'll admit, it didn't feel right that we should re-class an inventory item as currency just to have the discussed functionality so nice we can avoid doing so.
    Cheers, s
     
  4. stu_dious

    stu_dious

    Joined:
    Feb 19, 2013
    Posts:
    32
    Hi @Laurie-Unity, I've added stackable item support (C#) based on an inventory item having an "amount" entry in its instanceData.

    I'm trying to get the same thing working in Cloud Code. I can pull / add / delete / update a player inventory item but can't see a way to pull the same item from the game inventory. A new player inventory item has null instanceData so I need to check if the game inventory item has customData with an "amount" entry so that I know whether or not it's a stackable item.

    Hope that makes sense.

    As an aside, my limited javascript knowledge was failing me :) From the docs, I think you can filter getPlayerInventory with an itemId to pull only those items but I'm getting invalid responses. I was trying variations on this:

    const result = await inventoryApi.getPlayerInventory(projectId, playerId, { inventoryItemIds: { itemId } } );

    Thanks, s
     
    Last edited: Mar 16, 2022