question

David Jones avatar image
David Jones asked

Accessing Virtual Currency

I got this bit of code off of unicorn battle, and I was wondering how you access the different values within the created dictionary. So if i was looking for my "Gold" currency that is already on my player to change to a string and display on the main menu. Using Unity C#

private static Dictionary<string, int> virtualCurrency = new Dictionary<string, int>();    

public static void GetUserInventory(Action callback = null)
    {
        PlayFabClientAPI.GetUserInventory(new GetUserInventoryRequest(), (GetUserInventoryResult result) =>
        {
        virtualCurrency.Clear();
        playerInventory.Clear();


        foreach (var pair in result.VirtualCurrency)
            {
                virtualCurrency.Add(pair.Key, pair.Value);
		//trying to return these ^^^

            }
            
        foreach (var eachItem in result.Inventory)
            {
               
            }


        if (callback != null)
            callback();


        PF_Bridge.RaiseCallbackSuccess("", PlayFabAPIMethods.GetUserInventory, MessageDisplayStyle.none);
    }, PF_Bridge.PlayFabErrorCallback);
    }
10 |1200

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

Hernando avatar image
Hernando answered

The simplest way to access the amount of a currency, you can access by the string key, please refer the following code:

result.VirtualCurrency["GM"]

Then convert the int value to a string before displaying it on the screen:

yourTextObject.text = result.VirtualCurrency["GM"].ToString();

Besides, If you want to store the entire dictionary object to your private variable virtualCurrecy:

virtualCurrency = result.VirtualCurrency;
10 |1200

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

Akarsh jain avatar image
Akarsh jain answered

@Hernando yeah but its not working in the case we want to display it as the text, then it returns error object not set to reference: NULL, really annoying bug stuck on it from 3 days. PLEASE HELP!!!

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.

askmnaskym22 avatar image askmnaskym22 commented ·

did you find a solution?

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.