question

hapygames avatar image
hapygames asked

Virtual Currency doesn´t update

The amount of the Virtual Currency I purchase doesn´t update.

This is the method I use to buy the virtual currency :

public void Give100Coins()
    {
        PurchaseItemRequest purchaseRequest = new PurchaseItemRequest();
        purchaseRequest.CatalogVersion = "ShopItems";
        purchaseRequest.ItemId = "Coins";
        purchaseRequest.VirtualCurrency = "HC";
        purchaseRequest.Price = 50;
        PlayFabClientAPI.PurchaseItem(purchaseRequest, result => {
            Debug.Log("You recieved 100 Coins");
        }, error => {
            Debug.LogError(error.ErrorMessage);
        });
    }

And this is the login method that I edited, in order to get the virtual currency.

public void Login()
    {
        loginWithEmail = new LoginWithEmailAddressRequest { Email = userEmail, Password = userPassword };
        loginWithEmail.TitleId = PlayFabSettings.TitleId;


        //Virtual Currency
        loginWithEmail.InfoRequestParameters = playerInfo;


        // Request Login to PlayFab API
        PlayFabClientAPI.LoginWithEmailAddress(loginWithEmail, result => 
        {
            HappyCoins = result.InfoResultPayload.UserVirtualCurrency["HC"];
            OnLoginResult(result);


        }, OnPlayFabError);
    }


    public void OnLoginResult(LoginResult obj)
    {
        PlayerPrefs.SetString("EMAIL", userEmail);
        PlayerPrefs.SetString("PASSWORD", userPassword);


        PhotonNetwork.LoadLevel("Lobby");
    }

I also activated the ability, for clients, to add and subtract virtual currency.

Do I need to have the latest PlayFab SDK, in order for this to work?

Thanks, in advance, for the help!

10 |1200

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

Made Wang avatar image
Made Wang answered

Your code only obtains the virtual currency once after the login is successful.Even if it is placed in the Update, it will not be obtained repeatedly, so if you log in again, you should be able to see the virtual currency changes.

If you don't want to log in again to obtain the update of virtual currency, you can get the value of the virtual currency after each purchase. I modified your code and used GetPlayerCombinedInfo, you can refer to it.

public void Give100Coins()
    {
        PurchaseItemRequest purchaseRequest = new PurchaseItemRequest();
        purchaseRequest.CatalogVersion = "ShopItems";
        purchaseRequest.ItemId = "Coins";
        purchaseRequest.VirtualCurrency = "HC";
        purchaseRequest.Price = 50;
        PlayFabClientAPI.PurchaseItem(purchaseRequest, result => {
            Debug.Log("You recieved 100 Coins");
            //Get Player Combined Info
            PlayFabClientAPI.GetPlayerCombinedInfo(new GetPlayerCombinedInfoRequest
            {
                InfoRequestParameters = new GetPlayerCombinedInfoRequestParams
                {
                    GetUserVirtualCurrency = true
                }
            },
            result =>
            {
                HC_Text.text = "Happy Coin(s): " + result.InfoResultPayload.UserVirtualCurrency["HC"].ToString();
            },
            error =>
            {
                Debug.LogError(error.ErrorMessage);
            });
        }, error => {
            Debug.LogError(error.ErrorMessage);
        });
    }


10 |1200

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

Made Wang avatar image
Made Wang answered

Do you use Unity to develop your feature? And did you call both methods successfully? If your Give100Coins method call is successful, then you can check whether the virtual currency is reduced in Game Manager->Players->[Your Player]->VirtualCurrency, you can open Game Manager- >Title Overview->PlayStream Monitor page, and then run your program. Here you can see the detailed purchase process about who bought which item, and how much virtual currency has been reduced. If these are all successful, and your game UI still doesn’t be updates, maybe you just need to do it get the value of virtual currency again.

About SDK, we recommend that you keep the latest version.

If you have any errors during the running of the program, please provide us the detailed error messages.

10 |1200

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

hapygames avatar image
hapygames answered

The method "Give100Coins" is succesful and gives 100 coins, when pressing the button, and the virtual currency is being added. Under Game Manager->Players->[Your Player]->VirtualCurrency, I see that my Player has an amount of 28250 coins.

In Unity though, the amount of my virtual currency is not being shown.

Here is the script, that I use to make the amount of coins viewable:

public class VirtualCurrency : MonoBehaviour
{
    public Text HC_Text;
    public CustomAccountManagement CAM;
    private void Start()
    {
        HC_Text = GameObject.Find("HappyCoinsText").GetComponent<Text>(); 
        CAM = GameObject.Find("PlayFabManager").GetComponent<CustomAccountManagement>();
    }



    private void Update()
    {
        HC_Text.text = "Happy Coin(s): " + CAM.HappyCoins;
        //Debug.Log(CAM.HappyCoins);
    }



    public void Give100Coins()
    {
        PurchaseItemRequest purchaseRequest = new PurchaseItemRequest();
        purchaseRequest.CatalogVersion = "ShopItems";
        purchaseRequest.ItemId = "Coins";
        purchaseRequest.VirtualCurrency = "HC";
        purchaseRequest.Price = 50;
        PlayFabClientAPI.PurchaseItem(purchaseRequest, result => {
            Debug.Log("You recieved 100 Coins");
        }, error => {
            Debug.LogError(error.ErrorMessage);
        });
    }
}

The "CustomAccountManagement" is the script where the login/registration is being managed.

Here is the Login part of the script, again, with all the references:

public class CustomAccountManagement : MonoBehaviour
{
    public static CustomAccountManagement customAccountManagement;

    public LoginWithEmailAddressRequest loginWithEmail;

    //Virtual Currency
    public GetPlayerCombinedInfoRequestParams playerInfo;
    public int HappyCoins;



    //Login
    public void Login()
    {
        loginWithEmail = new LoginWithEmailAddressRequest { Email = userEmail, Password = userPassword };
        loginWithEmail.TitleId = PlayFabSettings.TitleId;
        //Virtual Currency
        loginWithEmail.InfoRequestParameters = playerInfo;
        // Request Login to PlayFab API
        PlayFabClientAPI.LoginWithEmailAddress(loginWithEmail, result => 
        {
            HappyCoins = result.InfoResultPayload.UserVirtualCurrency["HC"];
            OnLoginResult(result);
        }, OnPlayFabError);
    }
    public void OnLoginResult(LoginResult obj)
    {
        PlayerPrefs.SetString("EMAIL", userEmail);
        PlayerPrefs.SetString("PASSWORD", userPassword);
        PhotonNetwork.LoadLevel("Lobby");
    }
}
 

I activated these things in the GetPlayerCombinedInfoRequestParams:


screenshot-1.png (59.0 KiB)
10 |1200

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

hapygames avatar image
hapygames answered

Thank you! It worked. I just had to modify your solution a bit.

Here is the working solution, for the people who might want to know it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab.ClientModels;
using PlayFab;
using System;
using TMPro;


public class VirtualCurrency : MonoBehaviour
{


    public TMP_Text HCText;
    public CustomAccountManagement CAM;




    private void Start()
    {
        HCText = GameObject.Find("HappyCoinsText").GetComponent<TextMeshProUGUI>(); 
        CAM = GameObject.Find("PlayFabManager").GetComponent<CustomAccountManagement>();
    }




    public void Give100Coins()
    {
        PurchaseItemRequest purchaseRequest = new PurchaseItemRequest();
        purchaseRequest.CatalogVersion = "ShopItems";
        purchaseRequest.ItemId = "100Coins";
        purchaseRequest.VirtualCurrency = "HC";
        purchaseRequest.Price = 0;
        PlayFabClientAPI.PurchaseItem(purchaseRequest, result => {
            Debug.Log("You recieved 100 Coins");
            //Get Player Combined Info
            PlayFabClientAPI.GetPlayerCombinedInfo(new GetPlayerCombinedInfoRequest
            {
                InfoRequestParameters = new GetPlayerCombinedInfoRequestParams
                {
                    GetUserVirtualCurrency = true
                }
            },
            equals =>
            {
                string coinsOutput;


                coinsOutput = "Happy Coin(s)" + equals.InfoResultPayload.UserVirtualCurrency["HC"];


                HCText.text = coinsOutput;


                Debug.Log("Happy Coin(s)" + equals.InfoResultPayload.UserVirtualCurrency["HC"].ToString());
            },
            failure =>
            {
                Debug.LogError(failure.ErrorMessage);
            });
        }, error => {
            Debug.LogError(error.ErrorMessage);
        });
    }
}
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.