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. Dismiss Notice

RPC Question

Discussion in 'Multiplayer' started by Xenome, May 29, 2014.

  1. Xenome

    Xenome

    Joined:
    Oct 29, 2013
    Posts:
    54
    I am making a multiplayer game and I am a bit confused on how exactly the RPC function works.

    For instance if I call an RPC that changes the gameobject name to "test" in RPCMode.All and each player has the script, will it change only for his model in every client or will it change everyone's name to "test".

    What if I do the same thing but I call it from the server? For example:

    Code (csharp):
    1. void ChageName()
    2. {
    3.  networkView.RPC("ServerChangeName", RPCMode.Server);
    4. }
    5.  
    6. [RPC]
    7. void ServerChangeName()
    8. {
    9.  networkView.RPC("ClientChangeName", RPCMode.All);
    10. }
    11.  
    12. [RPC]
    13. void ClientChangeName()
    14. {
    15.  gameobject.name = "test";
    16. }
    If there is any specified document about this I would like a link.
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Well, this looks a bit... overcomplicated.

    I did it by simply changing the name on the server end, then sending the RPC to other players to change the instance name on their ends, like this:
    Code (csharp):
    1.  
    2. [RPC]
    3. void SendName (name : String)
    4. {
    5. yourNameVariable = name;
    6. }
    7.  
    8. void Update ()
    9. {
    10. networkView.RPC("nameChange", RPCMode.All); //you can also use others here, if there are issues with server doing it wrong.
    11. }
    12.  
    There you go. just some help i can give about RPCs. also hope I understood you correctly.

    -FuzzyQuills
     
  3. Xenome

    Xenome

    Joined:
    Oct 29, 2013
    Posts:
    54
    I am not sure that calling an RPC from update is a good idea.
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    That there was just an example. you can use an RPC call anywhere you want. for something like a player being defeated, you would use an "if" statement or switch to decide when to tell the opponents a player has died. Kinda like this: (again, typing out of my head)
    Code (csharp):
    1.  
    2. void Update () {
    3. if (player.defeated) {
    4. <do something>
    5. networkView.RPC(<do something>, RPCMode.All, <parameters for do something>);
    6. }
    7. }
    8.  
    In your case, just have a Boolean variable in place for every time you change your name. that way, by clicking a button, the bool will come on, the RPC will be sent, then the bool could be set top false to prevent sending it again. Here's another example: (if i got the syntax wrong, it's because i am not used to C#!)
    Code (csharp):
    1.  
    2. bool sending;
    3. String typer;
    4. [RPC]
    5. void sendName (String info){
    6. name = info;
    7. }
    8. void Update () {
    9. if (sending){
    10. name = typer;
    11. networkView.RPC(sendName, RPCMode.Others, typer);
    12. sending = false;
    13. }
    14. }
    15.  
    16. void OnGUI () {
    17. if (GUI.Button(Rect(<button dimensions>), "send name")){
    18. sending = true;
    19. }
    20. typer = GUI.TextField(Rect(<dimensions>), typer);
    21. }
    22.  
    the typer variable could then be tied to a text field in OnGUI, to change the name easily. you can also tie a button to the bool variable in the same way.

    Good luck!

    -FuzzyQuills

    EDIT: just changed the code to state the OnGUI function
     
    Last edited: Jun 2, 2014