Sign In Register

How can we help you today?

Start a new topic
Answered

Buy currency pack with virtual currency

Im trying to create packs of "gold" currency1 in my game and they are supposed be purchasable with currency2 (paid currency), so I just basically need to buy goldpack1 which will cost 80 currency 2 and should give me 1k of currency 1, there is any built-in way to do this? Otherwise sounds like I would need to setup my golpack1 as a virtual good and give the amount of currency1 in BuyVirtualGoodResponse which I was trying to avoid tho.


Any ideas?


Best Answer

Ok, thats what I thought. So I came up with this solution:


  

var list = Spark.getData().boughtItems;

if(list)
{
    var virtualCurrency = Spark.getProperties().getProperty("CONFIG").virtualCurrency;
    var showBalance = false;
    var player = Spark.getPlayer();
    
    for (i = 0; i < list.length; i++) 
    {
        var shortCode = list[i].shortCode;
        var ach = Spark.getConfig().getVirtualGood(shortCode);
        
        if(ach.getTags().indexOf(shortCode) !== -1)
        {
            player.credit1(ach.getCurrency1Cost());
            
            var request = new SparkRequests.ConsumeVirtualGoodRequest();
            request.quantity = 1;
            request.shortCode = shortCode;
            request.Send();
            
            showBalance = true;
        }
    }
    
    if(showBalance){
        Spark.setScriptData("balance1", player.getBalance1());
    }
}

  


Hi Hjupter,


No there is no built in way to do this I'm afraid. Currency Packs are used when purchasing an IAP from a 3rd party store. The values stored in currencies 1-6 are whats credited to the player when they make the purchase through the 3rd party store. Storing the shortCode of the Virtual Good with its credit value and then crediting in manually in the BuyVirtualGoodResponse would probably be the way to go here. So if the player had enough to purchase goldpack1 and they did purchase it, in the response you could lookup the packs credit value (you could store this in a collection or with a property attached to the Virtual Good) and then credit it with the following.

  

Spark.getPlayer().credit1(1000, "Custom Reason");

  

The "Custom Reason" string is not required but may be useful if you want to log some info about the purchase. If you have any further questions just let us know.


Thanks,

Liam


1 person likes this
Answer

Ok, thats what I thought. So I came up with this solution:


  

var list = Spark.getData().boughtItems;

if(list)
{
    var virtualCurrency = Spark.getProperties().getProperty("CONFIG").virtualCurrency;
    var showBalance = false;
    var player = Spark.getPlayer();
    
    for (i = 0; i < list.length; i++) 
    {
        var shortCode = list[i].shortCode;
        var ach = Spark.getConfig().getVirtualGood(shortCode);
        
        if(ach.getTags().indexOf(shortCode) !== -1)
        {
            player.credit1(ach.getCurrency1Cost());
            
            var request = new SparkRequests.ConsumeVirtualGoodRequest();
            request.quantity = 1;
            request.shortCode = shortCode;
            request.Send();
            
            showBalance = true;
        }
    }
    
    if(showBalance){
        Spark.setScriptData("balance1", player.getBalance1());
    }
}

  


1 person likes this

Went into the same question and I think the answer is bit confusing... It credits the "cost of currency 1" directly without using property, which I think is a good approach, it also checking the tag which I don't think it's necessary...


So the modified version will be:

var list = Spark.getData().boughtItems;

if (list) {
    
    var player = Spark.getPlayer();
    
    for (i = 0; i < list.length; i++) {
        
        var shortCode = list[i].shortCode;
        var vg = Spark.getConfig().getVirtualGood(shortCode);
        
        if (vg !== null) {
            
            var currency1Credit = vg.getCurrency1Cost();
            
            if (currency1Credit > 0) {
            
                player.credit1(currency1Credit);
                
                var request = new SparkRequests.ConsumeVirtualGoodRequest();
                request.quantity = 1;
                request.shortCode = shortCode;
                request.Send();
                
                Spark.setScriptData("currency1", player.getBalance1());
                break;
                
            }
            
        }
    }
    
}

 

Login to post a comment