ActionScript Virtual Goods

Introduction

Once you've created Virtual Goods on the Portal, you can incorporate them in your game.

This tutorial shows you how to buy and consume goods using a shop interface:

Creating a 'Grant Currency' Event

Creating the Buy and Consume Functions

Keeping Track of Player Details

Testing Virtual Goods


Example ActionScript Virtual Goods code can be downloaded here

Creating the 'Grant Currency' Event

1. Navigate to Configurator > Events and click Add. The Add Event page opens.

2. Create an Event that credits the authenticated player with extra currency.

3. Add an Attribute that will be used to indicate the amount to credit.

You will log this Event whenever the authenticated player consumes a Gold Coin (Virtual Good):

4. Save the Grant currency Event and navigate to Configurator > Cloud Code and under Scripts click Events.

5. Select the GRANT_CURRENCY Event to open the Cloud Code Editor. In the Cloud Code Editor, create a variable that holds the amount being passed in the Event through the CASH Attribute, and call it money:

6. Next, get the player object using Spark.getPlayer() and credit their CURRENCY_1 with the money value.

Here's the Cloud Code:

//Get the data passed in from the player
var money = Spark.getData().CASH
//Credit the player with the amount asked for the chosen currency and the reason.
Spark.getPlayer().credit("CURRENCY_1", money, "For clicking the button")


7. Click to Save the Cloud Code you've added to the GRANT_CURRENCY Event.

Creating the Buy and Consume Functions

8. Create a function which will log a buyRequest when called by using BuyVirtualGoodsRequest. The BuyVirtualGoodsRequest needs your custom currency Short Code, a quantity, and the Short Code for the item to be purchased.

                private function PurchaseItem():void
                {
                    logger("Buying Virtual Good...");
                    requestBuilder.createBuyVirtualGoodsRequest().setCurrencyShortCode(CURRENCY_1).setQuantity(Number(BuyQnt.text)).setShortCode("Gold_Coin").send(BuyResponse);
                }

9. Create a function for the consumption of Virtual Goods, this will use the ConsumeVirtualGoodRequest. The ConsumeVirtualGoodRequest needs a quantity and Short Code of the item to be consumed.

        private function ConsumeItem():void
                {
                    logger("Consuming Virtual Good...");
                    requestBuilder.createConsumeVirtualGoodRequest().setQuantity(Number(ConsumeQnt.text)).setShortCode("Gold_Coin").send(ConsumeResponse);
                }

10. Now you can make the response handler functions for your consume and buy requests. Both response handlers will be similar. For our tutorial, we won't need them to do much:

        private function BuyResponse(response:BuyVirtualGoodResponse):void
                {

                    if (response.HasErrors())
                    {
                        logger("Problem occurred");
                    }
                    else
                    {
                        logger("Item bought");
                    }

                    requestBuilder.createAccountDetailsRequest().send(UpdateDetailsForShop);
                }

        private function ConsumeResponse(response:ConsumeVirtualGoodResponse):void
                {
                    if (response.HasErrors())
                    {
                        logger("Problem occurred");
                    }
                    else
                    {
                        requestBuilder.createLogEventRequest().setEventKey("Grant_Currency").setNumberEventAttribute("CASH", Number(ConsumeQnt.text)).send(GeneralLogResponse);
                        logger("Item consumed");
                    }

                    requestBuilder.createAccountDetailsRequest().send(UpdateDetailsForShop);
                }

Keeping Track of Player Details

11. To update the shop details, request the account details for the currently authenticated player:

        private function UpdateDetailsForShop(response:AccountDetailsResponse):void
                {
                    ShopCurrency.text = response.getCurrency("CURRENCY_1").toString();
                    NOwned.text = response.getVirtualGoods().Gold_Coin.toString();
                }

Testing Virtual Goods

Create a way for the player to buy and consume Virtual Goods in your project. Test your shop to make sure that:

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here