2

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");
                }         
            }    
        }
    
  • Well from your log it looks like it is called **4 times** each with different values => where/how is the RPC called exactly? Seems to me that each and every of your four players sends it to each and every other player ... You probably wanted to do it only once? E.g. only if you are the MasterClient? – derHugo May 31 '21 at 07:03
  • @derHugo The RPC is called from a room instantiated object, im calling the RPC 4 times, once for each player that is why its appearing 4 times in the debug log – Eustas Silver May 31 '21 at 07:14

1 Answers1

0

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);
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • I dont think there is a need to use if(Masterclient) as the RPC call method is being called from a single room instantiated object that belongs to the master client: – Eustas Silver May 31 '21 at 08:46
  • @EustasSilver again .. my current impression is that this is done four times which seems like it is done once per existing player. => 16 RPC in total - 4 to each of the 4 players. I don't have any information of where this method is called on or if there really is only one object doing it ... please provide more details if you think they are relevant ;) – derHugo May 31 '21 at 08:58
  • At first thank you for the effort. To clarify things more the layout is as follows; The room instantiate a game manager that distribute the game cards into 4 lists (1 for each player), the game will instantiate 4 game objects (with a script that has the RPC method, the game manager then calls the RPC function on each of those players after getting their photon View. The problem is each p-layer is getting a different value even though im sending the same variable without changing it. I hope i managed to clarify things. – Eustas Silver May 31 '21 at 10:48
  • when and how exactly is that `game manager` instantiated? `The room instantiates` -> no, a certain player has to do this – derHugo May 31 '21 at 10:58
  • each client game scene has a player manager script on an empty object that is already existing, the script uses PhotonNetwork.InstantiateRoomObject(GameManager), this instantiate only one instance of said Game manager that automatically belongs to the master client – Eustas Silver May 31 '21 at 11:05
  • But if each client has such an object it rather sounds like you are generating four instances of that `GameManager` .. as said you should then already here check if you are the Master and only in this case execute that line – derHugo May 31 '21 at 11:51
  • there is only one GameManager as i am instantiating it using PhotonNetwork.InstantiateRoomObject(GameManager) and not PhotonNetwork.Instantiate(GameManager),this way it instantiate only one instance of an object for the whole room – Eustas Silver May 31 '21 at 13:02
  • @EustasSilver yes but in the design you have `each client game scene has a player manager script` it it sounds like it exists once for each client since each client executes the line `PhotonNetwork.InstantiateRoomObject(GameManager)` -> four individual instances are created – derHugo May 31 '21 at 13:08
  • well as per Photon documentation, PhotonNetwork.InstantiateRoomObject(GameManager) doesn't instantiate the object more than once and only for the master client, even if i added the code to all players. and that is the case when i check in the inspector there is only one gameplay manager in the scene, so the issue isn't duplicate versions of the gameplay managers, i think – Eustas Silver May 31 '21 at 13:25
  • @EustasSilver could you provide the `player manager` script that calls the `PhotonNetwork.InstantiateRoomObject(GameManager)`? – derHugo May 31 '21 at 13:33
  • I have added the player manager script in the original post for your reference. – Eustas Silver May 31 '21 at 17:20
  • @EustasSilver hey I think you (accidentally?) added it to the answer .. please rather edit it into your question ;) – derHugo May 31 '21 at 17:29
  • Oh, sorry about that, i added it to the original question this time xD – Eustas Silver May 31 '21 at 17:40