Sign In Register

How can we help you today?

Start a new topic
Answered

An efficient way update current virtual goods list and remove products player already has max quantity of?

ok so i currently have my store inventory being populated by ListVirtualGoodsRequest, is there any particular methods used to have the products the player already has a max quantity of automatically disable the UI Buttons "intractable" tab for that product?


I have an idea as far as maybe using AccountDetailsRequest to create a list and somehow disable all of the gameobject relative to the same ShortCode but I honestly have no idea how to write it in C#..


thanks in advance,

-CJ


Best Answer

Morning guys,


I've quickly put something together for you:


 

//Send AccountDetailsRequest with the player that called this Event, and get their VirtualGoods
var vgs = new SparkRequests.AccountDetailsRequest().Send().virtualGoods;

// If the player has Virtual Goods
if(vgs){
    
    //Iterate each Virtual Good
    for(var i in vgs){
    
        //If the current ammount is equal to the maxQuantity
        if(Spark.getConfig().getVirtualGood(i).getMaxQuantity() == vgs[i]){
            //Disable UI here
        }
    }
}

Create a custom event, put this code in the Cloud Code>Events script, log in with a player in Test Harness and call this event. I've commented what's happening where but you might want to step through the code using the debugger so you can see how it works.


Regards,


Mantas Danilovas


You're on the right track with ListVirtualGoodsRequest and AccountDetailsRequest. On the client side, instead of making 2 calls when you display your store(first being ListVirtualGoodsRequest and second being AccountDetailsRequest), create a new event in Cloud Code that does it for you, and returns if the good(s) should be interactable. Your cloud code should do this:
1. Get ListVirtualGoodsRequest
2. Get AccountDetailsRequest
3. For each good, set a field in the return object that determines if the button is clickable.

Was this in line with what you were asking?

 

OMG! yes! you tookthe words right out of my head! lol do you have an example script i could reference Davandra? Im still trying to wrap my head around this cloud coding thing.

Answer

Morning guys,


I've quickly put something together for you:


 

//Send AccountDetailsRequest with the player that called this Event, and get their VirtualGoods
var vgs = new SparkRequests.AccountDetailsRequest().Send().virtualGoods;

// If the player has Virtual Goods
if(vgs){
    
    //Iterate each Virtual Good
    for(var i in vgs){
    
        //If the current ammount is equal to the maxQuantity
        if(Spark.getConfig().getVirtualGood(i).getMaxQuantity() == vgs[i]){
            //Disable UI here
        }
    }
}

Create a custom event, put this code in the Cloud Code>Events script, log in with a player in Test Harness and call this event. I've commented what's happening where but you might want to step through the code using the debugger so you can see how it works.


Regards,


Mantas Danilovas


1 person likes this

Awesome! thank you Mantas!!


-CJ

Yup Mantas nailed it. Here's more of what I was thinking of, in which the cloud code returns all your items, and if they are clickable (interactable).

 

// request all the available virtual goods
// the response is something like this
//  [
//   {
//     "shortCode": "VG_First",
//     "currency1Cost": 1,
//     "maxQuantity": 1,                << NOTE: only displayed if you set the max quantity
//     "description": "This is my first Virtual Good",
//     "name": "First Virtual Good"
//   },
//   {
//     "shortCode": "VG_Second",
//     "currency1Cost": 0,
//     "currency2Cost": 1,
//     "description": "This is my second Virtual Good",
//     "name": "Second Virtual Good"
//   }
//  ]
var availableVirtualGoods = Spark.sendRequest(new SparkRequests.ListVirtualGoodsRequest()).virtualGoods;

// get all owned virtual goods
// the response is something like this
//  {
//   "VG_First": 1,
//   "VG_Second": 5
//  }
var ownedVirtualGoods = Spark.getPlayer().getVirtualGoods();

// loop through each available virtual good to set it's interactability
for (i = 0; i < availableVirtualGoods.length; i++) {
    var virtualGood = availableVirtualGoods[i];
    
    // if there is a maxQuantity field, and the player has already purchased it, and the player has the max quantity, set the iteractability to false
    if (virtualGood.maxQuantity !== null && ownedVirtualGoods[virtualGood.shortCode] == virtualGood.maxQuantity) {
        virtualGood.interactable = false;
    } else {
        virtualGood.interactable = true;
    }
}

Spark.setScriptData("result", availableVirtualGoods);

// you response will be something like this
// {
//  "@class": ".LogEventResponse",
//  "scriptData": {
//   "result": [
//   {
//     "shortCode": "VG_First",
//     "currency1Cost": 1,
//     "maxQuantity": 1,
//     "description": "This is my first virtual good",
//     "name": "First Virtual Good",
//     "interactable": false
//   },
//   {
//     "shortCode": "VG_Second",
//     "currency1Cost": 0,
//     "currency2Cost": 1,
//     "description": "This is my second Virtual Good",
//     "name": "Second Virtual Good",
//     "interactable": true
//   }
//   ]
//  }
// }

 

 

Sorry for all of the questions, but whats a good way to disable the UI Button from the cloud? took a moment to try and figure it out myself but i couldn't find documentations  on on it..


-CJ

Actually there is no good way or bad way, it is only how to code it to get the result.

ooo sorry Davendra i didn't see your example until after i sent that last reply! lol it makes since now. going to apply your method now! :)


-CJ

ok last question and i think I will officially have this problem solved... Im trying to apply Davendra's method but cant seem to write the string correctly in C#. can you tell me what im doing wrong with the second line:


new LogEventRequest_OWNED().Send((response) => {

   foreach (var entry in response.ScriptData)

   {

      virtualGoodEntry.GetComponent<SHOPVGButton>().TitleString = entry.Name.ToString();

      virtualGoodEntry.GetComponent<SHOPVGButton>().CostString = entry.Currency1Cost.ToString();

   }

});


Hi, this is your solution about your question:  

new LogEventRequest_OWNED().Send((LogEventResponse response) => {
				if(!response.HasErrors){
					List<object> entryList = response.ScriptData.GetObjectList("result") as List<object>;
					for(int i = 0;i < entryList.Count; i++) {
						Dictionary<string, object> entry = entryList[i] as Dictionary<string, object>;
						virtualGoodEntry.GetComponent<SHOPVGButton>().TitleString = Convert.ToString(entry["name"]);
						virtualGoodEntry.GetComponent<SHOPVGButton>().CostString = Convert.ToString(entry["currency1Cost"]);
					}
				}

			});

 


that worked perfectly Hong!! thank you all so much!! I've been struggling with this shop section for almost 2 weeks! I'm finally worthy of a good nights sleep again! haha


-CJ

Login to post a comment