question

Niclas Rosengaard Andreasen avatar image
Niclas Rosengaard Andreasen asked

Returning ItemInstanceID from Server.GrantItems

Hello 
Im trying to return and iteminstanceID of the current item that was JUST granted via script to player.
Cause i want to call another script that updates the current item with custom data.

So far i have been able to grant myself the item. But never see the returned ItemInstance.
    public void GrantVirtualItemViaPlayFabCloud(string ItemIdsName, string ItemCatalogVersionName)
    {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest
        {
            FunctionName = "grantVirtualItem",
            GeneratePlayStreamEvent = true,
            FunctionParameter = new
            {
              Catalogversion = ItemCatalogVersionName,
                ItemIds = ItemIdsName
            }


        },
        resultCallback => {
            Debug.Log(PlayFabSimpleJson.SerializeObject(resultCallback.FunctionResult));
            JsonObject jsonResult = (JsonObject)resultCallback.FunctionResult;
            object ItemInstanceID;
            ItemInstanceID = jsonResult.Values;
            Debug.Log(ItemInstanceID);
          
        },
        errorCallback => {
            Debug.Log(errorCallback.GenerateErrorReport());
        }
        );
    }

and cloud script

handlers.GrantVirtualItemToUser = function(args){
    
    var CatalogVersion = null;
    var ItemIds = null;
                 
    if (args && args.hasOwnProperty("CatalogVersion"))
        CatalogVersion = args.CatalogVersion;


    if (args && args.hasOwnProperty("ItemIds"))
        ItemIds = args.ItemIds;
    
    
    var addVirtualItem = server.GrantItemsToUser({
        PlayFabId: currentPlayerId,
        CatalogVersion: CatalogVersion,
        
        ItemIds: [ItemIds]
    });
    
    var ItemInstanceIDfromAddedItem = addVirtualItem[0].ItemInstanceId


    
    return addVirtualItem[0].ItemInstanceId; 
    
}


But im getting error in the result code. The first debug line result null and then it fails.

I need some help here

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

·
Citrus Yan avatar image
Citrus Yan answered

Referring to this GrantItemsToUsers API reference, you can see that it will return a list of GrantedItemInstance, therefore, in your CloudScript code, line 25, you’ll need write the following code to return the ItemInstanceId back to the clients:

return addVirtualItem.ItemGrantResults[0].ItemInstanceId;
2 comments
10 |1200

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

Niclas Rosengaard Andreasen avatar image Niclas Rosengaard Andreasen commented ·

Citrus How do Will i get the result inside Unity what code should i use to see the returned code??

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Niclas Rosengaard Andreasen commented ·

Using the code I posted, CloudScript will return the data with the following structure back to the clients:

 "data": {
        "FunctionName": "xxx",
        "Revision": 59,
        "FunctionResult": "C6D680xxxx916EC0",
        "Logs": [],
        "ExecutionTimeSeconds": 0.1259969,
        "ProcessorTimeSeconds": 0.0,
        "MemoryConsumedBytes": 16248,
        "APIRequestsIssued": 1,
        "HttpRequestsIssued": 0
    }

You can see that the value inside the FunctionResult property is simply a string, therefore, in your client code, change your resultCallback code to the following should work:

resultCallback => {

            Debug.Log("item instance id: " + result.FunctionResult.ToString());
        },
0 Likes 0 ·

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.