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 });
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; } } };
Answer by Jacob van der Vliet · Jun 06, 2019 at 12:22 PM
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.
Answer by SethDu · Jun 06, 2019 at 08:19 AM
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.
server.GetPlayersInSegment returns successfully, but *Empty* function result 1 Answer
server.AddSharedGroupMember returns Invalid Shared Group ID 1 Answer
Don't know how to send values to azure function via cloudscript with Unity 1 Answer
JSON Field Reformatting on Automation Save 2 Answers
Get players in segment count Via cloud script and update title data 1 Answer