question

Jacob van der Vliet avatar image
Jacob van der Vliet asked

Why does UpdateUserInventoryItemCustomData add all of it's field to item custom data??

In cloudscript I am trying to fill in an items custom data with the custom data from the catalog item.

When I run the server.UpdateUserInventoryItemCustomData from cloud script it adds the PlayFabId, ItemInstanceId and Data field as individual entries in the custom data, and does not work if those fields are not there? Is this intended behavior? It seems wonky.

Result: https://i.imgur.com/8eYS5vH.png

Cloudscript:

server.UpdateUserInventoryItemCustomData({
                PlayFabId: currentPlayerId,
                ItemInstanceId: instanceId,
                Data: item.CustomData
            });

CloudScriptdata
1 comment
10 |1200

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

Jacob van der Vliet avatar image Jacob van der Vliet commented ·

This is the full function for context. It is called with the player_inventory_item_added rule.

handlers.newItemAdded = function(args, context) {
    let response = server.GetCatalogItems({
        CatalogVersion: "Main"
    });

    let catalog = response["Catalog"];

    let eventData = context.playStreamEvent;
    let addedId = eventData.ItemId;
    let instanceId = eventData.InstanceId;

    for (let itmIdx in catalog) {
        let item = catalog[itmIdx];

        if (item.ItemId == addedId && item.CustomData) {

            log.debug("Custom Item Data: " + item.CustomData);

            var req = {
                PlayFabId: currentPlayerId,
                ItemInstanceId: instanceId,
                Data: item.CustomData
            };
            server.UpdateUserInventoryItemCustomData(req);

            return;
        }
    }
};
0 Likes 0 ·
Jacob van der Vliet avatar image
Jacob van der Vliet answered

Well I feel silly, the item.CustomData was a string not a json object!

This is still some very strange behavior! I would expect this to throw the "data must contain at least one key/value" error, not insert my PlayFabId and ItemInstanceId as well as data.

10 |1200

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

Seth Du avatar image
Seth Du answered

It is not the intended behavior, the problem can be the format of your json variable “item” is incorrect. I have written a hard-coded test example, please see the difference in your code:

handlers.test =
  function (context) {
    var item ={ 
      "name":"testitem", 
      "CustomData": { 
        "AttackSpeed":1, 
        "Damage": 40 
      }
  };

  server.UpdateUserInventoryItemCustomData({
    PlayFabId: "xxxxxxxxx",
    ItemInstanceId: "xxxxxxxx",
    Data: item.CustomData
    }
  );
}

The test result is:

In addition, from your code:

handlers.newItemAdded = function(args, context) {
    let response = server.GetCatalogItems({
        CatalogVersion: "Main"
    });

    let catalog = response["Catalog"];

    let eventData = context.playStreamEvent;
    let addedId = eventData.ItemId;
    let instanceId = eventData.InstanceId;

    for (let itmIdx in catalog) {
        let item = catalog[itmIdx];

        if (item.ItemId == addedId && item.CustomData) {

            log.debug("Custom Item Data: " + item.CustomData);


	    // Please see the changes below:


            var req = {
                PlayFabId: currentPlayerId,
                ItemInstanceId: instanceId,
                Data: JSON.parse(item.CustomData)
            };
            server.UpdateUserInventoryItemCustomData(req);

            return;
        }
    }
};

The CustomData retrieved from other callbacks should be a string, you need to parse it into an Object before you use it.

Be aware that please enable "Publish results as PlayStream Event", so that you can see the result in the PlayStream monitor.


10 |1200

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

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.