How Can I convert PlayFab's UserDataRecord to String array

413 Views Asked by At

I was trying to get some variables from an specific player, and then store them in arrays to manipulate and display them, I don't have any problems putting the keys on the array, but at the moment of putting the values of the dictionary that returns for the get user data it doesn't let me store them in the array

It displays 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
}
1

There are 1 best solutions below

10
TEEBQNE On BEST ANSWER

The issue is pretty clear in the error. The type returned from this call is of type PlayFabClientModels.UserDataRecord[]. Checking out the docs, the type UserDataRecord is setup as

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.

I am assuming you would like the Value inside of the record. I am not sure how you are currently accessing the Key either as the docs specify that the GetUserDataResult returns Data which is of type UserDataRecord and DataVersion which is of type number.

The error is currently telling you that the copyTo you are using is failing because the current type is PlayFabClientModels.UserDataRecord[] and you are expecting string[]. You would need to iterate over each UserDataRecord and grab the Value data from each index.

I believe something along the lines of

string[] yourData = result.Data.Value.Select(x => x.Value).ToArray();

Should work. You will need to include using System.Linq at the top of your file. If it does not work, I would just need to know the typing of result.Data.Values but due to the error, I am assuming it is an innumerable array of data. I am not familiar with PlayFab so if the current docs are outdated please correct me in the comments and I'll try to update the answer.