Search Unity

Third Party Photon Unity Networking : Problem of data synch

Discussion in 'Multiplayer' started by Lyro_1, Jun 8, 2017.

  1. Lyro_1

    Lyro_1

    Joined:
    Nov 27, 2016
    Posts:
    7
    Hello guys,

    I am rushing a game that I have to finish for this week-end, so I am in a hurry, but I need some help.
    I have a multiplayer mode in my game, handled by Photon Unity Networking, and I have a huge issue.

    I want to synch data throught the server, based on the "true" data of the Master Client. There are different prefabs in my scene that are managing different parts of the game, such as inputs, gamerules or networking.

    As an example, I need all the clients to have the same countdown of the game, so that they all know when the game stops. They need various data like this, such as score of each team, the number of players, the winner if there is one, etc...

    So I have a function
    Code (CSharp):
    1. void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    in each script that is observed by a Photon View. My GameMode.cs script, which is responsible of the gamemode rules and for all the data about the game itself, have this function like following :

    Code (CSharp):
    1. void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    2.     {
    3.         if (PhotonNetwork.isMasterClient && stream.isWriting)
    4.         {
    5.             stream.SendNext(number_player);
    6.             stream.SendNext(number_player_t1);
    7.             stream.SendNext(number_player_t2);
    8.             stream.SendNext(players);
    9.             stream.SendNext(score_t1);
    10.             stream.SendNext(score_t2);
    11.             stream.SendNext(goal_score);
    12.             stream.SendNext(goal_time);
    13.             stream.SendNext(timeLeftcountdown);
    14.             stream.SendNext(IsCountdown);
    15.             stream.SendNext(Started);
    16.             stream.SendNext(gameEnd);
    17.             stream.SendNext(winner);
    18.         }
    19.         else
    20.         {
    21.             number_player = (int)stream.ReceiveNext();
    22.             number_player_t1 = (int)stream.ReceiveNext();
    23.             number_player_t2 = (int)stream.ReceiveNext();
    24.             players = (Player[])stream.ReceiveNext();
    25.             score_t1 = (int)stream.ReceiveNext();
    26.             score_t2 = (int)stream.ReceiveNext();
    27.             goal_score = (int)stream.ReceiveNext();
    28.             goal_time = (int)stream.ReceiveNext();
    29.             timeLeftcountdown = (int)stream.ReceiveNext();
    30.             IsCountdown = (bool)stream.ReceiveNext();
    31.             Started = (bool)stream.ReceiveNext();
    32.             gameEnd = (bool)stream.ReceiveNext();
    33.             winner = (int)stream.ReceiveNext();
    34.         }
    So, if you are the game master, you send your data about the game, like the time left, the score, etc... and if you are not, you catch the data send by the game master.

    But I tried this in Unity, and it doesn't work, and I don't understand why... Any idea ?

    Thanks for your help !
     
  2. Lyro_1

    Lyro_1

    Joined:
    Nov 27, 2016
    Posts:
    7
    The method was not called because you can't send "exotic" data type throught the stream. Here, a Player[] was not handled by the method, so it was firing an error. Hope it will help some people :)
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    You would have to register the Player as Custom Type. PUN needs to know how to serialize the objects you want sent.
    Then it should be supported as array, too.
    See CustomTypes.cs for the implementations of Unity's types.
     
    Lyro_1 likes this.