question

momanfawozi avatar image
momanfawozi asked

Trouble Retrieving Player Display Names Using PlayFab CloudScript in Unity (PLayer)

I'm currently developing a game in Unity and using PlayFab for backend services. I'm trying to retrieve player display names from a specific segment using PlayFab CloudScript, but I'm encountering issues with the implementation. Here's the CloudScript function I'm using:

 handlers.GetPlayersInSegment = function (args, context) {
     var segmentId = "BD37826336FD4711"; // My actual segment ID
    
     var playerDisplayNames = [];
     var segmentMembers = server.GetPlayersInSegment({ SegmentId: segmentId }).Profiles;
    
     var limit = Math.min(20, segmentMembers.length);
     for (var i = 0; i < limit; i++) {
         var profile = segmentMembers[i];
         playerDisplayNames.push(profile.DisplayName);
     }
    
     return { playerDisplayNames: playerDisplayNames };
 };

In my Unity C# script, I'm calling this CloudScript function using PlayFabClientAPI.ExecuteCloudScript, but I'm getting a "Failed to retrieve player display names. FunctionResult is null" error in the OnCloudScriptSuccess callback.

Here's the relevant C# code:

 private void SearchForPlayer()
 {
     string segmentId = "BD37826336FD4711"; // My actual segment ID
     PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest
     {
         FunctionName = "GetPlayersInSegment",
         FunctionParameter = new Dictionary<string, object> { { "segmentId", segmentId } }
     }, OnCloudScriptSuccess, OnCloudScriptFailure);
 }
    
 private void OnCloudScriptSuccess(ExecuteCloudScriptResult result)
 {
     var functionResult = result.FunctionResult as Dictionary<string, object>;
     if (functionResult != null && functionResult.ContainsKey("playerDisplayNames"))
     {
         var displayNames = functionResult["playerDisplayNames"] as List<object>;
         List<string> playerNames = new List<string>();
         foreach (var displayName in displayNames)
         {
             playerNames.Add(displayName.ToString());
         }
         Debug.Log("Player Display Names: " + string.Join(", ", playerNames));
     }
     else
     {
         Debug.LogError("Failed to retrieve player display names.");
     }
 }
    
 private void OnCloudScriptFailure(PlayFabError error)
 {
     Debug.LogError("CloudScript call failed: " + error.ErrorMessage);
 }

I've followed the PlayFab documentation and examples, but I can't seem to get it working. Any help or suggestions on what might be causing this issue would be greatly appreciated. Thank you.

apis
10 |1200

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

1 Answer

·
Neils Shi avatar image
Neils Shi answered

In your cloudscript code, you're trying to get the “Profiles” property from the results of API GetPlayersInSegment, but according to Play Stream - Get Players In Segment - REST API (PlayFab Server) | Microsoft Learn, the “Profiles” property is not included in the results of GetPlayersInSegment. What you should get is the “PlayerProfiles” property, which contains information about the players in the segment. So, you should modify your cloudscript code to “var segmentMembers = server.GetPlayersInSegment({ SegmentId: segmentId }).PlayerProfiles;”. That’s why your “FunctionResult” is null. In addition, I also tested your OnCloudScriptSuccess method, and you convert the FunctionResult to Dictionary, which will cause the conversion to fail. You may modify your code to "var functionResult = result. FunctionResult as IDictionary<string, object>; ”.

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.

momanfawozi avatar image momanfawozi commented ·

It works perfectly thank very much for your help

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.