Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question How can I grab an Inventory Item Custom Data using Cloud Code?

Discussion in 'Cloud Code' started by Malhmoud, Mar 9, 2024.

  1. Malhmoud

    Malhmoud

    Joined:
    May 6, 2013
    Posts:
    3
    Hello everyone,

    As per the documentation here: Cloud Code Services SDKs (unity3d.com)

    I cannot find a method to get a specific item custom data, neither its returned in the value of returned items when running the "getPlayerInventory" method.

    The only way I can it possible right now is still using the the C# function "Unity.Services.Economy.Model.PlayersInventoryItem.GetItemDefinition()" as it returns an InventoryItemDefinition which has the custom Data.

    In other words, can I get the value from Cloud Code directly or there is no method that exposes this value at the current moment?

    upload_2024-3-9_23-47-51.png

    Screenshot attached for an explanation of what valuie is needed.
     
  2. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    50
    Hi @Malhmoud,

    As the economy inventory API doesn't return the custom data as part of the response https://services.docs.unity.com/economy/v2/#tag/Inventory/operation/getPlayerInventory, I've taken a look at the GetItemDefinition method and essentially the method is a helper function to retrieve the configuration of the inventory item.

    Unfortunately a similar helper method doesn't currently exist in Cloud Code but I believe you would be able to fetch what you need via the configuration API (see https://cloud-code-sdk-documentatio.../v2.4/configurationapi#getPlayerConfiguration) by looking up the inventory's custom data in the response using the inventory item ID.
     
    GabKBelmonte likes this.
  3. Malhmoud

    Malhmoud

    Joined:
    May 6, 2013
    Posts:
    3
    Thank you @samg-unity, can you please give me an example on how to use the API? I am having a hard time sending creating a ConfigurationApiGetPlayerConfigurationRequest
     
  4. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    50
    No problem @Malhmoud

    I set up some basic economy configuration with a Sword item and custom data;

    upload_2024-3-18_10-21-57.png


    Then I've put together the following script which I believe gets the result you're after,

    (Note: the configAssignmentHash is entirely optional but I wanted to demonstrate how you can you can pass that value if you want to make use of overrides https://docs.unity.com/ugs/en-us/ma...des-CC#Economy_Game_Overrides_with_Cloud_Code)

    Code (JavaScript):
    1. const { InventoryApi, ConfigurationApi } = require("@unity-services/economy-2.4");
    2.  
    3. const swordId = "SWORD";
    4.  
    5. module.exports = async ({ params, context, logger }) => {
    6.  
    7.   const {playerId, projectId} = context;
    8.   const {configAssignmentHash} = params;
    9.  
    10.   const configApi = new ConfigurationApi(context);
    11.   const inventoryApi = new InventoryApi(context);
    12.  
    13.   const getPlayerConfigRequest = {
    14.     configAssignmentHash,
    15.     playerId,
    16.     projectId,
    17.   }
    18.  
    19.   const getPlayerInventoryRequest = {
    20.     configAssignmentHash,
    21.     playerId,
    22.     projectId,
    23.   }
    24.  
    25.   try {
    26.  
    27.     // TEST SETUP: Used to create initial player inventory
    28.     // await inventoryApi.addInventoryItem({
    29.     //   addInventoryRequest: {
    30.     //     inventoryItemId : swordId,
    31.     //     instanceData: {
    32.     //       damage: 80,
    33.     //     }
    34.     //   },
    35.     //   playerId,
    36.     //   projectId
    37.     // });
    38.  
    39.     var config = await configApi.getPlayerConfiguration(getPlayerConfigRequest);  
    40.  
    41.     const inventoryConfig = config.data.results.filter((configItem) => {
    42.         return configItem.type === "INVENTORY_ITEM";
    43.     })
    44.  
    45.     const result = await inventoryApi.getPlayerInventory(getPlayerInventoryRequest);
    46.  
    47.     const itemsWithCustomData = result.data.results.map((inventoryItem) => {
    48.       const inventoryItemConfig = inventoryConfig.find((configItem) => {
    49.         return configItem.id === inventoryItem.inventoryItemId;
    50.       })
    51.       inventoryItem.customData = inventoryItemConfig.customData;
    52.       return inventoryItem;
    53.     });
    54.  
    55.     return itemsWithCustomData;
    56.     // [{
    57.     //   "created": {
    58.     //     "date": "2024-03-18T10:05:07Z"
    59.     //   },
    60.     //   "customData": {
    61.     //     "unique": true
    62.     //   },
    63.     //   "instanceData": {
    64.     //     "damage": 80
    65.     //   },
    66.     //   "inventoryItemId": "SWORD",
    67.     //   "modified": {
    68.     //     "date": "2024-03-18T10:05:07Z"
    69.     //   },
    70.     //   "playersInventoryItemId": "d4339b75-8168-4529-60ad-d343607e0d6b",
    71.     //   "writeLock": "1"
    72.     // }]
    73.  
    74.   } catch (err) {
    75.     logger.error("Error while retrieving inventory items", {"error.message": err.message});
    76.     throw err;
    77.   }
    78. };
     
    Malhmoud likes this.