question

Avinaya Banskota avatar image
Avinaya Banskota asked

I am making a 2 player (Versus) game.How do I upload each player's score in the leaderboard when playing from the same device?

Leaderboards 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.

Sarah Zhang avatar image
Sarah Zhang answered

You can use two players’ authenticationcontext to construct two PlayFabClientInstanceAPI object, then use these API instances to call the API for two players separately. Besides, for the security reasons, we would suggest you call the server API UpdatePlayerStatistics on CloudScript and use ExecuteCloudScript on client to execute the corresponding functions. You can refer to the following code to construct the API instances for two players.

public class PlayFabLogin : MonoBehaviour
{
    public PlayFabClientInstanceAPI player01;
    public PlayFabClientInstanceAPI player02;
 
    public void Start()
    {
       player01 = new PlayFabClientInstanceAPI();
       player02 = new PlayFabClientInstanceAPI();
       LoginPlayer01();
       LoginPlayer02();
    }

    public void LoginPlayer01() 
    {
        var request = new LoginWithCustomIDRequest() { CustomId = "Player01", CreateAccount = true };
        player01.LoginWithCustomID(request, OnLoginSuccess, OnFailure);
    }

    public void LoginPlayer02()
    {
        var request = new LoginWithCustomIDRequest() { CustomId = "Player02", CreateAccount = true };
        player02.LoginWithCustomID(request, OnLoginSuccess, OnFailure);
    }
    private void OnLoginSuccess(LoginResult result)
    {
        Debug.Log(result.PlayFabId);
    }

    private void OnFailure(PlayFabError error)
    {
        Debug.LogError(error.GenerateErrorReport());
    }
}
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.

Avinaya Banskota avatar image Avinaya Banskota commented ·

Thank you very much.I will try it and see.

0 Likes 0 ·
Avinaya Banskota avatar image
Avinaya Banskota answered

Hi,

So my test game is- there are 2 buttons each for player1 and player 2.The number of times a button is pressed = score ++ for that player.I want the player1 and player2 scores to upload in the 2 leaderboards.

I tried incorporating the code you had given.

The error I get when the below code is run is:

Failed to send session data. Error: /Event/WriteEvents: The requesting entity is not authorized to write events with entity title_player_account!(Account number).

This is my code:

public class PlayerScoreTest : MonoBehaviour
{
    public static PlayerScoreTest playerScoreTest;

    public int scoreP1=0;
    public int scoreP2=0;

    public PlayFabClientInstanceAPI player1;
    public PlayFabClientInstanceAPI player2;



    void OnEnable()//simple singleton
    {
        if(PlayerScoreTest.playerScoreTest==null)
        {
            PlayerScoreTest.playerScoreTest=this;
        }
        else if(PlayerScoreTest.playerScoreTest !=this)
        {
            Destroy(this.gameObject);
        }
        DontDestroyOnLoad(this.gameObject);
    }



    // Start is called before the first frame update
    void Start()
    {
        player1=new PlayFabClientInstanceAPI();
        player2=new PlayFabClientInstanceAPI();
        LoginPlayer01();
        LoginPlayer02();


        //Note: Setting title Id here can be skipped if you have set the value in Editor Extensions already.
        if (string.IsNullOrEmpty(PlayFabSettings.TitleId))//if the id section of the PlayFab setting is empty then add the id
        {
            PlayFabSettings.TitleId = "XXXXX"; // this should be the titleId from PlayFab Game Manager in the editor window
        }


#if UNITY_ANDROID
        var requestAndroid= new LoginWithAndroidDeviceIDRequest {AndroidDeviceId= ReturnMobileID(),CreateAccount=true};//the CreateAccount=true automatically registers the device
        PlayFabClientAPI.LoginWithAndroidDeviceID(requestAndroid,OnLoginMobileSuccess,OnLoginMobileFailure);
#endif
#if UNITY_IOS
        var requestIOS= new LoginWithIOSDeviceIDRequest { DeviceId=ReturnMobileID(),CreateAccount=true};
        PlayFabClientAPI.LoginWithIOSDeviceID(requestIOS,OnLoginMobileSuccess,OnLoginMobileFailure);
#endif
    }

    public static string ReturnMobileID()
    {
        string deviceID=SystemInfo.deviceUniqueIdentifier;
        return deviceID;
    }
    
    private void OnLoginMobileSuccess(LoginResult result)//this is specific for mobile login success
    {
        Debug.Log("Congratulations, you made your first successful API call!");
    }

    private void OnLoginMobileFailure(PlayFabError error)//if the login is unsuccessful then we take steps to register the player
    {
        Debug.LogError(error.GenerateErrorReport());
        
    }

    public void LoginPlayer01() {
        var request = new LoginWithCustomIDRequest() { CustomId = "Player1", CreateAccount = true };
        player1.LoginWithCustomID(request, OnPlayerLoginSuccess, OnPlayerFailure);
    }
 
    public void LoginPlayer02()
    {
        var request = new LoginWithCustomIDRequest() { CustomId = "Player02", CreateAccount = true };
        player2.LoginWithCustomID(request, OnPlayerLoginSuccess, OnPlayerFailure);
    }
    private void OnPlayerLoginSuccess(LoginResult result)
    {
        Debug.Log(result.PlayFabId);
    }
 
    private void OnPlayerFailure(PlayFabError error)
    {
        Debug.LogError(error.GenerateErrorReport());
    }


    public void increaseScoreP1()// increased with button press
        {
            scoreP1++;
            Debug.Log("Player1 score is :" + scoreP1);
        }

    public void increaseScoreP2()// increased with button press
        {
            scoreP2++;
            Debug.Log("Player2 score is :" + scoreP2);
        }

#region Leaderboard ____________________________________________________________________________________________________________________________________________________________
public void SendLeaderboard(int P1Score)
{
    var request=new UpdatePlayerStatisticsRequest{
        Statistics=new List<StatisticUpdate>{
            new StatisticUpdate{
                StatisticName="Player1Score",
                Value=P1Score
                
            }
        }
    };
    player1.UpdatePlayerStatistics(request,OnLeaderboardUpdate,OnError);
    
}

public void SendLeaderboard2(int P2Score)
{
    //2 score like below doesnt work.Playfab is only show 1 score per device.
    var request=new UpdatePlayerStatisticsRequest{
        Statistics=new List<StatisticUpdate>{
            new StatisticUpdate{
                StatisticName="Player2Score",
                Value=P2Score
                
            }
        }
    };
    player2.UpdatePlayerStatistics(request,OnLeaderboardUpdate,OnError);
}


void OnLeaderboardUpdate(UpdatePlayerStatisticsResult result)
{
    Debug.Log("Successful Leaderboard sent");
}

void OnError(PlayFabError error)
{
    Debug.LogError(error.GenerateErrorReport());
}


public void submitToLeaderboard()//When ui button is pressed send leaderboard stats
{
    
    SendLeaderboard(scoreP1);
    
}
public void submitToLeaderboard2()//When ui button is pressed send leaderboard stats
{
    SendLeaderboard2(scoreP2);
}

public void getleaderBoard()
{
    var request= new GetLeaderboardRequest{StartPosition=0,StatisticName="Player1Score",MaxResultsCount=10};
    player1.GetLeaderboard(request,OnGetLeaderboard,OnError);
}

public void getleaderBoard2()
{
    var request= new GetLeaderboardRequest{StartPosition=0,StatisticName="Player2Score",MaxResultsCount=10};
    player2.GetLeaderboard(request,OnGetLeaderboard2,OnError);
}

void OnGetLeaderboard(GetLeaderboardResult result)
{
    //Debug.Log(result.Leaderboard[0].StatValue);
    foreach (PlayerLeaderboardEntry player in result.Leaderboard)
    {
        Debug.Log(player.DisplayName + ": " + player.StatValue);
    }
}

void OnGetLeaderboard2(GetLeaderboardResult result)
{
    //Debug.Log(result.Leaderboard[0].StatValue);
    foreach (PlayerLeaderboardEntry player in result.Leaderboard)
    {
        Debug.Log(player.DisplayName + ": " + player.StatValue);
    }
}

#endregion Leaderboard
}

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.

Avinaya Banskota avatar image Avinaya Banskota commented ·

It works when i delete the Unity_Android and Unity_IOS region.Why is this happening??

Thanks

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.