question

Talha Muhammad avatar image
Talha Muhammad asked

Bug Related to Catalogue.

Hi, I dont know whether this is a bug or some issue from my end.

In the Success call back to the GetCatalogue Api. Everything runs fine. I get all the prices. But whenever I save the catalogue using result.ToJson() and then load it again. Everything is still fine apart from the the prices. VirtualCurrencyPrices are null even tho they are there in the txt file savedcatalogue.txt. As you can see All the values are there in the text file but retrieving them i get null.

   uint cost = item.VirtualCurrencyPrices["GC"];
   Debug.Log("Loaded.. item Price = " + cost);

this always return null exception. 
private void SuccessGettingCatalogue(GetCatalogItemsResult result)
{
 foreach (CatalogItem item in result.Catalog)
        {
            uint cost = item.VirtualCurrencyPrices["GC"];
            Debug.Log("Loaded.. item Price = " + cost);
        }

 var str= result.ToJson();

 string path = Path.Combine(Application.persistentDataPath, "SavedCatalogue.txt");
 File.WriteAllText(path,str);
 

}


    void LoadData()
    {
        string jsonString = LoadJson();
       GetCatalogItemsResult MySavedCatalogueResult= JsonUtility.FromJson<GetCatalogItemsResult>(jsonString);
        List<CatalogItem> MySavedCatalogue = MySavedCatalogueResult.Catalog;
        foreach (CatalogItem item in MySavedCatalogue)
        {
            uint cost = item.VirtualCurrencyPrices["GC"];
            Debug.Log("Loaded.. item Price = " + cost);
        }

    }

public string LoadJson()
{
	string MyString;
	string path = Path.Combine(Application.persistentDataPath, "SavedCatalogue.txt");
	if (File.Exists(path))
	{
		using (StreamReader reader = new StreamReader(path))
		{
			 MyString=File.ReadAllText(path);
		}
		return MyString;
	}
		return null;
	}

	
savedcatalogue.txt (9.3 KiB)
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.

Talha Muhammad avatar image Talha Muhammad commented ·

??

0 Likes 0 ·
Talha Muhammad avatar image
Talha Muhammad answered

I had to find the solution on my own.

unity3d - C# Unity Reading data from JSON into an Object Array - Stack Overflow. This linked helped me with grab the VCs.

10 |1200

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

Rick Chen avatar image
Rick Chen answered

This is not the API bug. Could you please describe which platform you are using? Is it Unity? If so, please note that the VirtualCurrencyPrices is a Dictionary with "string" keys and "int" values, it cannot simply be converted to json using result.ToJson().

Here’s an example of the demo in Unity:

https://github.com/PlayFab/UnicornBattle/blob/master/UnicornBattle/Assets/Scripts/PF_StaticsAndHelpers/PF_PlayerData.cs#L78

These 2 lines show you how to handle the VC in Unity:

foreach (var pair in result.VirtualCurrency)

virtualCurrency.Add(pair.Key, pair.Value);

Please refer to this thread: https://community.playfab.com/questions/49828/unable-to-fetch-player-virtual-currency.html

2 comments
10 |1200

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

Talha Muhammad avatar image Talha Muhammad commented ·

Hi, Yes the platform is unity. So what should I do then? I need to save the whole catalogue on device and load it at the start of game each time.

When I load the object, In "MySavedCatalogue" the virtual currencies is always null.

0 Likes 0 ·
Talha Muhammad avatar image Talha Muhammad commented ·

I Dont understand. Im beyond confused. Could you please modify the code to make it clear.

??

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.