Sending RPC to specific photon players.
Hi, I need help sending an RPC to a specific photon player. I don't want to send it to all players since I'm sending over an int from a master client to another client.
What is the best way to send an RPC to a client that RPC'd the master?
All this code works except for "RPC_SetSpawnIndex()" In RPC_GetNextSpawn() the master would increment their spawnindex and then pass that int back to the client that RPC'd the master client.
So when it comes to the master client sending back a message to the client that RPC'd the master it just doesn't work. Nothing happens. No error or anything...
Here's my code.
[PunRPC]
private void RPC_GetNextSpawn(PhotonMessageInfo info) // Gets the spawn point from the master client
{
print("Master Received Message from:"+ info.sender);
spawnPos++;
PhotonView.RPC("RPC_SetSpawnIndex", info.sender, spawnPos); // Sends master clients spawnindex to sender.
}
[PunRPC]
private void RPC_SetSpawnIndex(PhotonMessageInfo info, int pos) // Sender receives the clients index and sets it as their own
{
//print("I," + PhotonNetwork.player.NickName + " From " + info.sender);
//print("I," + PhotonNetwork.player.NickName + " set my spawn to :"+pos+", From"+info.sender);
spawnPos = pos;
}
[PunRPC]
private void RPC_CreatePlayer() // Spawns the player
{
GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
PhotonView.RPC("RPC_GetNextSpawn", PhotonTargets.MasterClient);
print("My name is "+ PhotonNetwork.player.NickName +" My New Spawn Point Index: "+spawnPos);
GameObject tankInstance = PhotonNetwork.Instantiate("Tank_Networked", spawnPoints[spawnPos].transform.position, spawnPoints[spawnPos].transform.rotation, 0);
tankInstance.GetComponent<TankController>().SetColors(); // Sets the color of the tank from the client.
}
Answer by BazilBaachu · Feb 24, 2020 at 05:14 PM
Hey i am new to unity and i too wanted to send rpc to a specific player but i couldn't find a good solution in forums. So after playing with my code for a while i found a solution that helped me to send RPC to a specific player. The PhotonView. RPC method has an overload where Player is the parameter instead of RPCTARGET, through which i could send the rpc to the specific client passing his index in PhotonNetwork.playerlists.
It worked for my situation. Hope it help others too : )
,Hi, I don't know if you got the solution but for me i haven't got what i wanted from any forum :( . But after playing with my code i figured how to send RPC to a specific player. The PhotonView. RPC method has an overload where it takes a Player as a parameter instead of RpcTarget. So that you could send the RPC to a specific person. If you have the index of the player in PhotonNetwork. Playerlists, you could send rpc to that specific player.
Hope it helps... :)
@BazilBaachu This solution worked for me, thanks a lot :'D
Answer by ChristianSimon · Mar 15, 2018 at 10:40 AM
Hi,
I don't know if sending a RPC from inside another RPC is a good solution at all. Maybe using RaiseEvent or the Player Custom Properties is a better solution in this case.
When using the RaiseEvent function you have the advantage that you don't need a PhotonView component for communication. You can also set the client, who will receive the message you are going to send next. To see how RaiseEvent works, I would recommend you taking a look at the RPCs and RaiseEvent documentation page.
To tell a client the index of his spawn, you can either use another RaiseEvent call again or use the Player's Custom Properties.
Answer by Sanjay112000 · Feb 28, 2021 at 06:01 AM
Thank you bazilbaachu(https://answers.unity.com/users/1127669/bazilbaachu.html).
I am posting my entire workflow of instantiating players at different positions:
Initially checking whether we are master client or not:
Then obtain list of players and send rpc to each of them individually on positions to spawn them.You can store an array of positions and send index in rpc.
Here is the code:
photonView = PhotonView.Get(this); if(PhotonNetwork.IsMasterClient) { foreach(Player pl in PhotonNetwork.PlayerList) { //Debug.Log(pl); photonView.RPC("InstantiationPlayer",pl,index);
index++;
}
}
[PunRPC]
void InstantiationPlayer(int index)
{
PhotonNetwork.Instantiate(Playerprefabs[value].name,SpawnPoints[index].transform.position, Quaternion.identity, 0); }
Thank you.