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 Adding to inventory isn't working now? (409 Errors)

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

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all,
    I am working on getting the player inventory working, I've gotten it working before and managed to add items to a player's inventory, but now it seems to not be working, and I'm not sure why. I am trying to add a few new items to list of items but that's about it. I haven't changed my code (at first) and it didn't do anything, but I've been monkeying with my code more and it doesn't seem to work. I started a new blank scene to try and locate the problem, but I can't seem to find it.

    I got Authenticate to work, then I can get my inventory, I can even try to get individual items, and those all work fine following the tutorials. I am trying to follow the tutorial for adding an item, the same tutorial I followed before, which is just this one: https://docs.unity.com/economy/SDK-player-inventory.html#AddInventoryItemAsync but it doesn't work, and I'm not sure why.

    Code (csharp):
    1. async Task AddTin()
    2.     {
    3.         AddInventoryItemOptions options = new AddInventoryItemOptions
    4.         {
    5.             PlayersInventoryItemId = "customID",
    6.             InstanceData = null
    7.         };
    8.         PlayersInventoryItem createdInventoryItem = await EconomyService.Instance.PlayerInventory.AddInventoryItemAsync("TIN_ORE", options);
    9.     }
    This is the code I am working with that for some reason doesn't work. I tried a few different iterations on the code, but I'm not sure which part is wrong. Even tried doing this:

    Code (csharp):
    1.  
    2. async Task AddTin()
    3.     {
    4.         Dictionary<string, object> instanceData = new Dictionary<string, object>
    5.         {
    6.             { "rarity", "purple" }
    7.         };
    8.  
    9.         AddInventoryItemOptions options = new AddInventoryItemOptions
    10.         {
    11.             PlayersInventoryItemId = "customID",
    12.             InstanceData = instanceData
    13.         };
    14.         PlayersInventoryItem createdInventoryItem = await EconomyService.Instance.PlayerInventory.AddInventoryItemAsync("TIN_ORE" options);
    15.     }
    and various other things. Anyway I keep getting this error message and I'm hoping it can help to fine the problem:

    EconomyException: HTTP/1.1 409 Conflict
    UnityEngine.Debug:LogError (object)
    Unity.Services.Economy.EconomyAPIErrorHandler:HandleException (Unity.Services.Economy.Internal.Http.HttpException) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Exceptions/EconomyAPIErrorHandler.cs:115)

    and I also get this error:

    HttpException`1: (409) HTTP/1.1 409 Conflict
    Unity.Services.Economy.Internal.Http.ResponseHandler.HandleAsyncResponse (Unity.Services.Economy.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/ResponseHandler.cs:120)

    So any ideas? It seems to be some kind of response error, but I'm not sure why it would have that kind of error.

    From searching it sounds like its an error with two functions trying to change the same information, which doesn't make much sense. I've only got one function working at a time. I am just very lost right now.
     
    Last edited: Nov 28, 2022
  2. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Alright so I tried a brand new project, brand new economy set up, brand new unity account, everything new and basic. Created a new project, new scene, and only added a single script that first does authenticate, then if that's successful (which it has been working) add an item to the player's Economy Inventory. Then tries to pull that item from the Economy Inventory to make sure its all working. Even set the item to just be a SWORD like in the tutorials.

    Just like with my real project, it works the first time and I can add an item to the inventory, but if I try to do it a second time I get a 409 error.

    Oddly, when I try adding a different item (a shield) it does not add. When I go back to my dashboard and look at my player, I only find the Sword item in the inventory. Trying to add a shield item gives me a 409 error.

    So I'm beginning to think it isn't adding the item that is giving a 409 error but something else is. Looking up 409 errors it sounds like it refers to having two different processes trying to access or change the same information, but I only have the one script in the project and only have the one process. So there shouldn't be anything else that is interacting with it at all. I'm not really sure what the problem is, does anyone know how to fix a 409 error?
     
  3. WilliamBraun

    WilliamBraun

    Joined:
    Nov 17, 2022
    Posts:
    5
    I think I found the error! So for anyone else trying to solve the 409 error I believe I have found the answer. (Also if any mods want to add "409 Error" to the title of this post that might help others who are looking)

    The problem is the line:
    Code (csharp):
    1. PlayersInventoryItemId = "customID",
    The economy system does not allow you to have two copies of an item with the same customID value, or as its called in the dashboard the Players Inventory Item ID. So this value needs to be unique for EVERY item that you are adding to your inventory. (Its also the value that will be used when deleting items from inventory, so make sure your naming system is simple!) Not a hard problem to solve, just something new to be aware of.

    (In case anyone was wondering, it was actually the examples in the package download that gave me the answer! Those things are HANDY!)
     
    Last edited: Nov 28, 2022
  4. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi @Corva-Nocta,

    I can see what is going here, @WilliamBraun is absolutely correct, let me explain.

    In your code you are trying to add an inventory item ID = TIN_ORE to the player's inventory. The question you need to ask yourself is whether the player can only ever hold one unique piece of TIN_ORE, or potentially could they hold numerous. This is important because every item instance in the player's inventory needs to have a unique ID.

    So, either
    • Provide your own genuinely unique ID for the PlayersInventoryItemId to ensure the player can only ever hold one of this object
    • or, leave it null and allow the AddInventoryItemAsync to autogenerate a unique value for you.
    In your case, TIN_ORE is not a unique item, but a resource that the player could hold numerous instances of, so you should leave the
    PlayersInventoryId = null
    and let the Economy system autogenerate one for you and return it in the
    createdInventoryItem



    e.g.
    Code (CSharp):
    1. Dictionary<string, object> instanceData = new Dictionary<string, object>
    2. {
    3.     { "rarity", "purple" }
    4. };
    5.  
    6. AddInventoryItemOptions options = new AddInventoryItemOptions
    7. {
    8.     PlayersInventoryItemId = null
    9.     InstanceData = instanceData
    10. };
    11.  
    12. PlayersInventoryItem createdInventoryItem = await EconomyService.Instance.PlayerInventory.AddInventoryItemAsync("SWORD", options);

    I hope that helps to clarify what was occurring.