Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by eordoghdaniel · Jun 08 at 04:34 AM · CloudScriptunity3d

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.

screen-shot-2022-06-07-at-102519-pm.png (374.5 kB)
screen-shot-2022-06-07-at-102855-pm.png (165.6 kB)
Comment
eordoghdaniel

People who like this

1 Show 0
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Xiao Zha · Jun 08 at 08:29 AM

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.

Comment
eordoghdaniel

People who like this

1 Show 1 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eordoghdaniel · Jun 08 at 03:12 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    11 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    FunctionResult missing from ExecuteCloudScriptResult 2 Answers

    Cloud Script FunctionResult Returns Null 2 Answers

    Server/RedeemMatchmakerTicket working on Unity Editor, not in Android device 1 Answer

    Store with random items 1 Answer

    "RaiseEvent() failed. " warning occurred in Unity 1 Answer

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges