Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by Justin Enayat · Oct 06, 2021 at 04:42 AM · apissdkssupport

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?

Comment

People who like this

0 Show 0
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by SethDu · Oct 06, 2021 at 08:57 AM

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);
        }
    }
}
Comment
Justin Enayat

People who like this

1 Show 1 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Justin Enayat · Oct 06, 2021 at 05:56 PM 0
Share

Thank you ahhhhh I was losing my mind yesterday

avatar image

Answer by Justin Enayat · Oct 06, 2021 at 04:45 AM

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 => { });     } }
Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    9 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    Time limited store 1 Answer

    Getting InvalidDropTable when calling EvaluateRandomResultTable 1 Answer

    Cloudscript returning NULL in Response 1 Answer

    Steam Login (Wrong AppID or Publisher ID)? 1 Answer

    Display Properties for User Generated Content? 1 Answer

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges