Photon RPC sends wrong int values

Viewed 432

So im trying to use RPC and send a list of int and an int as parameters, however, while the function sends the int list correctly, the int value it sends is wrong and different for each client even though I'm send the same value to all of them.

This is the RPC method;

[PunRPC]
void GetPlayerCards(int[] _cardListInt, int _gCard)
{
    if(intPlayerCardlist.Count == 0)
    {
        intPlayerCardlist.AddRange(_cardListInt);
    }

    foreach (int cardInt in intPlayerCardlist)
    {
        if(MyCardlist.Contains(gameFullCardlist.cardList[cardInt])) continue;

        else MyCardlist.Add(gameFullCardlist.cardList[cardInt]);
    }

    groundCard = gameFullCardlist.cardList[_gCard];

    Debug.Log("value: " + _gCard + " card name: " + groundCard);
}

This is the call method which is being called from a room instantiated object that belongs to the master client:

int[] List1 = cardList1.ToArray();
int[] List2 = cardList2.ToArray();
int[] List3 = cardList3.ToArray();
int[] List4 = cardList4.ToArray();
int[] List5 = cardList5.ToArray();
int gCard = cardList5[0];

PhotonView PV_P1 = gamePlayersList[0].GetComponent<PhotonView>();
PhotonView PV_P2 = gamePlayersList[1].GetComponent<PhotonView>();
PhotonView PV_P3 = gamePlayersList[2].GetComponent<PhotonView>();
PhotonView PV_P4 = gamePlayersList[3].GetComponent<PhotonView>();

PV_P1.RPC("GetPlayerCards", PV_P1.Owner, List1, gCard);
PV_P2.RPC("GetPlayerCards", PV_P2.Owner, List2, gCard);
PV_P3.RPC("GetPlayerCards", PV_P3.Owner, List3, gCard);
PV_P4.RPC("GetPlayerCards", PV_P4.Owner, List4, gCard);

if you notice im send the same value for all clients but im getting different results, see the log below:

Debug Log

The game Manager and players are being instantiated from an empty game object in each client scene that uses the following script;

private void Awake() 
        {
            //Order of Instantiation is important
            PhotonNetwork.InstantiateRoomObject(InstantiatedPlayersListGameobject.name, Vector3.zero, Quaternion.identity);
            PhotonNetwork.InstantiateRoomObject(cardsDeck.name, Vector3.zero, Quaternion.identity);
            PhotonNetwork.InstantiateRoomObject(GameplayManager.name, Vector3.zero, Quaternion.identity);
        }

        void Start() 
        {

            if(PhotonNetwork.IsConnectedAndReady)
            {
                if (playerPrefab != null)
                {
                    GameObject playerInstantiation = PhotonNetwork.Instantiate(playerPrefab.name, Vector3.zero, Quaternion.identity);
                    playerInstantiation.transform.SetParent(InstanceParent.transform, false);
                    playerInstantiation.transform.localScale = Vector3.one;
                    playerInstantiation.transform.localPosition = Vector3.zero;
                    // playerInstantiation.name = (playerInstantiation.GetComponent<PhotonView>().Owner.NickName + " - AN: " + 
                    // playerInstantiation.GetComponent<PhotonView>().Owner.ActorNumber);
                    Debug.Log("player instantiated");
                }
                else
                {
                    Debug.Log("Place player Prefab");
                }         
            }    
        }
    
1 Answers

It is hard to tell without having more details and context of your code snippets.

However, I think I know what's wrong here.

You expect: You are sending four RPC to four different players.

What happens instead: You are sending four RPC to four different players BUT you do the whole thing four times => each player receives not one (as expected) but rather four RPC with different values.

If it would work as expected you would only see one single log on each of your players since each one should only receive one of your RPC calls. Instead it seems that your calling method is executed on each and every of your four players and each one does its individual randomization of cards before sending these individual values to everyone else.


As in real life where you usually have a "dealer" Photon provides such a person: The so called MasterClient.

The method you are using to invoke this randomization and RPC to everyone should check PhotonNetwork.isMasterClient

if(PhotonNetwork.isMasterClient)
{
    // ...

    int[] List1 = cardList1.ToArray();
    int[] List2 = cardList2.ToArray();
    int[] List3 = cardList3.ToArray();
    int[] List4 = cardList4.ToArray();
    int[] List5 = cardList5.ToArray();
    int gCard = cardList5[0];

    // HINT: In general rather pre-cache and re-use these references
    PhotonView PV_P1 = gamePlayersList[0].GetComponent<PhotonView>();
    PhotonView PV_P2 = gamePlayersList[1].GetComponent<PhotonView>();
    PhotonView PV_P3 = gamePlayersList[2].GetComponent<PhotonView>();
    PhotonView PV_P4 = gamePlayersList[3].GetComponent<PhotonView>();

    PV_P1.RPC("GetPlayerCards", PV_P1.Owner, List1, gCard);
    PV_P2.RPC("GetPlayerCards", PV_P2.Owner, List2, gCard);
    PV_P3.RPC("GetPlayerCards", PV_P3.Owner, List3, gCard);
    PV_P4.RPC("GetPlayerCards", PV_P4.Owner, List4, gCard);
}
Related