question

Kim Strasser avatar image
Kim Strasser asked

How can I get the custom data from my shop items?

I want to get the following custom data of my shop items but I get an error message when I want to get the values:

Error CS1061: 'KeyValuePair<string, object>' does not contain a definition for 'TryGetValue' and no accessible extension method 'TryGetValue' accepting a first argument of type 'KeyValuePair<string, object>' could be found (are you missing a using directive or an assembly reference?)

List<string> customdatakeys = new List<string>();
List<string> customdatavalueswood = new List<string>();
List<string> customdatavaluesiron = new List<string>();
List<string> customdatavaluesdiamond = new List<string>();

var result = await PlayFabClientAPI.GetCatalogItemsAsync(new PlayFab.ClientModels.GetCatalogItemsRequest()
{
    CatalogVersion = "MyShop"
});

if (result.Error != null)
{

}
else
{
foreach (var entry in result.Result.Catalog)
{
  if (entry.CustomData != null)
  {
    var customdata = PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject<Dictionary<string, object>>(entry.CustomData);
                                    
    foreach (var customentry in customdata)
    {
        customdatakeys.Add(customentry.Key);
        if (customentry.TryGetValue("Wood", out object x))
        {
            customdatavalueswood.Add(Convert.ToString(x));
        }
        else
            customdatavalueswood.Add("0");


        if (customentry.TryGetValue("Iron", out object y))
        {
            customdatavaluesiron.Add(Convert.ToString(y));
        }
        else
            customdatavaluesiron.Add("0");


        if (customentry.TryGetValue("Diamond", out object z))
        {
            customdatavaluesdiamond.Add(Convert.ToString(z));
        }
        else
            customdatavaluesdiamond.Add("0");
        }    
    }
  }
}
}

How can I get the custom data from my shop items in the client?

In-Game Economy
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

·
Seth Du avatar image
Seth Du answered

May I ask what's the platform and SDK version you are implementing the project? The error simply indicates the customentry doesn't have TryGetValue method. You may consider using GetType() or typeof() to print out the data type of this variable before making use of it.

1 comment
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

I use the C# SDK. It works now:

List<string> titlekeys = new List<string>();
List<string> titlevalues = new List<string>();

if (entry.CustomData != null)
{
  var customdata = PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject<Dictionary<string, string>>(entry.CustomData);

  foreach (KeyValuePair<string, string> pair in customdata)
  {
    string currentkey = pair.Key;
    if ((pair.Value != null) && (pair.Value != string.Empty))
    {
      titlekeys.Add(pair.Key);
      titlevalues.Add(pair.Value);
    }
  }

  for (int i = 0; i <= titlekeys.Count - 1; i++)
  {
    string key = titlekeys[i];
    var data = PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject<Dictionary<string, object>>(customdata[key]);

    if (data.TryGetValue("Wood", out object x))
    {
      customdatavalueswood.Add(Convert.ToString(x));
    }
    else
      customdatavalueswood.Add("0");
  }
}
0 Likes 0 ·

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.