question

Kim Strasser avatar image
Kim Strasser asked

How can I find out in the client if a statistic exists and get its value?

I get error messages when I want to use Contains to get the value of a statistic:

Error CS1503: Argument 1: cannot convert from 'string' to 'PlayFab.ClientModels.StatisticModel'
Error CS1503: Argument 1: cannot convert from 'string' to 'int'

I need to get the values of several statistics for each of the 20 players. Then, I add the values into the corresponding list. If a certain statistic doesn't exist, then I want to add 0 to the corresponding list.

But I don't know how to use Contains because it doesn't take a string.

How can I find out if a statistic exists and get its value? Can I use Contains?

List<int> Levelsplayedlist = new List<int>();
List<int> Countrylist = new List<int>();

var result = await PlayFabClientAPI.GetLeaderboardAsync(new GetLeaderboardRequest()
{
  StatisticName = "Level 1",
  MaxResultsCount = 20,     
  ProfileConstraints = new PlayFab.ClientModels.PlayerProfileViewConstraints()
  {
    ShowStatistics = true,
    ShowDisplayName = true,
    ShowAvatarUrl = true,
    ShowTags = true,
    ShowCreated = true,
    ShowLastLogin = true
  }
});

foreach (var entry in result.Result.Leaderboard)
{
  if (entry.Profile.Statistics.Contains("Total levels played"))
    Levelsplayedlist.Add(entry.Profile.Statistics["Total levels played"].Value);

  if (entry.Profile.Statistics.Contains("Playercountry"))
    Countrylist.Add(entry.Profile.Statistics["Playercountry"].Value);
}
Leaderboards and Statistics
10 |1200

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

1 Answer

·
Ivan Cai avatar image
Ivan Cai answered

As your problem describes, entry.Profile.Statistics returns a List<StatisticModel>, and you can‘t use Contains to determine whether the Name in the StatisticModel matches what you want,since entry.Profile.Statistics.Contains(StatisticModel item) does not take String as the param.When you want to get the values of several statistics, you can traverse entry.Profile.Statistics(List<StatisticModel>) directly, and get the Value to add.

The code can be modified as follows:

 foreach (var entry in result.Result.Leaderboard)
        {
            foreach (var stat in entry.Profile.Statistics)
            {
                switch (stat.Name)
                {
                    case "Total levels played":
                        Levelsplayedlist.Add(stat.Value);
                        break;
                    case "Playercountry":
                        Countrylist.Add(stat.Value);
                        break;
                    default:
                        break;
                }
            }
        }

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.