question

kendrickvillaruel avatar image
kendrickvillaruel asked

Cast List from an ExecuteCloudscript return

Hi, my intention is to include the catalog items as a return for cloudScript execution.

In my cloudscript, I have included this lines of code with Catalog as key and the List of CatalogItems as Value.

var cat = server.GetCatalogItems(
    CatalogVersion = "Upgrade"
  )
  element["Catalog"] = cat.Catalog;
return element;

So the cloudscript returns like this. A Catalog item, including the other initialization I needed.

{
  "Catalog": [
    {
      "ItemId": "V1GreenFirePower",
      "ItemClass": "V1GreenStat",
      "CatalogVersion": "Upgrade",
      "DisplayName": "V1Green FirePower",
      "VirtualCurrencyPrices": {
        "VC": 10
      },
      "RealCurrencyPrices": {},
      "Tags": [],
      "CustomData": "{\"Val\":\"[1,1.5,1.8,2.1,2.5]\",\"Prices\":\"[0,20,30,40,60]\"}",
      "Consumable": {},
      "CanBecomeCharacter": false,
      "IsStackable": true,
      "IsTradable": false,
      "IsLimitedEdition": false,
      "InitialLimitedEditionCount": 0
    },
    {
      "ItemId": "V1GreenShield",
      "ItemClass": "V1GreenStat",
      "CatalogVersion": "Upgrade",
      "DisplayName": "V1Green Shield",
      "VirtualCurrencyPrices": {
        "VC": 10
      },
      "RealCurrencyPrices": {},
      "Tags": [],
      "CustomData": "{\"Val\":\"[2,3,4,5,6]\",\"Prices\":\"[0,15,25,35,50]\"}",
      "Consumable": {},
      "CanBecomeCharacter": false,
      "IsStackable": true,
      "IsTradable": false,
      "IsLimitedEdition": false,
      "InitialLimitedEditionCount": 0
    },
    {
      "ItemId": "V1GreenArmor",
      "ItemClass": "V1GreenStat",
      "CatalogVersion": "Upgrade",
      "DisplayName": "V1Green Armor",
      "VirtualCurrencyPrices": {
        "VC": 10
      },
      "RealCurrencyPrices": {},
      "Tags": [],
      "CustomData": "{\"Val\":\"[2,3,4,5,6]\",\"Prices\":\"[0,10,20,30,50]\"}",
      "Consumable": {},
      "CanBecomeCharacter": false,
      "IsStackable": true,
      "IsTradable": false,
      "IsLimitedEdition": false,
      "InitialLimitedEditionCount": 0
    }
  ],
  "Ammo": "100",
  "Gender": "None",
  "SkinTone": "None",
  "Skin": "None"
}

My question is, how can I cast it in the client side?

I've tried a bunch of approach listed below

var asd = PlayFabSimpleJson.DeserializeObject<List<CatalogItem>>((string)result["Catalog"]);
var asd = (List<CatalogItem>)result["Catalog"];

But I am not getting anywhere.

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
JayZuo avatar image
JayZuo answered

Since your result is returned from Cloud Script, I'd assume you are using ExecuteCloudScript as the following:

PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest
{
    FunctionName = "xxxx",
    GeneratePlayStreamEvent = true
},
result =>
{
    if (result.Error == null)
    {
        var functionResult = result.FunctionResult;
    }
    else
    {
        Debug.LogError(result.Error.StackTrace);
    }
},
error => { Debug.LogError(error.GenerateErrorReport()); });

In this case, both of your code won't work as your Cloud Script function returns a object and PlayFab SDK will automatically deserialize the object into "result.FunctionResult". So, you should be able to get the list of catalog items as the following:

var jsonResult = result.FunctionResult as PlayFab.Json.JsonObject;
var catalog = jsonResult["Catalog"];

Forced conversion as "(List<CatalogItem>) catalog" will give you an error as when PlayFab SDK deserializes FunctionResult, it can only deserializes to common object. To get a list of "CatalogItem", you will have to do something like the following:

PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest
{
    FunctionName = "xxx",
    GeneratePlayStreamEvent = true
},
result =>
{
    if (result.Error == null)
    {
        var jsonResult = result.FunctionResult as PlayFab.Json.JsonObject;
        //catalog will be List<object> 
        var catalog = jsonResult["Catalog"];

        var serializer = PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer);
        //Serialize to string fist
        var catString = serializer.SerializeObject(catalog);
        //Deserialize to List<CatalogItem>
        var asd = serializer.DeserializeObject<List<CatalogItem>>(catString);
    }
    else
    {
        Debug.LogError(result.Error.StackTrace);
    }
},
error => { Debug.LogError(error.GenerateErrorReport()); });
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.