Documentation

Did you find this helpful?

Getting Player Profiles

This tutorial walks you through how to use the GetPlayerProfile call to get a player’s profile with PlayerProfileViewConstraints properties that are enabled from the your title’s setting. Specifically, we will be getting a player’s profile, created date, and last login time.

Requirements

For this tutorial, basic knowledge of how to create a player for your title is needed so that you can perform a GetPlayerProfile on the player. Refer to the Getting Started with PlayFab which will run you through the process of creating a player for the title.

It is also worthwhile to read the Game Manager Tour if you are unfamiliar with the Game Manager as it is the place where we configure profile constraints.

Step 1 - Create a user with a display name

The first step is to create a player and add a display name to the user. This example will create a new user with the display name "UnicornTossMaster".

void CreatePlayerAndUpdateDisplayName() {
    PlayFabClientAPI.LoginWithCustomID( new LoginWithCustomIDRequest {
        CustomId = "PlayFabGetPlayerProfileCustomId",
        CreateAccount = true
    }, result => {
        Debug.Log("Successfully logged in a player with PlayFabId: " + result.PlayFabId);
        UpdateDisplayName();
    }, error => Debug.LogError(error.GenerateErrorReport()));
}

void UpdateDisplayName() {
    PlayFabClientAPI.UpdateUserTitleDisplayName( new UpdateUserTitleDisplayNameRequest {
        DisplayName = "UnicornTossMaster"
    }, result => {
        Debug.Log("The player's display name is now: " + result.DisplayName);
    }, error => Debug.LogError(error.GenerateErrorReport()));
}

The response should show "Successfully logged in a player with PlayFabId: SOME_PLAYFAB_ID” followed by "The player's display name is now: UnicornTossMaster"

Step 2 - Making a player profile call

Now that there is a player to call get player profile for, the next step is to makes a player’s most basic profile.

C# Example

In the following C# example is a basic GetPlayerProfile call

void GetPlayerProfile(string playFabId) {
    PlayFabClientAPI.GetPlayerProfile( new GetPlayerProfileRequest() {
        PlayFabId = playFabId,
        ProfileConstraints = new PlayerProfileViewConstraints() {
            ShowDisplayName = true
        }
    }, 
    result => Debug.Log("The player's DisplayName profile data is: " + result.PlayerProfile.DisplayName),
    error => Debug.LogError(error.GenerateErrorReport()));
}

In the response there be a PlayerProfileModel object which will contain the display name UnicornTossMaster for the player.

Step 3 - Attempting to get player created time and last login time

In the Player Profile Model there is a fair amount of data about the player. In the previous step, a GetPlayerProfile was issued and the response that was received only contained display name information. The next step is to get even more profile data for the player.  To do so we will call GetPlayerProfile with additional fields in the ProfileConstraints request parameter.

C# Example

The following C# example modifies the GetPlayerProfile method from step 2 and calls GetPlayerProfile to request the Created and LastLogin fields from the PlayerProfileModel. However, this will be an unsuccessful call at this step!!!

void CreatePlayerAndUpdateDisplayName(string playFabId) {
    PlayFabClientAPI.GetPlayerProfile( new GetPlayerProfileRequest() {
        PlayFabId = playFabId,
        ProfileConstraints = new PlayerProfileViewConstraints {
            ShowDisplayName = true,
            ShowCreated = true,
            ShowLastLogin = true
        }
    }, 
    result => Debug.Log("The player's profile Created date is: " + result.PlayerProfile.Created),
    error => Debug.LogError(error.GenerateErrorReport()));
}

This call should result in an error with an error code 1303 RequestViewConstraintParamsNotAllowed and an error message stating that there are “Invalid view constraints” with a JSON output of constraints the title currently has set. The error message is because we have not yet configured the ability to show Created and LastLogin in our title’s profile constraint settings.

Step 4 - Configuring player profile view constraints for the title

In order to get more data from the PlayerProfileModel when we call the GetPlayerProfile API, we will need to configure the constraints on the data to be available. These settings are in the Title’s settings in game manager.

To configure constraints for the title, in Game Manager, go to Settings -> Client Profile Options. Notice that under "ALLOW CLIENT ACCESS TO PROFILE PROPERTIES:" that only “Display name” is checked, which allowed us to get the display name in step 1.

Now to enable both Created and LastLogin from the PlayerProfileModel, check “Creation date” and “Last login time”, then click on SAVE CLIENT PROFILE OPTIONS.

Step 5 - Getting player created time and last login time

Now, we will go back to the GetPlayerProfile call in step 3.

Executing the same code again will now return a successful PlayerProfileModel with data on the user’s creation time, last login, and the display name “UnicornTossMaster”.

Step 6 - Getting player profile via Login operation

In most cases you will want to get certain player profile data as soon as player is logged in. PlayFab API allows to combine those 2 calls into one. The following example shows how to gain Profile information through Login request and uses LoginWithCustomId as an example. Please note, this works with every login mechanism:

void CreatePlayerAndUpdateDisplayName(string customId) {
    PlayFabClientAPI.LoginWithCustomID( new LoginWithCustomIDRequest() {
        CustomId = customId,
        // Define info request parameters
        InfoRequestParameters = new GetPlayerCombinedInfoRequestParams() {
            // And make sure PlayerProfile is included
            GetPlayerProfile = true,
            // Define rules for PlayerProfile request
            ProfileConstraints = new PlayerProfileViewConstraints() {
                // And make sure that both AvatarUrl and LastLogin are included.
                ShowAvatarUrl = true,
                ShowLastLogin = true,
            }
        }
    },
    result =>
    {
        // Extract the data you have requested
        var avatarUrl = result.InfoResultPayload.PlayerProfile.AvatarUrl;
    },
    error => Debug.LogError(error.GenerateErrorReport()));
}


Did you find this helpful?