Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

RPC calls with the new Unity Networking?

Discussion in 'Multiplayer' started by 420BlazeIt, Aug 24, 2015.

  1. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Hi guys I am used to the legacy networking so I used to use code like this:

    Code (CSharp):
    1. netView.RPC("rpcName", Target);
    2.  
    3. [RPC]
    4. void rpcName ()
    5. {
    6.     //Do something
    7. }
    So how do I do that with the new Unity Networking?

    Thanks! :)
     
  2. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
  3. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Thanks for the link. From what I can see RPCs are way easier to use is there a way I can use the old RPCs with the new Networking?
     
  4. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    No you can't mix old and new API. You have to change your code with Command and ClientRpc attribute.
     
  5. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    In the old Unity Networking for a chat box you'd send an rpc to update the chatbox with the new message.

    How do I do that with the new networking?
     
  6. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    You have to send a Command to the server and the server have to send an Rpc to the clients.

    For example:

    Code (CSharp):
    1. // historic.
    2. private List<string> m_chatMessages = new List<string>();
    3.  
    4. // function called somewhere when the player write a message in the chat box.
    5. public void SendChatMessage(string message)
    6. {
    7.     CmdAddChatMessage(message);
    8. }
    9.  
    10. // function called on the server.
    11. [Command]
    12. private void CmdAddChatMessage(string message)
    13. {
    14.     RpcAddChatMessage(message);
    15. }
    16.  
    17. // function called on the clients
    18. [ClientRpc]
    19. private void RpcAddChatMessage(message)
    20. {
    21.     m_chatMessages.Add(message);
    22. }