我正在 WPF 下开发一个 C# .Net Core 项目,并与 PlayFab 链接,更具体地说,与 CloudScript 修订版链接。以下代码是 WPF 中 C# 类中的一个函数,该函数在调用 CloudScript 函数后运行。CloudScript 函数购买物品并将其提供给玩家。当函数没有返回错误时,我的问题适用。
这是代码:
/// <summary>
/// PlayFab check query
/// </summary>
/// <param name="result"></param>
private void GetCloudScriptPurchase(ExecuteCloudScriptResult result)
{
JsonObject jsonResult = (JsonObject)result.FunctionResult;
try
{
// This is where I get responseContent
jsonResult.TryGetValue("responseContent", out object responseContent);
shopDataContext.PlFabInfo = (string)responseContent;
// Here, this is what would suit me better, I avoid an API call just by modifying
// the variable with the return of the CloudScript function
jsonResult.TryGetValue("messageValue", out object messageValue);
this.getUserInvent = (PlayFabResult<GetUserInventoryResult>)messageValue;
this.ClientVirtualCurrency(
getUserInvent.Result.VirtualCurrency["BC"]
);
// Here I update the local variable by making another API call,
// which I want to avoid
this.getUserInvent = PlayFabClientAPI.GetUserInventoryAsync(new GetUserInventoryRequest()).Result;
this.ClientVirtualCurrency(
PlayFabClientAPI.GetUserInventoryAsync(
new GetUserInventoryRequest()).Result.Result.VirtualCurrency["BC"]
);
}
catch (Exception Exception)
{
throw Exception;
}
}
这个shopDataContext是一个特定于实际页面的 Binding,这里它只是显示 CloudScript 函数的返回。此ClientVirtualCurrency函数计算在客户端上无错误显示的玩家金额。它需要一个 int 参数。
CloudScript 的返回和代码的返回:
return { responseContent: messageReturn, messageValue: server.GetUserInventory({PlayFabId: currentPlayerId}) }
"FunctionResult": {
"responseContent": "The skin deuxiopack is now yours!",
"messageValue": {
"PlayFabId": "862C4ED0EE57D60F",
"Inventory": [
{
"ItemId": "deuxiopack",
"ItemInstanceId": "63A13435128DA937",
"ItemClass": "Pack",
"PurchaseDate": "2021-06-15T15:10:54.87Z",
"CatalogVersion": "Skin",
"DisplayName": "DeuxioPack",
"UnitPrice": 0
},
{
"ItemId": "trexiopack",
"ItemInstanceId": "504C450155074940",
"ItemClass": "Pack",
"PurchaseDate": "2021-06-11T14:37:18.772Z",
"CatalogVersion": "Skin",
"DisplayName": "TrexioPack",
"UnitPrice": 0
},
{
"ItemId": "starterpack",
"ItemInstanceId": "C191AB8C4A6717C6",
"ItemClass": "Pack",
"PurchaseDate": "2021-06-11T14:29:31.955Z",
"Annotation": "Granted via Game Manager",
"CatalogVersion": "Skin",
"DisplayName": "StarterPack",
"UnitPrice": 0
}
],
"VirtualCurrency": {
"BC": 200
},
"VirtualCurrencyRechargeTimes": {}
}
}
CloudScript 函数返回一个responseContent对象,该对象是要在客户端上显示的成功或错误消息(在我们的例子中是成功消息)的字符串。此函数还返回一个messageValue对象,它是一个PlayFab.Json.JsonObject。
当我尝试时,此错误显示在捕获中:System.InvalidCastException: Unable to cast object of type 'PlayFab.Json.JsonObject' to type 'PlayFab.PlayFabResult`。因此,我无法将PlayFab.Json.JsonObject类型更改为PlayFab.PlayFabResult,以便能够在没有新 API 调用的情况下更新我的本地变量getUserInvent 。
如果有人可以帮助我或给我提示,请提前致谢。