Using selectively OnPhotonSerializeView

Options
I want to send/recieve in method OnPhotonSerializeView() only those players, who have some customProperty to reduce data traffic/ping.

Every player has customProperties "positionX" and "positionY". I try smth like that:
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
   stream.SendNext... //send data
   ...
}
else
{
   object val;
   if (PhotonNetwork.player.customProperties.TryGetValue("PositionX", out val) == true)
   {
     int posX = (int)(float)PhotonNetwork.player.customProperties["PositionX"];
     int posY = (int)(float)PhotonNetwork.player.customProperties["PositionY"]; //get own client position

     Vector3 myPosition = new Vector3(posX, posY);
     Vector3 hisPosition = transform.position;

     if (Vector3.Distance(hisPosition , myPosition ) > 200) //check if distance between us is big
     {
     return; //dont recieve data -> we don't waste some traffic to get data (is it true?)
     }
   }
                
   stream.ReceiveNext... //continue getting data
   ...
}

Am I on right way? Or this won't reduce data traffic/ping?

Thanks a lot :)

UPD: This works (doesn't recieve data if distance is big), but I don't know how to check if this reduces client's traffic.

Comments

  • Hi crutch12,

    when OnPhotonSerializeView has already been called, the client already received data, so in this case you just skipped some updates on player positions, which - I guess - will be the steps after your return statement.

    What you want to achieve can be done by using Interest Groups. Therefore a client can join an interest group to only send data to or receive data from clients, which are subscribed to the same group. This is done by setting the group value on the player's PhotonView component, e.g. photonView.group = 0; whereat 0 is default value.

    Please note, that when you have at least two clients, which are subscribed to different interest groups, they don't receive updates from each other. So you need a way to make sure, that the clients are joining the same group when they are close to each other. One way to achieve this for example is to subdivide the world into different cells, whereat each cell represents a certain interest group. When the player is moving around he is subscribed to the interest group which is represented by the cell he is currently inside. This is a solution we are currently working on, so maybe I can share this with you, need to check that.

    Another way is to send at least one position update to each player per second, so that each client can perform a position check and join a given interest group, if they are close to each other.