question

Opticgamer avatar image
Opticgamer asked

How to Display Virtual Currency?

Hello, I have looked at many questions about trying to display virtual currency but could never understand how to use it properly. Most of the examples involve using static classes and I've never used them before. Is it possible to get vc without static classes? Can anyone provide an example in C# on how exactly to use it? I have looked at the example project but still don't understand how to use it properly since i've never used static classes:

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

Character Data
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

I am not sure what is the detailed scenario but here is an example, and I am using Unity with LINQ to output VC.

using PlayFab;
using PlayFab.ClientModels;
using System.Linq;
using System;

// in start()
Dictionary<string, int> VC = new Dictionary<string, int>();

PlayFabClientAPI.LoginWithCustomID(
    new LoginWithCustomIDRequest {
        CreateAccount = true,
        CustomId="xxxxxxx",
        TitleId = "xxxx"
    },
    loginResult=> {
        PlayFabClientAPI.GetUserInventory(
            new GetUserInventoryRequest {},
            GetResult=> {
                VC = GetResult.VirtualCurrency;
                var lines = VC.Select(kvp => kvp.Key + ": " + kvp.Value.ToString());
                print(string.Join(Environment.NewLine, lines));
            },
            GetError=> { });
    },
    loginError=> { });
The VirtualCurrency in the callback result of GetUserInventory is a Dictionary.
4 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.

Opticgamer avatar image Opticgamer commented ·

Wow thank you, this is exactly what I was looking for :)

0 Likes 0 ·
Nagharajan R avatar image Nagharajan R commented ·

I am trying to use this coding but If I have to show the value inside a unity text field like given below. How do i write the code? sorry I am new to this coding and trying to understand.


InGameAmounttext.text = result.VirtualCurrency.TryGetValue("GC", out Credits);


Debug.Log(lines); when I use this after the command I am able to see the player's currency and value pulling correctly but i am unsure as to how I can convert that as a text. can someone please help ?

0 Likes 0 ·
Opticgamer avatar image Opticgamer Nagharajan R commented ·
Get the text you want to assign the VirtualCurrency to and set it equal to the result. Its already shown in the debug line. Just change print to "your text name".text = string.Join(Environment.NewLine, lines); 
1 Like 1 ·
Nagharajan R avatar image Nagharajan R Opticgamer commented ·

Thank you so much. I appreciate the assistance.

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.