question

eordoghdaniel avatar image
eordoghdaniel asked

CloudScript: Updating custom data on Player's inventory item with Azure Functions in Unity

1) I grant an item to the player with "GrantItemsToUserRequest"

2) After this I take from the response the ItemInstanceId to be able to immediately write some custom data to the granted equipment

3) The request is going through but the custom data doesn't appear on the item

Could you please help?

Azure function:

public static class UpdateUserInventoryEquipmentData
    {
        [FunctionName("UpdateUserInventoryEquipmentData")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            dynamic args = context.FunctionArgument;
 
            string itemInstanceID = args["instanceID"];
            
            Dictionary<string, string> customData = new Dictionary<string, string> { 
                { "equipped", "false" },
            };
 
            PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process);
            PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
            
            var authContext = new PlayFabAuthenticationContext
            {
                EntityToken = context.TitleAuthenticationContext.EntityToken
            };
 
            var request = new UpdateUserInventoryItemDataRequest
            {
                ItemInstanceId = itemInstanceID,
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
                Data = customData
            };

            return new OkObjectResult(request);
        }
    }

Unity call for the custom data update:

public void CallCloudScriptUpdateUserInventoryEquipmentData(string itemInstanceID)
    {
        PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest()
        {
            Entity = new PlayFab.CloudScriptModels.EntityKey()
            {
                Id = PlayFabSettings.staticPlayer.EntityId, 
                Type = PlayFabSettings.staticPlayer.EntityType,
            },
            FunctionName = "UpdateUserInventoryEquipmentData",
            FunctionParameter = new Dictionary<string, object>() {
                { "instanceID", itemInstanceID }
            },


            GeneratePlayStreamEvent = true
        }
        , (ExecuteFunctionResult result) =>
        {
            if (result.FunctionResultTooLarge ?? false)
            {
                Debug.Log("This can happen if you exceed the limit that can be returned from an Azure Function, See PlayFab Limits Page for details.");
                return;
            }
            Debug.Log($"The {result.FunctionName} function took {result.ExecutionTimeMilliseconds} to complete");
            Debug.Log($"Result: {result.FunctionResult.ToString()}");
        }
        , (PlayFabError error) =>
        {
            Debug.Log($"Opps Something went wrong: {error.GenerateErrorReport()}");
        });
    }

I have the CloudScript configured in PlayFab:

Azure Portal is properly set up.

After the call I get response:

{"characterId":null,"customTags":null,"data":{"equipped":"false"},"itemInstanceId":"6281XXXXXXXA2E0","keysToRemove":null,"playFabId":"82XXXXXXXX1DD","authenticationContext":null}

At the end I don't have the custom data updated in the inventory:

What am I doing wrong? Any help is appreciated.

unity3dCloudScript
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

·
Xiao Zha avatar image
Xiao Zha answered

When you create the UpdateUserInventoryItemDataRequest, you may need to use

var result = await PlayFabServerAPI.UpdateUserInventoryItemCustomDataAsync(request)

to actually execute request. For more about Azure Function, please refer to this documentation.

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.

eordoghdaniel avatar image eordoghdaniel commented ·

It is working like a charm. Much appreciated @Xiao Zha!!

1 Like 1 ·
Travis Mosier avatar image Travis Mosier commented ·

@Xiao Zha I know its been a minute since this was posted but can you expand on this example a little more? This is the closest example I could find to use an Azure Function to update my player's inventory items but I'm getting the same error. Not sure how I'm supposed to utilize the "var result ...." you answered with.

Would help me out tremendously!

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.