Search Unity

Using Game Foundation Inventory via Interface Wrapper

Discussion in 'Game Foundation' started by dasparadoxon, Sep 12, 2021.

  1. dasparadoxon

    dasparadoxon

    Joined:
    Aug 16, 2014
    Posts:
    11
    Just to give some feedback : Using the Game Foundation as Inventory Backend via a Inventory Interface Wrapper for my Interaction System works like a charm. :)

    So far I am done with the Custom Inspectors for using the Inventory for checks like "is that inventory needed to execute that Action in the inventory" and "add the inventory item to the players inventory".

    Really cool.

    Wrapper :
    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3. namespace InterActionProject
    4. {
    5.     /// <summary>
    6.     /// Provides the Interface for writing an Inventory Wrapper for Interaction Project
    7.     /// </summary>
    8.     public interface IInventoryWrapper
    9.     {
    10.         public List<ItemDefinition> GetAllItemsInInventoryCatalog();
    11.  
    12.         public ItemDefinition GetItemDefinition(string key);
    13.  
    14.         public ItemDefinition GetItem(string name);
    15.  
    16.         public List<Item> GetAllItemsInInventory();
    17.     }
    18. }
    And the actual wrapper for GF :

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEditor;
    3. using UnityEngine;
    4. using UnityEngine.GameFoundation;
    5. using UnityEngine.GameFoundation.Components;
    6. using UnityEngine.GameFoundation.Data;
    7. using UnityEngine.GameFoundation.DefaultCatalog;
    8.  
    9. namespace InterActionProject
    10. {
    11.     public class GameFoundationInventoryWrapper : MonoBehaviour, IInventoryWrapper
    12.     {
    13.         #region Public Fields
    14.  
    15.         public bool debugMode = true;
    16.  
    17.         /// <summary>
    18.         /// Reference in the scene to the Game Foundation Init component.
    19.         /// </summary>
    20.         public GameFoundationInit gameFoundationInit;
    21.  
    22.         /// <summary>
    23.         /// status of the Game Foundation
    24.         /// callback sets this to true if ready
    25.         /// </summary>
    26.         public bool gameFoundationIsReady = false;
    27.  
    28.         #endregion Public Fields
    29.  
    30.         #region Unity Methods
    31.  
    32.         private void Start()
    33.         {
    34.         }
    35.  
    36.         #endregion Unity Methods
    37.  
    38.         #region Class Methods
    39.  
    40.  
    41.  
    42.         public List<ItemDefinition> GetAllItemsInInventoryCatalog()
    43.         {
    44.             List<ItemDefinition> itemDefinitionListToReturn = new List<ItemDefinition>();
    45.  
    46.             var catalogAssetPath = "Assets/GameFoundation/GameFoundationCatalog.asset";
    47.             var catalogAsset = AssetDatabase.LoadAssetAtPath<CatalogAsset>(catalogAssetPath);
    48.  
    49.             List<CatalogItemAsset> catalogItems = new List<CatalogItemAsset>();
    50.  
    51.             List<InventoryItemDefinitionAsset> itemDefinitions = new List<InventoryItemDefinitionAsset>();
    52.  
    53.             int numberOfCatalogItems;
    54.  
    55.             numberOfCatalogItems = catalogAsset.GetItems(catalogItems);
    56.  
    57.             Debug.Log($"Number of Catalog Item Assets : {numberOfCatalogItems}");
    58.  
    59.             foreach(CatalogItemAsset cia in catalogItems)
    60.             {
    61.                 Debug.Log(cia.GetType().ToString());
    62.  
    63.                 if(cia.GetType() == typeof(InventoryItemDefinitionAsset))
    64.                 {
    65.  
    66.                     Debug.Log("Found InventoryItemDefinitionAsset");
    67.  
    68.                     InventoryItemDefinitionAsset asset = (InventoryItemDefinitionAsset)cia;
    69.  
    70.                     // Transfering the GameFoundation Inventory Item Definition Asset to the interal
    71.                     // ItemDefinition
    72.  
    73.                     List<PropertyData> propertyData = new List<PropertyData>();
    74.  
    75.                     propertyData = asset.GetDefaultProperties();
    76.  
    77.                     ItemDefinition newItemDefinition = new ItemDefinition();
    78.  
    79.                     newItemDefinition.Key = asset.key;
    80.                     newItemDefinition.Name = asset.displayName;
    81.  
    82.                     propertyData = asset.GetDefaultProperties();
    83.  
    84.                       List<PropertyData> staticPropertiesList = asset.GetStaticProperties();
    85.  
    86.                     Property iconProperty = staticPropertiesList[0].value;
    87.  
    88.                     Texture t = iconProperty.AsAsset<Texture>();
    89.  
    90.                     newItemDefinition.Icon = t;
    91.  
    92.                     itemDefinitionListToReturn.Add(newItemDefinition);
    93.                 }
    94.             }
    95.  
    96.  
    97.  
    98.             return itemDefinitionListToReturn;
    99.         }
    100.  
    101.         public ItemDefinition GetItem(string name)
    102.         {
    103.             ItemDefinition itemDefinitionToReturn = new ItemDefinition();
    104.  
    105.             return itemDefinitionToReturn;
    106.         }
    107.  
    108.         public List<Item> GetAllItemsInInventory()
    109.         {
    110.  
    111.  
    112.             List<Item> itemListToReturn = new List<Item>();
    113.  
    114.             return itemListToReturn;
    115.         }
    116.  
    117.         public ItemDefinition GetItemDefinition(string key)
    118.         {
    119.             ItemDefinition itemDefinitionToReturn = new ItemDefinition();
    120.  
    121.             var catalogAssetPath = "Assets/GameFoundation/GameFoundationCatalog.asset";
    122.             var catalogAsset = AssetDatabase.LoadAssetAtPath<CatalogAsset>(catalogAssetPath);
    123.  
    124.             CatalogItemAsset catalogItemAssetToFind;
    125.  
    126.             catalogItemAssetToFind = catalogAsset.FindItem(key);
    127.  
    128.             if (catalogItemAssetToFind == null)
    129.                 Debug.Log($"Could not find Catalag Item with key '{key}'");
    130.  
    131.  
    132.             InventoryItemDefinitionAsset asset = (InventoryItemDefinitionAsset)catalogItemAssetToFind;
    133.  
    134.             // Transfering the GameFoundation Inventory Item Definition Asset to the interal
    135.             // ItemDefinition
    136.  
    137.             List<PropertyData> propertyData = new List<PropertyData>();
    138.  
    139.             propertyData = asset.GetDefaultProperties();
    140.  
    141.             itemDefinitionToReturn.Key = asset.key;
    142.             itemDefinitionToReturn.Name = asset.displayName;
    143.  
    144.             List<PropertyData> staticPropertiesList = asset.GetStaticProperties();
    145.  
    146.             Property iconProperty = staticPropertiesList[0].value;
    147.  
    148.             Texture t = iconProperty.AsAsset<Texture>();
    149.  
    150.             itemDefinitionToReturn.Icon = t;
    151.  
    152.             //Debug.Log(iconProperty.type);
    153.  
    154.             return itemDefinitionToReturn;
    155.         }
    156.  
    157.         public void GameFoundationIsReadyCallback()
    158.         {
    159.             gameFoundationIsReady = true;
    160.  
    161.             dbg("Game Foundation is Ready Callback ! : " + gameFoundationIsReady.ToString());
    162.  
    163.             //ICollection<InventoryItemDefinition> target;
    164.  
    165.             //List<InventoryItemDefinition> itemDefinitions = new List<InventoryItemDefinition>();
    166.  
    167.             //int numberOfItemDefinitions = GameFoundationSdk.catalog.GetItems<InventoryItemDefinition>(itemDefinitions);
    168.         }
    169.  
    170.         protected void dbg(string message, bool error = false)
    171.         {
    172.             if (debugMode & !error)
    173.                 Debug.Log("[ " + this.GetType().Name + " (" + Time.time + ")] " + message);
    174.  
    175.             if (error)
    176.                 Debug.LogError("<color=\"red\">[" + this.GetType().Name + " (" + Time.time + ")] " + message + "</color>");
    177.         }
    178.  
    179.         #endregion Class Methods
    180.     }
    181. }
     

    Attached Files:

    erika_d and tony_c-unity3d like this.
unityunity