Search Unity

return value from client to server (host)

Discussion in 'Multiplayer' started by khalidhex, Dec 3, 2016.

  1. khalidhex

    khalidhex

    Joined:
    Apr 13, 2016
    Posts:
    11
    Hello ...

    I have two local players (client-server) and i need to execute a method on the client scene and return the value to the server scene.... the scenario goes like this ........ the client throw a card and end his turn > the name of the card send to the server as string .

    This script execute on the server ! but i need it to execute it on the client and the value (only) returned to the server .
    Code (CSharp):
    1.  if (Network.peerType == NetworkPeerType.Client)
    2.   {
    3.   GUI.Label(new Rect(100, 100, 100, 25), " Client Logged");
    4.  
    5.   if (GUI.Button(new Rect(100, 150, 100, 25), "Log Out"))
    6.   {
    7.   Network.Disconnect();
    8.   }
    9.  
    10.   if (GUI.Button(new Rect(100, 190, 100, 25), "Thrown Card Name"))
    11.   {
    12.   GetComponent<NetworkView>().RPC("ClientThrownCardName", RPCMode.Server);
    13.   }
    14.  
    15.   }
    16.   if (Network.peerType == NetworkPeerType.Server)
    17.   {
    18.   GUI.Label(new Rect(100, 100, 100, 25), " Server Started");
    19.   GUI.Label(new Rect(100, 125, 100, 25), "" + Network.connections.Length);
    20.  
    21.   if (GUI.Button(new Rect(100, 150, 100, 25), "Log Out"))
    22.   {
    23.   Network.Disconnect();
    24.   }
    25.  
    26.  
    27.   }
    28.   }
    29.   }
    30.   [RPC]
    31.  
    32.   public void ClientThrownCardName()
    33.   {
    34.   if (Network.isServer)
    35.   {
    36.   run();
    37.   }
    38.  
    39.   }
    40.  
    41.  
    42.   [RPC]
    43.   public void run()
    44.   {
    45.   var throwzone = GameObject.Find("ThrowZone");
    46.   Debug.Log(throwzone.transform.GetChild(0).transform.name);
    47.  
    48.   }
     
  2. khalidhex

    khalidhex

    Joined:
    Apr 13, 2016
    Posts:
    11
    Update : ok i figured it out on my own , It's actually pretty simple:p

    Code (CSharp):
    1.   if (Network.peerType == NetworkPeerType.Client)
    2.             {
    3.                 GUI.Label(new Rect(100, 100, 100, 25), " Client Loged");
    4.  
    5.                 if (GUI.Button(new Rect(100, 150, 100, 25), "Log Out"))
    6.                 {
    7.                     Network.Disconnect();
    8.                 }
    9.  
    10.                 if (GUI.Button(new Rect(100, 190, 100, 25), "Thrown Card Name"))
    11.                 {
    12.                     GetComponent<NetworkView>().RPC("run", RPCMode.Server, GameObject.Find("ThrowZone").transform.GetChild(0).transform.name);
    13.                 }
    14.  
    15.             }
    16.             if (Network.peerType == NetworkPeerType.Server)
    17.             {
    18.                 GUI.Label(new Rect(100, 100, 100, 25), " Server Started");
    19.                 GUI.Label(new Rect(100, 125, 100, 25), "" + Network.connections.Length);
    20.  
    21.                 if (GUI.Button(new Rect(100, 150, 100, 25), "Log Out"))
    22.                 {
    23.                     Network.Disconnect();
    24.                 }
    25.              
    26.  
    27.             }
    28.         }
    29.     }
    30.     [RPC]
    31.  
    32.      void run (string cardname)
    33.     {
    34.         if (Network.isServer)
    35.         {
    36.             Debug.Log(cardname);
    37.         }
    38.