问题描述
我试图从特定玩家那里获取一些变量,然后将它们存储在数组中以操作和显示它们,将键放在数组上没有任何问题,但是在放置值的那一刻为获取用户数据返回的字典它不允许我将它们存储在数组中
显示Cannot convert from 'string[]' to ' PlayFabClientModels.UserDataRecord[]'
private String[] qDataKeys;
private String[] qDataValues;
void GetQuestionsData()
{
PlayFabClientAPI.GetUserData(new GetUserDataRequest()
{
PlayFabId = "someID",Keys = null
},OnQuestionsDataReceived,OnError
);
}
void OnQuestionsDataReceived(GetUserDataResult result)
{
result.Data.Keys.CopyTo(qDataKeys,0);
result.Data.Values.CopyTo(qDataValues,0);// This is the one that gives the error,above one is okay
}
解决方法
问题在错误中非常清楚。此调用返回的类型为 PlayFabClientModels.UserDataRecord[]
类型。检查 docs,UserDataRecord 类型设置为
Ordered by:
Data name
Data type
Data Description
LastUpdated
string
Timestamp for when this data was last updated.
Permission
UserDataPermission
Indicates whether this data can be read by all users (public) or only the user (private). This is used for GetUserData requests being made by one player about another player.
Value
string
Data stored for the specified user data key.
我假设您想要记录中的 Value
。我不确定您目前如何访问 Key
,因为文档指定 GetUserDataResult
返回 Data
类型为 UserDataRecord
和 DataVersion
number
类型。
错误当前告诉您您正在使用的 copyTo
失败,因为当前类型是 PlayFabClientModels.UserDataRecord[]
而您期望的是 string[]
。您需要遍历每个 UserDataRecord
并从每个索引中获取 Value
数据。
我相信一些东西
string[] yourData = result.Data.Value.Select(x => x.Value).ToArray();
应该可以。您需要在文件顶部包含 using System.Linq
。如果它不起作用,我只需要知道 result.Data.Values
的类型,但由于错误,我假设它是一个无数的数据数组。我不熟悉 PlayFab,因此如果当前文档已过时,请在评论中纠正我,我会尝试更新答案。