question

Kim Strasser avatar image
Kim Strasser asked

How can I add virtual currency to a user's account?

I have a problem with the API. I don't know how to use AddUserVirtualCurrencyAsync correctly.

I get these error messages:

Error CS1503: Argument 2: cannot convert from 'method group' to 'object'

Error CS1503: Argument 3: cannot convert from 'method group' to 'Dictionary<string, string>'

What is wrong?

I want that a user can add gold coins(virtual currency) to his PlayFab account. In addition, I need to find out what the user's current amount of gold coins is when he starts the game. How can I get the current amount of gold coins from PlayFab when I start the game?

AddCurrency();

        void AddCurrency()
        {
            AddUserVirtualCurrencyRequest request = new AddUserVirtualCurrencyRequest();
            request.VirtualCurrency = "CU"; //put your virtual currency code here
            request.Amount = 100; //put the amount in here
            PlayFabClientAPI.AddUserVirtualCurrencyAsync(request, _OnAddCurrencyResult, _OnPlayFabError);
        }

        void _OnAddCurrencyResult(ModifyUserVirtualCurrencyResult _thisResult)
        {
            //Do all your successful handling in here
            Console.WriteLine("User has a current balance of: " + _thisResult.Balance + " (" + _thisResult.VirtualCurrency + ") after this change: " + _thisResult.BalanceChange);
        }

        void _OnPlayFabError(PlayFabError _thisErrorResult)
        {
            //Do all your error handling in here.
            Console.WriteLine("Got an error: " + _thisErrorResult.ErrorMessage);
        }


apis
2 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.

jital avatar image jital ♦ commented ·

Hello,

Which SDK are you using? Your code is C# and uses the Async methods which are apart of the base C# SDK, while in your code you are using callbacks which are a feature in the Unity C# SDK.

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser commented ·

I use the Xamarin SDK and MonoGame. I don't use Unity.

0 Likes 0 ·

1 Answer

·
jital avatar image
jital answered

Greetings,

Below is code for using the AddUserVirtualCurrency API call in Xamarin SDK.

private void AddVC()
{
	AddUserVirtualCurrencyRequest vcRequest = new AddUserVirtualCurrencyRequest
	{
		Amount = 100,
		VirtualCurrency = "TC"
	};


	var AddVcTask = PlayFabClientAPI.AddUserVirtualCurrencyAsync(vcRequest);
	AddVcTask.ContinueWith(OnAddVCComplete);
}


private void OnAddVCComplete(Task<PlayFabResult<ModifyUserVirtualCurrencyResult>> task)
{
	string message = "Unknown Error?";
	
	if (task.Result.Result != null)
	{
		message = "Congratulations, your Add VC call was a successful API call!";
	}
	
	if (task.Result.Error != null)
	{
		message = "Something went wrong with your Add VC API call.\n"
		+ "Here's some debug information:\n"
		+ task.Result.Error.GenerateErrorReport();
	}

	lock (_myLabelText)
	{
		_myLabelText = message;
	}
}
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.

Kim Strasser avatar image Kim Strasser commented ·

Thank very much :) It works now. The user got 100 virtual currency on his PlayFab account.

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.