question

Yichen Wang avatar image
Yichen Wang asked

How can I retrieve and change the custom data of an item? Please give an example.

unity3d
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

Do you mean the custom data of catalog Item or the custom data of inventory item? For the clarification, these two types of custom data are not the same. Catalog Items’ custom data means the common attributes of virtual goods, it can be configured using Admin API SetCatalogItems, and can be retrieved through Admin/Server/Client API GetCatalogItems. Inventory items’ custom data means the specific item’s data, it can be retrieved via Admin/Server/Client API GetUserInventory, and can be set using Server API UpdateUserInventoryItemCustomData. Besides, they can both be set on the Game Manager.

You can consider using the external API testing tool Postman to try out PlayFab REST API, our PlayFab Postman Collection contains the example code for all PlayFab API, please check the quick start for more info. For security reasons, we only suggest using Admin API and Server API in the administrative tools and servers. If you use Unity to develop the client, we would suggest you retrieve the data and modify it on the CloudScript or Azure Functions.

If you want to ask the exact example, please provide us with more details, such as development platform and specific API, thanks.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Yichen Wang avatar image Yichen Wang commented ·

Thank you very much for the clarification, but can you give me an example of the GetUserInventory API to retrieve custom data of an inventory item through server (cloudscript) and Client API please? I have already figured out how to use the server API UpdateUserInventoryItemCustomData to set the custom data by granting an item and setting the custom data of it (also please tell me if I am doing something wrong). Sorry for asking a lot, thank you very much, and have a great day!

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Yichen Wang commented ·

CloudScript

handlers.retreiveInventoryCustomData = function (args, context) {
    var request = {
        //replace currentPlayerId with your playerID
        PlayFabId: currentPlayerId
    };
    var result = server.GetUserInventory(request);
    var itemObj = result.Inventory.find(function (obj) {
        //replace "LaserSword" with your itemId
        return obj.ItemId === "LaserSword"
    });
    var customData = itemObj.CustomData;
    //replace with your key
    var testValue = customData.TestKey;
    return { testValue: testValue };
};

Unity

  private void OnLoginSuccess(LoginResult result)
    {
        var request = new GetUserInventoryRequest() { };
        PlayFabClientAPI.GetUserInventory(request, OnGetSuccess, OnFailure);
    }
    private void OnGetSuccess(GetUserInventoryResult result)
    {
        var InventoryList = result.Inventory;
        var item = InventoryList.Find(isMyKey);
        var customData = item.CustomData;
        string myvalue;
        if (customData.TryGetValue("TestKey", out myvalue))
        {
            Debug.Log(myvalue);
        }
    }


    private bool isMyKey(ItemInstance item)
    {
        return item.ItemId == "LaserSword";
    }
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.