question

brian-kircher avatar image
brian-kircher asked

Get remote PlayFabPlayer's display name

Using the PlayFab Party SDK for Unity, how can I get a remote player's display name? (e.g., for a lobby UI).

I'm trying to retrieve the display name using the enitity key and type on the remote player's PlayFabPlayer object along with the local player's session ticket, but the GetPlayerCombinedInfo API always returns the display name of the local player, even when passing the entity key and type of the remote player.

    
    private void OnNetworkJoined(object sender, string networkId)
    {
        var player = PlayFabMultiplayerManager.Get().LocalPlayer;
        AddPlayer(player);
    }


    private void OnRemotePlayerJoined(object sender, PlayFabPlayer player)
    {
        AddPlayer(player);
    }

    private void AddPlayer(PlayFabPlayer player)
    {
        var sessionTicket = FindObjectOfType<UserInfo>().LoginResult.SessionTicket;


        Debug.Log(player.EntityKey.Id + " - " + player.EntityKey.Type);


        PlayFabClientAPI.GetPlayerCombinedInfo(
            new GetPlayerCombinedInfoRequest
            {
                AuthenticationContext =
                    new PlayFabAuthenticationContext
                    {
                        EntityId = player.EntityKey.Id,
                        EntityType = player.EntityKey.Type,
                        EntityToken = player._entityToken,
                        ClientSessionTicket = sessionTicket
                    },
                InfoRequestParameters = new GetPlayerCombinedInfoRequestParams { GetUserAccountInfo = true }
            },
            (GetPlayerCombinedInfoResult result) =>
            {
                var username = result.InfoResultPayload.AccountInfo.TitleInfo.DisplayName;
                username = username ?? player._platformSpecificUserId;
                _playerNames.Add(player, username);
                OnPlayerAdded.Invoke(username);
            },
            (PlayFabError error) =>
            {
                Debug.LogError("Error retrievering username for player.");
                Debug.LogError(error);
            });
    }
unity3d
10 |1200

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

Seth Du avatar image
Seth Du answered

Sorry for the confusion in the previous replies. The reason why you get NotAuthorized is due to the default Entity Global Policy, meanwhile I have changed the default policy, which shows a different result.

I have reconsidered your scenario, if you have a title player account entity token, by default, GetProfile/GetProfiles API can only return the caller self's Profile. Here are 2 workaround solutions.

  1. Write the GetProfile/GetProfiles APIs in Cloud Script or Azure Function. Cloud Script holds a title-level entity token, which has the permission to get any entity profiles. In addition, you may selectively only return master player account ID/ PlayFab ID, which is a secure way.
  2. Change the default policy like I do, which is not recommended because my title is only for testing. I add a policy at the Entity Global Title Policy:
  {
    "Action": "*",
    "Effect": "Allow",
    "Resource": "pfrn:data--*!*/Profile/*",
    "Principal": "*",
    "Comment": "Allow all profile access",
    "Condition": null
  }
2 comments
10 |1200

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

Seth Du avatar image Seth Du ♦ commented ·

Here is a Cloud Script sample:

handlers.getPlayFabIDfromEntity = function(args,context) {
    if(!(args.hasOwnProperty("Id"))){
        return null;
    }
    
    var request = {
          "Entity": {
              "Id": args.Id,
              "Type": "title_player_account",
              "TypeString": "title_player_account"
              }
    };
    
    
    var result = entity.GetProfile(request);


    return result.Profile.Lineage.MasterPlayerAccountId;
}

0 Likes 0 ·
brian-kircher avatar image brian-kircher Seth Du ♦ commented ·

Thanks a lot! I added the profile access and that worked. I can now see the remote players display name. Will give the cloud script a try in the future.

0 Likes 0 ·
Seth Du avatar image
Seth Du answered
  • The AuthenticationContext seems to be unnecessary in your scenario. I am not sure of the rest of your code but If you have implemented PlayFab SDK, usually AuthenticationContext is used when you what to call the API for another player besides of currently logged in player.
  • I have modified the code, please use ProfileConstraints to get display name
PlayFabClientAPI.GetPlayerCombinedInfo(
    new GetPlayerCombinedInfoRequest
    {
        //target player you want to get
        PlayFabId = "xxxxxxxxxxx",
        InfoRequestParameters = new GetPlayerCombinedInfoRequestParams {
            GetUserAccountInfo= true,
            
            ProfileConstraints = new PlayerProfileViewConstraints
            {
                ShowDisplayName = true
            }
        }
    },
    onSuccess=> { },
    OnFailed=> { });
5 comments
10 |1200

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

brian-kircher avatar image brian-kircher commented ·

Thanks! I guess the question then is how do I get the PlayFabId of the "target"/remote player?

The scenario here is using the Party SDK to listen to remote player joined events. The PlayFabPlayer class it provides as the arg to that event doesn't have a PlayFabId property, but does have an entity key for the title_player_account.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ brian-kircher commented ·

If I understand correctly. Your requirement is to retrieve master player account ID(PlayFab ID) from Entity key, which is title player account.

You may need to add additional API GetProfile call to get the master player account ID.

0 Likes 0 ·
brian-kircher avatar image brian-kircher Seth Du ♦ commented ·

Ok, I wrapped the GetPlayerCombinedInfo in an intial call to GetProfile, but I'm getting unauthorized in the response when accessing the remote player profile (but not local), e.g.

private void AddPlayer(PlayFabPlayer player)
{
    PlayFabProfilesAPI.GetProfile(
    	new GetEntityProfileRequest
        {
            Entity = new PlayFab.ProfilesModels.EntityKey { Id = player.EntityKey.Id, Type = player.EntityKey.Type }
        },
        (GetEntityProfileResponse result) =>
        {
            var playfabId = result.Profile.Lineage.MasterPlayerAccountId;

            PlayFabClientAPI.GetPlayerCombinedInfo(...);
        },
        (PlayFabError error) =>
        {
            Debug.LogError(error);
        });
}

{"code":401,"status":"Unauthorized","error":"NotAuthorized","errorCode":1089,"errorMessage":"NotAuthorized"}

EnityKey is formed from the PlayFabPlayer specified in the callback of RemotePlayerJoined and corresponds to the title_player_account of the remote player.

0 Likes 0 ·
Show more comments
Show more comments

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.