question

brendan avatar image
brendan asked

Simple Buttons that Add and Subtract User Currency

Politicalpeacestudios
started a topic on Sun, 16 August 2015 at 5:54 AM

I want to have two very simple buttons. One that adds 100 points to current user currency, and another that subtracts 100 points from current user currency. And that is all the buttons do. I have seen AddUserVirtualCurrency and SubtractUserVirtualCurrency. But I don't really understand what its telling me to do, I am using Unity btw. The code in the "Sample Request" gives me a load of errors so I know I'm doing something wrong. How would I get this to work on C#?

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

·
brendan avatar image
brendan answered

Best Answer
sketchwork said on Sun, 16 August 2015 at 7:41 AM

You're welcome.

OK that's not going to work. Looks like you are trying to post raw html into your c# code.

You want something like this in your C# code:

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

            PlayFabClientAPI.AddUserVirtualCurrency(request, _OnAddCurrencyResult, _OnPlayFabError);
        }

        void _OnAddCurrencyResult(ModifyUserVirtualCurrencyResult _thisResult)
        {
            //Do all your successful handling in here
            Debug.Log("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.
            Debug.LogWarning("Got an error: " + _thisErrorResult.ErrorMessage);
        }

7 Comments
Politicalpeacestudios said on Sun, 16 August 2015 at 5:56 AM

Also in the documentation it says Im required to put a PlayFabID. But I don't want to take the money from a specific person? I want to take the money from who ever is logged in, the current user.


sketchwork said on Sun, 16 August 2015 at 6:54 AM

Hi there, Firstly, when you login you will be returned the current users ID when you login in. So just use this when dealing with these kind of operations. Secondly, in order to make sure this works you need to have the API calls activated in the game manager under settings. They are disabled by default to discourage the use due to the fact you should never really trust the client. Finally, you can use the AddUserVirtualCurrency and SubtractUserVirtualCurrency by using the api calls. The call will return the remaining balance and a few other things too. I hope this helps, Justin.


sketchwork said on Sun, 16 August 2015 at 7:03 AM

Actually, looking at your links - you seem to be using the Server API instead of the Client API. You should only really use the Client API. The Client API doesn't actually ask for the UserID because it will automatically use the user that you logged in as for the operation.


Politicalpeacestudios said on Sun, 16 August 2015 at 7:18 AM

Thanks for the reply again Justin! Good thing you pointed that out, that I was using the server documentation. Alright so I am still having a problem, this is my code Pastern Link and here is the error I am having Click Here. Any more advice? Than you again


sketchwork said on Sun, 16 August 2015 at 7:41 AM

You're welcome.

OK that's not going to work. Looks like you are trying to post raw html into your c# code.

You want something like this in your C# code:

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

            PlayFabClientAPI.AddUserVirtualCurrency(request, _OnAddCurrencyResult, _OnPlayFabError);
        }

        void _OnAddCurrencyResult(ModifyUserVirtualCurrencyResult _thisResult)
        {
            //Do all your successful handling in here
            Debug.Log("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.
            Debug.LogWarning("Got an error: " + _thisErrorResult.ErrorMessage);
        }

sketchwork said on Sun, 16 August 2015 at 7:42 AM

...and remember to put the correct using statements at the top or it won't work:

using PlayFab;
using PlayFab.ClientModels;

Politicalpeacestudios said on Sun, 16 August 2015 at 12:57 PM

Thanks a lot Justin! That is exactly what I needed man.. Thanks for all of your help!

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.

galichakristoffer avatar image galichakristoffer commented ·

this works for me but my text ui is not updating unless i restart the app. please me how to solve these,,,

0 Likes 0 ·
brendan avatar image brendan galichakristoffer commented ·

We do not interfere in the display layer of your game in any way - we're purely a backend service. How you display data you get from us is entirely up to you. What are you using to render the display to the player? A specific engine, like Unity? A plugin in that engine? Your own custom code?

1 Like 1 ·

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.