question

Eduardo Philipe Viana de Lima avatar image
Eduardo Philipe Viana de Lima asked

Why player avatar url gets null

So, first sorry for my english. I'm from Brazil.

particularly I'm still learning C #, then. I follow a tutorial where to make a leaderboard, and in this leaderboard I want to show the player avatar url of everyone who is in the Ranking. But when i play in a debug, it always returns null.

I once managed to show the avatar of the current player who logged in. But not from the leaderboard.

 void OnDisplayName(UpdateUserTitleDisplayNameResult result)
   {
 Debug.Log(result.DisplayName + "is your name");
   }
    private void OnLoginFailure(PlayFabError error)
    {
       var registerRequest = new RegisterPlayFabUserRequest {Email = email, Password = password, Username = username};
       PlayFabClientAPI.RegisterPlayFabUser(registerRequest, OnRegisterSuccess,OnRegisterFailure);
       errorRegister.gameObject.SetActive(true);
    }


private void OnRegisterFailure (PlayFabError error)
{


Debug.Log(error.GenerateErrorReport());
errorRegister.gameObject.SetActive(true);
}


public void getUserEmail(string emailIn)
{
    email = emailIn;
}
public void getUserPassword(string passwordIn)
{
    password = passwordIn;
}
public void getUsername(string usernameIn)
{
    username = usernameIn;
}


public void OnClickLogin ()
{
    var request = new LoginWithEmailAddressRequest {Email = email, Password = password};
    PlayFabClientAPI.LoginWithEmailAddress(request, OnLoginSuccess, OnLoginFailure);
}   
    




public int playerLevel;
public int playerRank;
public int playerMatch;




public void GetPlayerStats ()
{
    PlayFabClientAPI.UpdatePlayerStatistics( new UpdatePlayerStatisticsRequest {
    // request.Statistics is a list, so multiple StatisticUpdate objects can be defined if required.
    Statistics = new List<StatisticUpdate> {
    new StatisticUpdate { StatisticName = "Player Level", Value = 6 },
    new StatisticUpdate { StatisticName = "Player Rank", Value = 10 },
    new StatisticUpdate { StatisticName = "Player Matchs", Value = 1 },
    }
},
result => { Debug.Log("User statistics updated"); },
error => { Debug.LogError(error.GenerateErrorReport()); });
}




void GetStats()
{
    PlayFabClientAPI.GetPlayerStatistics(
        new GetPlayerStatisticsRequest(),
        OnGetStatistics,
        error => Debug.LogError(error.GenerateErrorReport())
    );
}


void OnGetStatistics(GetPlayerStatisticsResult result)
{
    Debug.Log("Received the following Statistics:");
    foreach (var eachStat in result.Statistics)
    {
     Debug.Log("Statistic (" + eachStat.StatisticName + "): " + eachStat.Value);


switch (eachStat.StatisticName)
{
    case "Player Level":
    playerLevel = eachStat.Value;
    break;
  
  case "Player Rank":
    playerRank = eachStat.Value;
    break;


case "Player Matchs":
    playerMatch = eachStat.Value;
    break;


}


    }
        
}
// Build the request object and access the API
public void StartCloudUpdatePlayerStats()
{
    PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
    {
        FunctionName = "UpdatePlayerStats", // Arbitrary function name (must exist in your uploaded cloud.js file)
        FunctionParameter = new { Level = playerLevel, Rank = playerRank, Match = playerMatch}, // The parameter provided to your function
        GeneratePlayStreamEvent = true, // Optional - Shows this event in PlayStream
    },  OnCloudUpdatePlayerStats, OnErrorShared);
}


// OnCloudHelloWorld defined in the next code block
    private  void OnCloudUpdatePlayerStats(ExecuteCloudScriptResult result) {
    // CloudScript returns arbitrary results, so you have to evaluate them one step and one parameter at a time
    Debug.Log(PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer));
    JsonObject jsonResult = (JsonObject)result.FunctionResult;
    object messageValue;
    jsonResult.TryGetValue("messageValue", out messageValue); // note how "messageValue" directly corresponds to the JSON values set in CloudScript
    Debug.Log((string)messageValue);
}


private static void OnErrorShared(PlayFabError error)
{
    Debug.Log(error.GenerateErrorReport());
}


    
    #region Leaderboards


    public void GetLeaderboard()
    {


      LeaderboardCanvas.SetActive(true);
        var requestLeaderboard = new GetLeaderboardRequest { StartPosition = 0, StatisticName = "Player Rank", MaxResultsCount = 50 };
        PlayFabClientAPI.GetLeaderboard(requestLeaderboard, OnGetLeaderboard, OnGetLeaderboardError);
       
    }
    


    public GameObject leaderboardListPrefab;
   public Transform verticalLeaderboardList;




    public void OnGetLeaderboard(GetLeaderboardResult result)
{
        //Debug.Log(result.Leaderboards[0].StatValue);




    foreach (PlayerLeaderboardEntry player in result.Leaderboard)
{
    
    GameObject tempListing = Instantiate (leaderboardListPrefab,verticalLeaderboardList);
    PlayFabLeaderboardList Leaderboard =  tempListing.GetComponent<PlayFabLeaderboardList>();
    Leaderboard.PlayerName.text = player.DisplayName;
    Leaderboard.PlayerPDL.text = player.StatValue.ToString();
    Debug.Log(player.Profile.AvatarUrl);
            






        }
        
        


    }
    
    /// <summary>
   
    /// </summary>


    public GameObject LeaderboardBackground;
public GameObject LeaderboardCloseButton;




    public void OnCloseLeaderboard()
{
LeaderboardCanvas.SetActive(false);
for (int i = verticalLeaderboardList.childCount -1; i>=0; i--)
{
  Destroy(verticalLeaderboardList.GetChild(i).gameObject);
}
DontDestroyOnLoad(this.LeaderboardBackground);
DontDestroyOnLoad(this.LeaderboardCloseButton);
}




   public void OnGetLeaderboardError(PlayFabError error)
   {
       Debug.LogError(error.GenerateErrorReport());
   }
   
    #endregion
    
Player DataLeaderboards and Statistics
10 |1200

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

Eduardo Philipe Viana de Lima avatar image
Eduardo Philipe Viana de Lima answered

@SethDu Thank you buddy, i have done this. And work!!! I could ask you for one more thing if it's not too much?. Could you give me an example how to use Update Avatar Url?

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.

Seth Du avatar image Seth Du ♦ commented ·

I have seen your use of ExecuteCloudScript API, it is correct and in terms of other APIs, they are very similar. Please always refer to the official API documentation. Here is the sample code:

PlayFabClientAPI.UpdateAvatarUrl(new UpdateAvatarUrlRequest()
{
    ImageUrl = "xxxxx"
},  
OnSuccess=>{}, 
OnFailed=>{});

If you are new to PlayFab, we highly recommend you to use Postman (RESTful API testing tools) so that you can learn how to craft the API request: https://docs.microsoft.com/en-us/gaming/playfab/sdks/postman/postman-quickstart

Please feel free to tell us if you have any other issues.

0 Likes 0 ·
Seth Du avatar image
Seth Du answered

To display the Avatar URL in the callback result of Leaderboard retrieval API, there are 3 things you may notice

  • Make sure the player has an Avatar URL (of course)
  • The Avatar URL option has been enabled in Client Profile Option so that other players will be able to view Avatar URL. You may navigate to [Game Manager] -> [Title Settings] -> [Client Profile Options] -> [ALLOW CLIENT ACCESS TO PROFILE PROPERTIES]
  • Add Profile Constraints for the API request. Here is an example request of GetLeaderboard:
{ 
"StatisticName": "Kills", 
"StartPosition": 0, 
"MaxResultsCount": 20, 
"ProfileConstraints":{ 
"ShowAvatarUrl":true  }  

}

Please feel free to tell us if there are any other issues when all the above configurations have been done.

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.

Eduardo Philipe Viana de Lima avatar image Eduardo Philipe Viana de Lima commented ·

@SethDu Hi, Seth thank you so much for your answer. Because my question was under admin review I found out by myself. But taking advantage, if not asking too much. Could you show me how I can do a request for update avatar Url? via client. I saw the api but I can't think properly

-1 Like -1 ·

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.