question

robertgrospitch avatar image
robertgrospitch asked

PlayFabCloudScriptPlugin: Object reference Error With Azure Function

Hello All,

I just came back to using Playfab and was met with the new Azure Function process for running cloudscripts. I have reviewed all the documentation and done the example projects and am still having some issue writing a simple script. Below I am attempting to simply update the users statistics. A couple things I ran into when trying this out.

  1. I was forced to specify "Playfab.Servermodels." before "StatisticUpdate" due to there also being a "clientmodels.StatisticUpdate". It was giving me an "ambigious code" error. Is this causing issues?
  2. I can build the current code below fine, however, when I execute the function I get the following:
[2021-12-06T18:06:36.630Z] Executed 'StatUpdate' (Failed, Id=1e17aa13-ac17-492c-a688-e03ef3e04868, Duration=150ms)
[2021-12-06T18:06:36.633Z] System.Private.CoreLib: Exception while executing function: StatUpdate. PlayFabCloudScriptPlugin: Object reference not set to an instance of an object.

The cloudscript is written as the following:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using PlayFab;
using PlayFab.CloudScriptModels;
using PlayFab.ClientModels;
using PlayFab.ServerModels;
using System.Collections.Generic;
using PlayFab.Plugins.CloudScript;


namespace Blockchain.Gaming
{
    public static class StatUpdate
    {
        [FunctionName("StatUpdate")]
        public static async Task<dynamic> MakeApiCall(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log)
        {
           var context = await FunctionContext<dynamic>.Create(req);
           var args = context.FunctionArgument;

            //The playfab request through the SDK Models that are imported
            //This is PlayfabAllSDK and PlayfabCloudscriptingPLugin
           {
               var request = new PlayFab.ServerModels.UpdatePlayerStatisticsRequest
               {
                   PlayFabId = context.CurrentPlayerId,
                   Statistics = new List<PlayFab.ServerModels.StatisticUpdate>
                   {
                       new PlayFab.ServerModels.StatisticUpdate
                       {
                           StatisticName = "Level",
                           Value = 2
                    
                       }
                   }
               };
            var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);

            return await serverApi.UpdatePlayerStatisticsAsync(request);
           }

        }
    }
}

Is there anyone out there can help me out on this? I have been bashing my head into this code for 3 days trying to grasp Azure Functions for the first time.

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.

robertgrospitch avatar image robertgrospitch commented ·

Quick Edit, I added the Playfab.Samples cs file and still getting the same error when executing the function after build.

0 Likes 0 ·

1 Answer

·
Made Wang avatar image
Made Wang answered

We tried your code, and there was no error message like yours, but the following problems occurred during the deployment process, you can refer to:

1. No Set Secret Key

2. Not Found User

These two problems are caused by the function of FunctionContext, which comes from the tool PlayfabCloudscriptingPLugin. We recommend that you refer to the documentation and use CS2AFHelperClasses.cs to obtain the context to avoid the above errors.

Regarding "Playfab.Servermodels.", it is because you referenced "PlayFab.ClientModels", this reference is not needed in this function, you can delete it.

If you still have errors in the operation after modification, please provide more error information.

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.

robertgrospitch avatar image robertgrospitch commented ·

Thank you, I was able to resolve all my issues from this!

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.