What exactly happens at the moment that a new client join the room?

Options
Suppose 3 clients are playing game in the room. Each client has the power which is reduced by others' attack. So every clients has different value of power at a moment. Everytime clients' power is reduced, they announce it to others
using photonView.RPC. (The value is a field in a script attached to the network object owned by client)
When a new client joins the room, PUN creates all current clients on the new clients' device. The new client also let other clients know his 1st power value by calling photonView.RPC. This is what I understand about how PUN works in my case.

Now I am struggling how to let the new client know other clients current power values.
Does PUN give those current power values of others to the new client ? Or I have to do something for this?

Thanks in advance for your time for this dull question.

Comments

  • Hi @mhhk88,

    when the new client has joined the room, he receives all Instantiation calls made by other clients. He then instantiates those objects with default values. To update this client you have different options.

    The first is to use buffered RPCs instead of RPCs. Although I let you know about this option, I wouldn't recommend it, because having a lot of buffered message might have a bad influence on joining times for new clients.

    The second option is to use a custom OnPhotonSerializeView implementation for synchronizing position, rotation, the 'power' value and basically each attribute you want to have synchronized, too.

    The third option is to use the void OnPhotonPlayerConnected(PhotonPlayer newPlayer) callback and fire another RPC to update the new client. Since this might be the best option in this case, I'm adding a code snippet as an example:
    public void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
    {
        if (photonView.isMine)
        {
            photonView.RPC("SetValue", newPlayer, powerValue);
        }
    }
    In this example code, the RPC gets only sent to the new player, because other clients already know this value.
  • mhhk88
    mhhk88
    edited June 2018
    Options
    I tried the 3rd option. This is my codes.
    void OnPhotonPlayerConnected(PhotonPlayer newPlayer) {
            if (photonView.isMine) {
    		photonView.RPC("SetValue", newPlayer, myFighter.GetComponent<PhotonView>().ownerId, 
                         myFighter.GetComponent<TankDamage>().powerValue);
    	}
    }
    
    [PunRPC]
    void SetValue(int playerId, float powerValue) {
    	GameObject[] fighters = GameObject.FindGameObjectsWithTag("FTR") as GameObject[];
    	foreach(GameObject go in fighters) {
    		if (go.GetComponent<PhotonView>().ownerId == playerId) {
    			go.GetComponent<TankDamage>().powerValue = hp;
    			break;
    		}
    	}
    }
    When a new client joins, It is called and worked as I wanted. Thank you so much.
    And I wonder if I did it as PUN wants us to do. Do you think that the SetValue method OK to go?
  • If you don't notice any unwanted effects in terms of performance this should be okay to work with. Anyway, there is one thing I would like to mention. The more players the are in the room, the more often SetValue gets called and furthermore GameObject.FindGameObjectsWithTag gets called. Those calls might have a bad influence on the performance, which you want to avoid, means that you probably want to store the result when the Find function is called first, e.g.:
    if (fighters == null)
    {
        fighters = GameObject.FindGameObjectsWithTag("FTR") as GameObject[];
    }