Search Unity

Fetching inventory item catalog after initialization

Discussion in 'Game Foundation' started by mikko_to, Jan 14, 2021.

  1. mikko_to

    mikko_to

    Joined:
    Oct 14, 2013
    Posts:
    21
    Hi,

    I'm not able to figure out how to, after initializing Game Foundation, how to fetch available Inventory Items or check the amount of items from the item catalog during runtime.

    In GF 0.8 version tutorial (https://docs.unity3d.com/Packages/c...nual/Tutorials/02-PlayingWithRuntimeItem.html) section "Getting the inventory item definition at runtime" the definition key is hard-coded ("MyFirstItem"), but I'd like to first have a list out of inventory items in the catalog and from there get the key (for example, to generate one collectible item by randomly picking it from the generated Inventory Item list or to pinpoint some item based on attached tags).

    Any guidance on how to get a grab on the available inventory item catalog?

    PS. Great work on Game Foundation so far! You're combining some really great and often used features into a nice package!
     
    erika_d likes this.
  2. U_AdrienPDB

    U_AdrienPDB

    Unity Technologies

    Joined:
    Aug 14, 2019
    Posts:
    8
    Hi mikko_to,

    To retrieve a list of catalog items from the catalog you have the
    GameFoundation.catalog.GetItems
    method. You also have a
    GameFoundation.catalog.FindItems
    if you want to filter the items.

    Here are some snippet to showcase how to use them:
    Code (CSharp):
    1. void NoFilter()
    2. {
    3.     // You can use any type of catalog item for this list.
    4.     List<InventoryItemDefinition> itemDefinitions = new List<InventoryItemDefinition>();
    5.     GameFoundationSdk.catalog.GetItems(itemDefinitions);
    6.  
    7.     foreach (InventoryItemDefinition definition in itemDefinitions)
    8.     {
    9.         // Process your definition here ...
    10.     }
    11. }
    12.  
    13. void TagFilter()
    14. {
    15.     const string tagKey = "myTag";
    16.  
    17.     // You can use any type of catalog item for this list.
    18.     List<InventoryItemDefinition> itemDefinitions = new List<InventoryItemDefinition>();
    19.     Tag tag = GameFoundationSdk.tags.Find(tagKey);
    20.     GameFoundationSdk.catalog.FindItems(tag, itemDefinitions);
    21.  
    22.     foreach (InventoryItemDefinition definition in itemDefinitions)
    23.     {
    24.         // Process your definition here ...
    25.     }
    26. }
    27.  
    28. void ComplexFilter()
    29. {
    30.     const string propertyKey = "myProperty";
    31.  
    32.     // You can only use CatalogItem for this list.
    33.     List<CatalogItem> itemDefinitions = new List<CatalogItem>();
    34.     GameFoundationSdk.catalog.FindItems(
    35.         catalogItem => catalogItem is InventoryItemDefinition definition
    36.             && definition.HasMutableProperty(propertyKey),
    37.         itemDefinitions);
    38.  
    39.     foreach (CatalogItem catalogItem in itemDefinitions)
    40.     {
    41.         // Process your catalog item here ...
    42.     }
    43. }
    44.  
    I hope this answered your question.

    Thank you ! I hope Game Foundation will help you make a great game. :)
     
  3. mikko_to

    mikko_to

    Joined:
    Oct 14, 2013
    Posts:
    21
    erika_d likes this.
  4. DenariiGames

    DenariiGames

    Joined:
    Dec 29, 2019
    Posts:
    13
    I'm searching for a way to find InventoryItem with a specific static property. Anyone have an example?
     
  5. richj_unity

    richj_unity

    Unity Technologies

    Joined:
    Sep 23, 2019
    Posts:
    40
    Do you want to search just by key, or by key+value? Is this a case where tags wouldn't be a good fit?
     
  6. DenariiGames

    DenariiGames

    Joined:
    Dec 29, 2019
    Posts:
    13
    Key+Value... I could use tags, but then its not as structured as I might like.
     
  7. richj_unity

    richj_unity

    Unity Technologies

    Joined:
    Sep 23, 2019
    Posts:
    40
    There's not currently a method for finding by static properties, but you can use a predicate function to find inventory items.

    Code (CSharp):
    1. // find all my "type=apparel" inventory items
    2. var collectionToPopulate = new List<InventoryItem>();
    3. GameFoundationSdk.inventory.FindItems(
    4.     item =>
    5.     {
    6.         item.definition.TryGetStaticProperty("type", out var typeStaticProperty);
    7.         return typeStaticProperty.AsString() == "apparel";
    8.     },
    9.     collectionToPopulate);
    edit: I had this as "find all my tier 2 items", but then I saw your video where tier was a mutable property - just to be clear that this snippet is about searching by static properties
     
    Last edited: Jul 13, 2021
    erika_d likes this.
  8. DenariiGames

    DenariiGames

    Joined:
    Dec 29, 2019
    Posts:
    13
    I could use Tags, but an item can only ever be one type so this makes more sense. I will use it to group/filter by type (e.g. weapons, apparel, etc). Thanks much!
     
    erika_d likes this.
  9. DenariiGames

    DenariiGames

    Joined:
    Dec 29, 2019
    Posts:
    13
    Will FindItems work with StackableInventoryItems? I can't seem to get stackables to get returned with the error Cannot convert lambda expression to type 'Tag' because it is not a delegate type

    Code (CSharp):
    1.             var collectionToPopulate = new List<StackableInventoryItem>();
    2.             GameFoundationSdk.inventory.FindItems(
    3.                 item =>
    4.                 {
    5.                     return item.GetMutableProperty("slot").AsInt() == (int)EquippedSlot.None;
    6.                 },
    7.                 collectionToPopulate
    8.             );
    9.  
     
    erika_d likes this.
  10. richj_unity

    richj_unity

    Unity Technologies

    Joined:
    Sep 23, 2019
    Posts:
    40
    Hmm, it looks like it's trying to call the wrong method signature for some reason. You might try declaring the lambda function as a variable to pass into FindItems.

    We'll look into a way we can prevent this from happening.
     
    erika_d and DenariiGames like this.