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 06 at 05:14 AM · CloudScriptunity3dPlayer Inventory

GrantItemsToUser with Unity and Azure Functions can't make it work

I am trying for to figure out how to grant an item to the player from the client side using Azure Functions.

  1. I see on PlayStream that the function was triggered
  2. I see in Azure Portal the CloudScript was triggered
  3. I have the CloudScript function set up in PlayFab

Still the item that I try to grant doesn't go through for the player. Where do I make the mistake?

Unity:

public void CallCSharpExecuteFunction()
    {
        PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest()
        {
            Entity = new PlayFab.CloudScriptModels.EntityKey()
            {
                Id = PlayFabSettings.staticPlayer.EntityId, //Get this from when you logged in,
                Type = PlayFabSettings.staticPlayer.EntityType, //Get this from when you logged in
            },
            FunctionName = "GrantEquipmentToPlayer", //This should be the name of your Azure Function that you created.
            FunctionParameter = new Dictionary<string, object>() { { "id", "O1B5" } }, //This is the data that you would want to pass into your function.


            GeneratePlayStreamEvent = true //Set this to true if you would like this call to show up in PlayStream
        }, (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()}");
        });
    }

Cloud script:

public static class GrantEquipmentToPlayer
    {
        [FunctionName("GrantEquipmentToPlayer")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            dynamic args = context.FunctionArgument;
 
            string itemId = args["id"];
            
            List<string> itemIds = new List<string> { itemId };
            Dictionary<string, string> customData = new Dictionary<string, string> { 
               
            };
 
            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 GrantItemsToUserRequest
            {
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
                AuthenticationContext = authContext,
                ItemIds = itemIds
            };
 
            return await PlayFabServerAPI.GrantItemsToUserAsync(request, customData);
        }

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 Made Wang · Jun 06 at 10:07 AM

I tested your code, and it works. Have you created a new key-value pair for PLAYFAB_DEV_SECRET_KEY in the Azure portal? Refer to Configure function app settings in Azure Functions | Microsoft Docs. You can also test it locally, refer to Local debugging for Cloudscript using Azure Functions - PlayFab | Microsoft Docs. Also, are you getting any error messages in the Log stream?

Comment
eordoghdaniel

People who like this

1 Show 3 · 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 07 at 12:52 AM 0
Share

Thank you for the reply @Made Wang! I have "PLAYFAB_DEV_SECRET_KEY" setup in Azure Portal as well as "PLAYFAB_TITLE_ID".

I get in Unity the successful response on the call:

I see in Azure Portal as well the Functions getting executed:

My problem is still in PlayFab the Player doesn't receive the Item that I wanted to grant. It is not in the players inventory. Do I miss some fields from the request like "CatalogVersion"? Is that mandatory?

I am a little bit lost, it's nice to see that the call works and I get the successful response, but ultimately the goal is to grant the item.

screen-shot-2022-06-06-at-63849-pm.png (23.5 kB)
screen-shot-2022-06-06-at-64042-pm.png (60.3 kB)
avatar image eordoghdaniel · Jun 07 at 01:11 AM 0
Share

@Made Wang it's working now. I really appreciate your feedback it helped.

My issue was that I didn't add the CS2AFHelperClasses.cs to the Local build and redeploy the function app, but I simply added the CS2AFHelperClasses code to the GrantEquipmenToPlayer function.

As soon as I added to the Local version the supporting namespaces it started working!

avatar image Made Wang eordoghdaniel · Jun 07 at 07:03 AM 0
Share

Glad to know you solved this issue.

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

    12 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 avatar image

    Related Questions

    Bundles with random currencies 1 Answer

    Unity PUN2 Room creation error 1 Answer

    [UNITY] Help with returning/parsing results of ExecuteCloudScriptResult 1 Answer

    FunctionResult missing from ExecuteCloudScriptResult 2 Answers

    [VERY URGENT] Unity Matchmaking with Mirror cannot connect to server 2 Answers

    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