question

Dylan Hunt avatar image
Dylan Hunt asked

How to deserialize a cloudscript server call back to the client?

So I just used a cloudscript call to server.GrantItemsToUser()

This returns an array of GrantedItemInstance.

If I return this to the client, can I deserialize this into an list of ItemInstance ? It's super awkward that it's specifically GrantedItemInstance -- I'm going to guess it's different and it'll crash if I try to deserialize, but wanted to post and make sure!

For example:

// CS
[...] var grantItemsToUserRes = server.GrantItemsToUser(req);
return grantItemsToUserRes; ______________________________________________ // C#
[...] (result) {
List<ItemInstance> infoResult = PlayFab.Json.JsonWrapper.
DeserializeObject<List<ItemInstance>>
(result.FunctionResult.ToString());

Will this work?

What's recommended in this scenario? I'm granting a starter pack if the items aren't found in their inventory. I'll do cloudscript to grant 2-3 starter items and return the items granted to update the local session.

As an alt., I suppose I could just return a bool if it worked or not since I know the items being granted, but it'd be nice to recycle the function for other uses, if it could return the granted items.

As a 2nd alt., I could probably just get the inventory after and return the entire inventory, although it's less efficient. I could just replace the entire inventory for the local session.

CloudScriptPlayer Inventory
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.

Dylan Hunt avatar image Dylan Hunt commented ·

For now, I went with 2nd alt, but still interested in best way to do this :)

// CS
[...]
var grantItemsToUserRes = server.GrantItemsToUser(req); var myNewInv = server.GetUserInventory(currentPlayerId); return myNewInv;
______________________________________________
// C#
[...] (result) {
GetUserInventoryResult infoResult = PlayFab.Json.JsonWrapper. DeserializeObject<GetUserInventoryResult>
(result.FunctionResult.ToString());
// Then update inventory of local session
0 Likes 0 ·
brendan avatar image
brendan answered

GrantedItemInstance derives from ItemInstance, but has additional information (on the player/character the item was granted to, for a start). So, you could read the ItemInstance elements from it, or just go with your plan to re-read the inventory. Either way would work fine.

10 |1200

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

haoshi avatar image
haoshi answered

for anyone having this problem, here's my solution:

var itemGrantResults = JsonLogger.GetJsonObject<Dictionary<string, List<ItemInstance>>>(result.FunctionResult);

if (itemGrantResults != null)
{
     foreach (ItemInstance item in itemGrantResults["ItemGrantResults"])
     {
          Inventory.AddItem(item);
     }
}

                
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.