Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved Inventory confusion on the docs

Discussion in 'Economy' started by Corva-Nocta, Nov 9, 2022.

  1. Corva-Nocta


    Feb 7, 2013
    Hi all,
    I'm trying to follow the code on the docs but having a little trouble figuring out how its implemented. I'm on this page of the docs: and just trying the first line where it gets the players current inventory. This is the code I am trying to use:

    Code (csharp):
    1. GetInventoryOptions options = new GetInventoryOptions
    2. {
    3.     ItemsPerFetch = 5
    4. };
    5. GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);
    6. List<PlayersInventoryItem> firstFiveItems = inventoryResult.PlayersInventoryItems;
    7. // do something with your items
    8. if (inventoryResult.HasNext) {
    9.     inventoryResult = await inventoryResult.GetNextAsync(5);
    10.     List<PlayersInventoryItem> nextFiveItems = inventoryResult.PlayersInventoryItems;
    11.     // do something with your items
    12. }
    I am not really sure how this code is supposed to be used. Its not a function, and not a definition, so how do I make it work? Its not cloud code right? Just a little stuck at how this code from the documentation is actually used.

    I have also found this page: which seems to be going over a similar idea, but with slightly different code. The code I am looking at for this is:

    Code (csharp):
    1. List<InventoryItemDefinition> definitions = await EconomyService.Instance.Configuration.GetInventoryItemsAsync();
    but I see that it takes a InventoryItemDefinition, which I can't find anywhere how to set that up. How do I make a currency definition? Is that something I do on the dashboard? or somewhere else?

    Any help at all would be great!
    Last edited: Nov 9, 2022
  2. Corva-Nocta


    Feb 7, 2013
    Ok so I am much closer that I was before, I looked at the sameples projects and got this code to work just fine:

    Code (csharp):
    1. public async Task RefreshInventory()
    2.     {
    3.         GetInventoryResult inventoryResult = null;
    4.         try
    5.         {
    6.             inventoryResult = await GetEconomyPlayerInventory();
    7.             Debug.Log("Got Inventory!");
    8.             Debug.Log(inventoryResult);
    9.         }
    10.         catch (EconomyRateLimitedException e)
    11.         {
    12.             //inventoryResult = await Utils.RetryEconomyFunction(GetEconomyPlayerInventory, e.RetryAfter);
    13.         }
    14.         catch (Exception e)
    15.         {
    16.             Debug.Log("Problem getting Economy inventory items:");
    17.             Debug.LogException(e);
    18.         }
    19.         if (this == null)
    20.             return;
    21.         //inventoryHudView.Refresh(inventoryResult.PlayersInventoryItems);
    22.     }
    23.     static Task<GetInventoryResult> GetEconomyPlayerInventory()
    24.     {
    25.         var options = new GetInventoryOptions { ItemsPerFetch = 10 };
    26.         return EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);
    27.     }
    So I can check on the player's inventory when they log in. A great start! Now I am just having the trouble of adding the items... How do I actually add an item to the inventory?
    Last edited: Nov 9, 2022
  3. Laurie-Unity


    Unity Technologies

    Mar 5, 2020
    Hi @Corva-Nocta,

    Sorry to see you struggling a bit with Economy.

    One important thing to note about your Inventory (also applies to currencies) is that there are seperate calls to retrieve the:
    • Configuration : The definition of inventory items in the game along with any custom data properties for each
    • Player Inventory : The actual inventory items that this player holds, including any custom instance data
    So based on that, you can grab the configuration of all inventory items in the game with

    Code (CSharp):
    1. // Inventory Item Definitions
    2. List<InventoryItemDefinition> definitions = await EconomyService.Instance.Configuration.GetInventoryItemsAsync();
    You would typically do this at the start of the game after ensuring that UGS has been initialized and the player has been signed in.

    Then grab all the inventory items that the player holds with
    when ever you need to check or cache all of their current inventory
    Code (CSharp):
    1. // Optional, defaults to 20
    2. GetInventoryOptions options = new GetInventoryOptions
    3. {
    4.     ItemsPerFetch = 5
    5. };
    7. GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);
    8. List<PlayersInventoryItem> firstFiveItems = inventoryResult.PlayersInventoryItems;
    9. // do something with your items
    11. if (inventoryResult.HasNext) {
    12.     inventoryResult = await inventoryResult.GetNextAsync(5);
    13.     List<PlayersInventoryItem> nextFiveItems = inventoryResult.PlayersInventoryItems;
    14.     // do something with your items
    15. }
    But keep in mind that you can check the player's inventory for specific items by specifying the item ID or IDs that you are interested in.

    Code (CSharp):
    1. GetInventoryOptions options = new GetInventoryOptions
    2. {
    3.     InventoryItemIds = new List<string>() { "SWORD" }
    4. };
    6. GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);
    8. List<PlayersInventoryItem> listOfItems = inventoryResult.PlayersInventoryItems;

    I hope that helps
  4. Corva-Nocta


    Feb 7, 2013
    I did manage to get that code working, actually pretty excited about that! Its not much, but every little bit of progress helps haha.

    The only problem I am having is that this code, and the docs for this code, seem to just be about how to get the references from Economy, but not how to actually or change anything in Economy. It'll be very handy once I can start toying with the actual inventory though!

    I did look a bit further to that code that does talk about adding items. Previously when I tried it the code was throwing errors, but this time it seems to not be throwing the errors! So I think I am making progress haha. I will let you know if I can get an item added properly.

    also sorry I have two posts talking about essentially the same thing :(
    Last edited: Nov 11, 2022
  5. Corva-Nocta


    Feb 7, 2013
    IT DID!!! I finally got it to work!!! Thanks so much! Dunno what the magic was that was needed, but I managed to get an item added to a player's inventory :D Now I get to do all the hard parts of doing stuff with it in game haha.

    Thanks for the help!
    Laurie-Unity likes this.