- Home /
RPC call from client to client
Hi
I'm trying to send data from client to client but it doesn't seem to work. I'm using Unitys MasterServer. It works fine when i'm sending data from server to client and client to server.
I'm sending data with RPC. Is it possible to send data from client to client or do I have to rewrite my code so I send an RPC call from client 1 to server, and then a new RPC call from server to client 2?
Here I call the RPC function:
networkView.RPC("UpdateBombIncoming", playerPurple);
Here's my RPC:
[RPC]
void UpdateBombIncoming(){
Debug.Log ("Bomb is incoming");
bombIncoming = true;
}
Here's my Network code:
public class NetworkManager : MonoBehaviour {
public int maxNumberOfPlayers = 3; public int port = 25002; public int playerID; public NetworkPlayer playerBlue; // Server public NetworkPlayer playerPurple; public NetworkPlayer playerGreen; public int playerColor; // 1: blue, 2: purple, 3: green public Font myFont; public int playerCount;
private SceneManager sceneManager; private bool showPlayersOnGUI;
string registeredGameName = "Bomba_Alpha"; bool isRefreshing = false; float RefreshRequestLength =
3.0f; HostData[] hostData;
void Awake(){
}
void Start(){
sceneManager = GetComponent<SceneManager>(); }
void Update(){
Debug.Log("Blue Player connected from " + playerBlue.ipAddress + ":" + playerBlue.port);
Debug.Log("Purple Player connected from " + playerPurple.ipAddress + ":"
+ playerPurple.port);
Debug.Log("Green Player connected from " + playerGreen.ipAddress + ":" + playerGreen.port);
Debug.Log("Players connected to server " + playerCount);
// When player is connected to server, show player on GUI
if(Network.isServer){
// Show players on GUI
showPlayersOnGUI = true;
}
else if(Network.isClient){
// Show players on GUI
showPlayersOnGUI = true;
}
}
private void StartServer(){
Network.InitializeServer (maxNumberOfPlayers, port, false);
MasterServer.RegisterHost(registeredGameName, "Bomba Alpha", "Test of TMB_server"); }
void OnServerInitialized(){
Debug.Log ("Server has been initialized!");
Debug.Log("Player " + playerCount + " connected from " + Network.player.ipAddress + ":" + Network.player.port);
// Set server as playerBlue
playerBlue = Network.player; }
void OnMasterServerEvent(MasterServerEvent masterServerEvent){
if(masterServerEvent == MasterServerEvent.RegistrationSucceeded){
Debug.Log("Registration Successful!");
} }
public IEnumerator RefreshHostList(){
Debug.Log ("Refreshing...");
MasterServer.RequestHostList (registeredGameName); // Get servers from MasterServer with 'our' name
float timeStarted = Time.time;
float timeEnd = Time.time + RefreshRequestLength;
// Contínuelly update hostData
while (Time.time < timeEnd) {
hostData = MasterServer.PollHostList(); // pull down the requested server
yield return new WaitForEndOfFrame();
}
// Check if there is no servers
if (hostData == null || hostData.Length == 0) {
Debug.Log ("No active servers have been found");
}
else{
Debug.Log(hostData.Length + " server(s) found");
} }
void OnPlayerConnected(NetworkPlayer player) {
playerCount++;
if(Network.player == player){
Debug.Log ("Hey!");
}
if (playerCount == 2){
playerPurple = player;
Debug.Log ("I'm purple!");
}
// Set 2nd client as playerGreen
else if (playerCount == 3){
playerGreen = player;
}
if(Network.isServer){
Debug.Log ("Colors are updating!");
networkView.RPC("UpdateColors", RPCMode.Others, playerBlue, playerPurple, playerGreen, playerCount);
}
Debug.Log("Player " + playerCount + " connected from " + player.ipAddress + ":" + player.port); }
void OnPlayerDisconnected(){
playerCount--; }
// On GUI is for testing and will not exist in the final game public void OnGUI(){
if (sceneManager.currentScene == 3){
// Start server
if(GUI.Button(new Rect(150f,100f,300f,150f), "Start new server")){
StartServer();
}
// Refresh server list
if(GUI.Button(new Rect(150f,250f,300f,150f), "Refresh server list")){
StartCoroutine("RefreshHostList");
}
if (hostData != null) {
for (int i = 0; i < hostData.Length; i++){
if(GUI.Button(new Rect(150f ,400f + (110f * i),300f,150f), hostData[i].gameName))
{
Network.Connect(hostData[i]);
}
}
}
// Show list of players connected to the server
if (showPlayersOnGUI) {
GUIStyle myStyle = new GUIStyle();
myStyle.font = myFont;
GUI.Label(new Rect(20, 700, 200, 20), ("Other players connected:"), myStyle);
if (Network.player == playerBlue){
GUI.Label(new Rect(20, 600, 200, 20), ("You are BLUE player"), myStyle);
if(playerCount == 1){
GUI.Label(new Rect(20, 750, 200, 20), ("- none -"), myStyle);
}
if(playerCount >= 2){
GUI.Label(new Rect(20, 750, 200, 20), ("Purple player"), myStyle);
}
if(playerCount == 3){
GUI.Label(new Rect(20, 800, 200, 20), ("Green Player"), myStyle);
}
}
else if (Network.player == playerPurple){
GUI.Label(new Rect(20, 600, 200, 20), ("You are PURPLE player"), myStyle);
if(playerCount >= 2){
GUI.Label(new Rect(20, 750, 200, 20), ("Blue player"), myStyle);
}
if(playerCount == 3){
GUI.Label(new Rect(20, 800, 200, 20), ("Green Player"), myStyle);
}
}
else if (Network.player == playerGreen){
GUI.Label(new Rect(20, 600, 200, 20), ("You are GREEN player"), myStyle);
if(playerCount == 3){
GUI.Label(new Rect(20, 750, 200, 20), ("Blue player"), myStyle);
GUI.Label(new Rect(20, 800, 200, 20), ("Purple Player"), myStyle);
}
}
}
} }
void UpdatePlayersOnNetwork(){
if (Network.player == playerBlue) {
Debug.Log("I'm player BLUE");
networkView.RPC("UpdateBluePlayer", RPCMode.Others, Network.player);
}
if (Network.player == playerPurple) {
Debug.Log("I'm player PURPLE");
networkView.RPC("UpdatePurplePlayer", RPCMode.Others, Network.player);
}
if (Network.player == playerGreen) {
Debug.Log("I'm player GREEN");
networkView.RPC("UpdateGreenPlayer", RPCMode.Others, Network.player);
} }
// -------- RPC sends data via network
--------
// Update player-color connections [RPC] void UpdateColors(NetworkPlayer blue, NetworkPlayer purple, NetworkPlayer green, int players){
Debug.Log ("Colors are updated");
playerBlue = blue;
playerPurple = purple;
playerGreen = green;
playerCount = players;
//Debug.Log ("Colors have been updated!"); } }
Thanks
Moeskjaer
Answer by jdog98 · Nov 11, 2014 at 05:27 PM
You need to say " if(Network.isClient == true){ } " Everyone will receive the RPC, but only clients will do what's in the {} ,the same works for" if(Network.isServer == true){ } " that's how we separate clients and server in an RPC. For you, the client needs to send the RPC to another client, so we use Network.isClient in the RPC
Thank you for answering.. Were exactly are you saying I should use Network.isclient? When I´m sending RCP like this: "networkView.RPC("UpdateBombInco$$anonymous$$g", playerPurple);", doesn't that mean it's supposed to only send it to playerPurple (thus making the Network.isClient kind of redundant)?
So everyone gets the gets the call to do something (RPC), including the server, and you want to separate server from client. You also reference playerPurple when you call the RPC, but the you don't reference it in the parameters of the actual RPC. You should get an error for that. If you want to send specific data from a specific player to another specific player, then I cant help you
Thanks! I think I have a solution.. But actually in "networkView.RPC("UpdateBombInco$$anonymous$$g", playerPurple);" playerPurple is a NetworkPlayer and to my understanding this should (and cannot) be a parameter in the RPC.. But anyway, I only have 1 server and 2 clients, so I think I will seperate them by always sending an int that tells who to receive the rpc kind of like you suggested, like:
[RPC]
public void UpdateBombInco$$anonymous$$g(int player){
if(playerInt = player){
// do something with player1
}
if(playerInt = player){
// do something with player2
}
if(playerInt = player){
// do something with player3
}
}
I'll give you a V if I succed :)
Your answer
Follow this Question
Related Questions
Synchronizing Game State Via RPCs 0 Answers
RPC and how to talk to clients 0 Answers
Network Connection Works Locally, but not Publicly 0 Answers
Send a Pause RPC on OnApplicationPause 1 Answer
RPC or Synchronization ? 1 Answer