Search Unity

Question Unity Economy Inventory Item custom JSON Data

Discussion in 'Unity Gaming Services General Discussion' started by VagelisGardikis, Feb 10, 2022.

  1. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    Hello. When i Add a new inventory item from the Dashboard, how can i put in the item's instance fata dictionary
    the value from the custom JSON data i wrote in the item configuration at the dashboard?
     
  2. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi There,

    Thanks for posting your question. I'm not 100% sure I understand the question, so please provide more details if I am jumping to the wrong conclusions!

    If you are asking how to define default keys and values in a new inventory item's Instance Data, you can type them into the Custom Data panel as you create the inventoty item definition. You can also edit them, save and publish your changes at a later date.

    upload_2022-2-10_17-0-34.png

    At runtime, you can acess the instance data for individual instances of items in a player's inventory from the the Economy package or through Cloud Code

    e.g.
    from Economy
    Code (CSharp):
    1. Dictionary<string, object> instanceData = new Dictionary<string, object>
    2. {
    3.     { "rarity", "purple" }
    4. };
    5.  
    6. PlayerInventory.AddInventoryItemOptions options = new PlayerInventory.AddInventoryItemOptions
    7. {
    8.     PlayersInventoryItemId = "customID",
    9.     InstanceData = instanceData
    10. };
    11.  
    12. PlayersInventoryItem createdInventoryItem = await Economy.PlayerInventory.AddInventoryItemAsync("SWORD", options);
    from Cloud Code
    Code (CSharp):
    1.   const createdItem = await inventory.addInventoryItem(projectId, playerId,
    2.     { inventoryItemId:"TICKET",
    3.       instanceData: { "itemValue":itemValue,
    4.                       "itemExpiresAt":itemExpiresAt }
    5.     });
    I hope that helps, but please let me know if I've missed the point.
     
    Last edited: Feb 11, 2022
  3. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    Hello. Yes, lets say i add a key and a value to the custom data on the dashboard.
    When i Add an item, i want this :

    Code (CSharp):
    1. Dictionary<string, object> instanceData = new Dictionary<string, object>
    2. {
    3.     { "rarity", "purple" }
    4. };
    To get the values i added to the custom Data on the dashboard.
    So when i Add the item, it automatically has in its dictionary the 'expires at' value at your example.