question

Cricket_FeVR avatar image
Cricket_FeVR asked

Reward certain amount of virtual currency to the user after game

How to give rewards(certain amount of virtual currency) from the server side to the user after completing a level? The coins are variable according to the xp gained from each level(I mean the coins are not given as bundle)

Can someone post the unity code. PlayFabServerAPI.AddUserVirtualCurrency?

CloudScriptIn-Game Economygame manager
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 answered

Hello,

Below is a the bare C# Unity code needed to Add VC(Virtual Currency) to a specific user from the ServerAPI

public static void AddVC()
{
     string id = "RE5HN9SD0WNKJ"; //Not a real PlayFab ID you will need to retrieve
                                  //the PlayFab ID of the User who's VC that you
            			  // would like to increase

     int vcToAdd = 1000; //You can add a function that will map a certain amount 
                         //of XP gain to VC to grant
     

     AddUserVirtualCurrencyRequest addVCrequest = new AddUserVirtualCurrencyRequest
     {
          Amount = vcToAdd,
          PlayFabId = id,
          VirtualCurrency = "VirtualCurrency"
     };


     PlayFabServerAPI.AddUserVirtualCurrency(addVCrequest, result => { Debug.Log("Success"); }, error => { Debug.Log("Error"); });

}
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.

brendan avatar image brendan ♦♦ commented ·

Just one addition - please be sure to put some code in this to help prevent cheating. As is, a hacker could simply call AddVC over and over again, getting VC every time. What you'll want to do is check some basics - are the values reported by the client valid for your game? When is the last time the player completed a level (if you're also updating user data, you could check that for the update time), etc.

0 Likes 0 ·
julianmsmith1 avatar image julianmsmith1 brendan ♦♦ commented ·

hey for some reason I get an error when I try to use the PlayFabID. Is there a setting or import I should be using?

0 Likes 0 ·
julianmsmith1 avatar image
julianmsmith1 answered

Hey guys, everytime I try to run tat cide I get the following error:

error CS0117: 'AddUserVirtualCurrencyRequest' does not contain a definition for 'PlayFabId'

Is there a certain setting or import I should be using?

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.