question

Ben avatar image
Ben asked

Get certain Context-Value via Azure Function

Hello, I have a small problem, I pass my string as example "Test" through GameStreamEvent to playfab, this triggers my azure function, then I want to add this string from the StreamEvent (Test) to User-Data, all is working and I can even see the "Test" String in "context.PlayStreamEventEnvelope.EventData...."

The last thing I tried was with context.FunctionArgument, but that isnt working too, I think it must be something with context.PlayStreamEventEnvelope.EventData, because when I use this all data is added to my player, but now I want that the azure function only adds the "test" field, but I have no idea how to access it, could someone please guide me in the right direction?

[FunctionName("PlayStreamEventHandler")]
        public static async Task Run([QueueTrigger("login-events", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
        {
            var context = JsonConvert.DeserializeObject<PlayerPlayStreamFunctionExecutionContext<dynamic>>(myQueueItem);
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");


            PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
            PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PlayFabDeveloperSecretKey");


            await UpdatePlayerData(log, context);
            
        }


        private static async Task UpdatePlayerData(ILogger log, PlayerPlayStreamFunctionExecutionContext<dynamic> context)
        {


            dynamic args = context.FunctionArgument;
            dynamic test = args["Test"];


        var updateUserDataRequest = new PlayFab.ServerModels.UpdateUserDataRequest
            {
                PlayFabId = context.PlayerProfile.PlayerId,
                
                Data = new Dictionary<string, string> {
                    {"Test", test}
                }
            };
            PlayFabResult<PlayFab.ServerModels.UpdateUserDataResult> result = await PlayFabServerAPI.UpdateUserDataAsync(updateUserDataRequest);
            HandleError(log, result);
        }
1 comment
10 |1200

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

Ben avatar image Ben commented ·

Here is how I start the new, custom event within Unity:

 PlayFabClientAPI.WritePlayerEvent(new WriteClientPlayerEventRequest()
            {
                Body = new Dictionary<string, object>() {
                            { "Test", test},
        },
                EventName = "Test"
            },
                     result => Debug.Log("Success"),
                     error => Debug.LogError(error.GenerateErrorReport()));
        }

0 Likes 0 ·

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

To access the custom data in the Event Data, you can refer to the code below.

string eventData = context.PlayStreamEventEnvelope.EventData;
JObject data = JObject.Parse(eventData);
string test = data["Test"].ToString();
10 |1200

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

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.