question

Justin Enayat avatar image
Justin Enayat asked

KeyNotFoundException: The given key was not present in the dictionary.

I'm trying to set up a store in my game with PlayFab. I've set up all the items in a catalog called "Ships", and I have a virtual currency set up with the id "CC." When this code is ran, the player is logged in, and the code has already gotten all the proper info parameters.

 using System.Collections; using System.Collections.Generic; using UnityEngine; using PlayFab; using PlayFab.ClientModels; using PlayFab.Json; public class PlayFabManager : MonoBehaviour {     public Ship[] ships;      public void GetShipPrices()     {         GetCatalogItemsRequest request = new GetCatalogItemsRequest();         request.CatalogVersion = "Ships";         PlayFabClientAPI.GetCatalogItems(request, result =>         {             List<CatalogItem> shipsInStore = result.Catalog;             foreach (CatalogItem i in shipsInStore)             {                 uint cost = i.VirtualCurrencyPrices["CC"];                 Debug.Log(cost);             }         }, error => { });     } }

When I call this method (after all the login stuff has happened), I keep getting this error:


KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at :0) PlayFabManager+<>c.b_33_0 (PlayFab.ClientModels.GetCatalogItemsResult result) (at Assets/Scripts/PlayFabManager.cs:140) PlayFab.Internal.PlayFabHttp+<>c_DisplayClass19_0`1[TResult].<MakeApiCall>b1 () (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabHTTP.cs:215) PlayFab.Internal.PlayFabUnityHttp.OnResponse (System.String response, PlayFab.Internal.CallRequestContainer reqContainer) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:201) UnityEngine.Debug:LogException(Exception) PlayFab.Internal.PlayFabUnityHttp:OnResponse(String, CallRequestContainer) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:205) PlayFab.Internal.d_12:MoveNext() (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:153) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)


The PlayFabManager.cs:140 is at the "uint cost = i.VirtualCurrencyPrices["CC"];" line. I have no idea why, I've searched all over the PlayFab documentation and can't figure out what I'm doing wrong.. any ideas?

apissdkssupport
10 |1200

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

Seth Du avatar image
Seth Du answered

As you can see the error is “KeyNotFoundException: The given key was not present in the dictionary.”, which indicates that when iterating the item list, some items doesn’t have a key named “CC” in VirtualCurrencyPrices dictionary.

Furthermore, according to my testing, PlayFab SDK won’t initialize an empty VirtualCurrencyPrices property for CatalogItem object, hence you will need to check null before retrieving the value.

Please refer to my sample code below:

foreach (CatalogItem i in shipsInStore)

{
    if (i.VirtualCurrencyPrices != null)
    {
        if (i.VirtualCurrencyPrices.TryGetValue("CC", out var value))
        {
            print(value);
        }
    }
}
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.

Justin Enayat avatar image Justin Enayat commented ·

Thank you ahhhhh I was losing my mind yesterday

0 Likes 0 ·
Justin Enayat avatar image
Justin Enayat answered

Sorry, the code didn't paste very well and I don't know how to edit the post.

using System.Collections; 

using System.Collections.Generic;

using UnityEngine; using PlayFab; 

using PlayFab.ClientModels; 

using PlayFab.Json; 

public class PlayFabManager : MonoBehaviour {     

public Ship[] ships;      

public void GetShipPrices()     

{         

GetCatalogItemsRequest request = new GetCatalogItemsRequest();         

request.CatalogVersion = "Ships";         

PlayFabClientAPI.GetCatalogItems(request, result =>         

{             

List<CatalogItem> shipsInStore = result.Catalog;             

foreach (CatalogItem i in shipsInStore)             

{                 

uint cost = i.VirtualCurrencyPrices["CC"];                

Debug.Log(cost);             

}      }, error => { });     } }
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.