Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Moeskjaer · Nov 11, 2014 at 03:37 PM · networkingrpcip

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Moeskjaer · Nov 11, 2014 at 09:22 PM 0
Share

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)?

avatar image jdog98 · Nov 12, 2014 at 04:43 AM 0
Share

alt textSo 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

fix.png (8.2 kB)
avatar image Moeskjaer · Nov 13, 2014 at 09:12 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges