Search Unity

Trying to understand networking. (I'm stuck and could use some help)

Discussion in 'Multiplayer' started by jzaun, Nov 4, 2010.

  1. jzaun

    jzaun

    Joined:
    Feb 20, 2010
    Posts:
    26
    Hi all,

    I'm trying to figure out how networking works. I've got some of the basics working but have some questions. What I have working is:

    * Hosting a game (creating a server, accepting clients)
    * Joining a game (getting a list from the master server, connecting to a game)
    * Chat (a very simple chat works, basically a c# version of what is in the networking demo)
    * Loading a map (a server RPC, allbuffered, at the start takes care of this)
    * Creating a local player (from the hosting computer works.)

    My big question is this:

    How can I call a RPC on a single client from the server? It looks like my only options are "to the server only", "to everyone but me" and "to everyone"

    What I'm looking to do is have the server do something like this:
    Code (csharp):
    1.  
    2.     void OnPlayerConnected (NetworkPlayer p)
    3.     {
    4.         _playerCount++;
    5.         _chat.SendChat("Server", "Player joined the game");
    6.         p.RPC("CreateLocallyControlledObject");
    7.         everyoneExceptP.RPC("CreateNetworkControlledObject");
    8.     }
    9.  
    Maybe I'm missing something? I *think* I can get around this situation above by having the connection client send a RPC to everyone expect itself to create the NetworkControlledObject... but that takes the server out of the loop and out of control (should I care? Coming from other client/server programming environments the server has always been in control - never trust the client type of thing).

    Another situation where a RPC to a single client would be very nice is in private chat messages, or any type of event that only effects a single client.

    Any help would be great!
     

    Attached Files:

  2. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    if you look at the documentation for rpc calls, you don't have to give it an "RPCMode", you can instead pass it a NetworkPlayer, and it will only send the message to that NetworkPlayer.

    so:
    networkView.RPC("CreateLocallyControlledObject", p);

    however, for a 'everyone but p', you can't really do that. You'll need to make some kind of utility class/function that you can send a networkview and array of networkPlayers you want it to send to, and then it will send an rpc individually to each one

    so like:

    rpcHelper.SendMessage("CreateNetworkControlledObject", somePlayerArraythatDoesn'tIncludeP, networkView);

    rpcHelper:
    Code (csharp):
    1.  
    2. function SendMessage(funcName : String, players : NetworkPlayer[], nView : NetworkView){
    3. for (player in Players){
    4. nView.RPC(funcName, player);
    5. }
    6. }
    7.  
     
    Last edited: Nov 4, 2010