Using GameSparks, it's possible to create two different types of virtual good. The first is an in-game item: for example, an inventory or in-game shop item. The second is a currency pack which, upon purchase, will award the purchasing player some virtual currency. Out of the box currency pack goods can only be purchased using a third-party store (for example, the Google Play store) and real money.


But let's say you wanted to purchase a currency pack using a virtual, in-game currency. Although it's not possible to do this through your game configuration alone, it can be done using some custom Cloud Code. For example, suppose in our game we have two virtual currency types - gold nuggets and gold bars - and we want to be able to exchange 100 gold nuggets for 5 gold bars.


There are three stages in the setup for this use case:

  • Create Virtual Currencies
  • Create Virtual Good
  • Write Cloud Code


Create Virtual Currencies 


First, we'll set up our currencies:




NOTE: Assigning a Sign-up Bonus is optional. If set, each newly registered player will receive the amount specified automatically.


Create Virtual Good

Next, we'll set up the virtual good we're going to buy.



These are the configuration settings we need:

  • We need to set the Type to Virtual Good. Although this virtual good will be acting as a currency pack, setting it to Currency Pack in the Configurator would mean we won't be able to purchase it using our gold nugget currency. Instead, for Tags, we'll add a single goldbarpack tag to let us know that this good is a currency pack of gold bars.
  • We set two Currency Costs using the virtual currencies we created earlier:
    • The goldNugget cost, which is how many gold nuggets the player will need to pay to purchase the good.
    • The goldBar cost, which is the number of gold bars that the player will be awarded on purchasing the pack. 


Cloud Code

Finally, we'll need to add some Cloud Code to our BuyVirtualGoodsResponse:

  • This will trigger each time a good is purchased, either through a BuyVirtualGoodsRequest or one of our third party store buy goods requests.
  • In this script, we'll check the nature of the purchased good (based on the tag set earlier for the virtual good) and award the corresponding gold bar currency where appropriate.


//Spark.getData will return the response JSON in it's entirety, including
//details of the item(s) which were purchased
var dat = Spark.getData();

//Check for errors. If the purchase itself failed we don't want to proceed
//any further
if(dat.error)   Spark.exit();

//We retrieve the details of the purchased item. The response's 'boughtItems'
//field contains an array of purchased good objects each containing the 
// shortCode and quantity of said good. 
//Because we are dealing with solely with virtual currencies, we're only 
//taking the first purchased good as it is impossible to purchase 
//multiple types of good via a BuyVirtualGoodsRequest.
var goodBought = dat.boughtItems[0];

//We then use the shortCode of the bought item to retrive the details 
// we've configured against the good type.
var goodConfig = Spark.getConfig().getVirtualGood(goodBought.shortCode);

//Check if the good has been tagged as a gold bar pack. The getTags function
//returns the goods configured tags as an array of strings.
if(goodConfig.getTags().indexOf("goldbarpack")>-1){
    
    //If the good in question is a gold bar pack, we calculate how much
    //of the gold bar currency to award based on the gold bar cost we
    //set on the good and the quantity of the good that was purchased,
    //then credit it to the player.
    //We can add a note to describe the reason the currency was awarded
    //which will appear in the playerTransactionAudit record.
    var player = Spark.getPlayer();
    player.credit(
        "goldBar", 
        goodConfig.getCurrencyCosts().goldBar * goodBought.quantity, 
        "Purchased " + goodBought.quantity + " " + goodBought.shortCode
    );
    
    //Finally, we consume the virtual good as it has served it's
    //purpose. Note that this step is optional, you may wish to keep
    //the virtual good as an easy way to check the total number of packs 
    //the player has bought.
    player.useVGood(goodBought.shortCode, goodBought.quantity);
}