I am trying for to figure out how to grant an item to the player from the client side using Azure Functions.
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); }
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?
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.
@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!
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