How do I deserialize an ExecuteCloudScriptResult.FunctionResult with a nested Dictionary<string, string>?
Here is the CloudScript generating the result:
// Equip an item to the player // Args: // itemInstanceId string The itemInstanceId of the item to equip // slot (optional) string Which slot to equip to. Either "Left Hand", "Right Hand". Only used by 1H weapons, other items use their default slot while 2H weapons equip to both hand slots. handlers.EquipItem = function(args) { var inventory = GetPlayerInventory(currentPlayerId); var item = FindItemByInstanceId(inventory, args.itemInstanceId); var data = GetPlayerReadOnlyData(currentPlayerId, ["Equipment"]); var equipment = JSON.parse(data["Equipment"].Value); if(!EquipValid(item, args.slot)) { // Method checks if the item and slot are valid log.debug("Equip is not valid for this object"); return { Success: false, Result: equipment // equipment is returned without modification, so even in the case of a failure the player gets a result }; } else { // Modify the equipment object data["Equipment"] = JSON.stringify(equipment); UpdatePlayerReadOnlyData(currentPlayerId, data); // Method to apply the data object back to the player's ReadOnlyData return { Success: true, Result: equipment }; } }
Here's a sample result:
{ "Success":true, "Result": { "Head":"", "Neck":"", "Shoulders":"", "Arms":"", "Hands":"", "Torso":"", "Chest":"", "Back":"", "Waist":"", "Legs":"", "Thighs":"", "Feet":"2037797F861EA380", "Right Hand":"", "Left Hand":"1DEEFACB07A59DA9" } }
Each Key in Result is the slot, while the Value is the ItemInstanceID of the item. Here the action is a success and we see that the Feet slot and Left Hand slot contain items. Likely I'll also include more information about the equip or more error logging.
My player's Equipment (client-side, C#) is a Dictionary<string, string>. I can then use the slot's Value to look up an item from the player's loaded Inventory (List<ItemInstance>) or from the catalog (List<CatalogItem>).
I've tried this, which works except for the nested dictionary:
private class EquipItemFunctionResult { public bool Success; public Dictionary<string, string> Equipment; } public void EquipItem(string itemInstanceId, string itemSlot = "") { var request = new ExecuteCloudScriptRequest() { FunctionName = "EquipItem", FunctionParameter = new { itemInstanceId = itemInstanceId, slot = itemSlot } }; System.Action<ExecuteCloudScriptResult> resultCallback = delegate(ExecuteCloudScriptResult obj) { var parsedResult = JsonUtility.FromJson<EquipItemFunctionResult>(obj.FunctionResult.ToString()); Debug.Log(parsedResult.Success); Debug.Log(parsedResult.Equipment); }; System.Action<PlayFabError> errorCallback = delegate(PlayFabError obj) { Toolbox.DialogWindow.ShowPlayFabError(obj); }; PlayFabClientAPI.ExecuteCloudScript(request, resultCallback, errorCallback); }
For a valid equip, this results in
True Null