Search Unity

GameFoundationSdk.inventory.FindItem

Discussion in 'Game Foundation' started by VektaCo, Jan 30, 2021.

  1. VektaCo

    VektaCo

    Joined:
    Sep 30, 2019
    Posts:
    31
    Does this function work? There are no examples online or in the samples.

    This always returns null
    Code (CSharp):
    1. GameFoundationSdk.inventory.FindItem(_ItemID);
    whereas doing it this way works, but is a lot of extra steps.

    Code (CSharp):
    1. InventoryItemDefinition Item = GameFoundationSdk.catalog.Find<InventoryItemDefinition>(_itemID);
    2.  
    3. List<InventoryItem> m_InventoryItems = new List<InventoryItem>();
    4.    GameFoundationSdk.inventory.FindItems(Item, m_InventoryItems);      
    5.  
    6. if (m_InventoryItems.Count > 0)
    7. {
    8.       return m_InventoryItems[0];
    9. }
     
  2. tony_c-unity3d

    tony_c-unity3d

    Unity Technologies

    Joined:
    Jul 18, 2019
    Posts:
    35
    Hi @VektaCo ,

    Apologies for the confusion, but the FindItem method in your code snippet uses the item id which is a unique GUID associated with the item, not the item definition key. To find all items with a specified definition, you'll need to use the 2nd approach to first find the inventory item definition based on the inventory item definition key THEN use that definition to find all items created using that definition.

    To elaborate, these inventory item id's are generated automatically and may look something like: "a66b0bfb-8174-4a95-a594-4faa78ab06e0" whereas the inventory item definition key is set by you in the editor and can be anything. For example, in our sample/01_InventoryBasics catalog, there are 2 inventory item definitions with keys: "apple" and "orange". These keys can be passed into catalog.Find<> (as your 2nd snippet demonstrates where _itemID is the inventory item definition key) to retrieve the associated inventory item definition.

    Hope this helps. If you have any other questions, please let me know. Thanks and have a great weekend!
     
    erika_d, VektaCo and saskenergy like this.