Search Unity

[Solved] NetworkView.RPC: Can you nest RPC calls?

Discussion in 'Scripting' started by asperatology, May 28, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Here's a sample code:

    Code (CSharp):
    1.     [RPC]
    2.     public void RPC_Call_1() {
    3.         //Do something...
    4.     }
    5.  
    6.     [RPC]
    7.     public void RPC_Call_2() {
    8.         //Do something...
    9.         //Obtain network view
    10.         networkView.RPC("RPC_Call_1", RPCMode.All, null);
    11.     }
    12.  
    13.     public void Update() {
    14.         //Do something...
    15.         //Obtain network view
    16.         networkView.RPC("RPC_Call_2", RPCMode.All, null);
    17.     }
    18.  
    Is it possible to nest RPC calls?

    If yes, but my code is wrong, can someone correct it for me? Thanks in advance.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Yes it is however you are going to end up calling RPC_Call_1 n^3 times each frame, ouch!
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Thanks, though I never expected it to be called n^3... If I call RPC_Call_2 twice, RPC_Call_1 will be called 6 times per frame? Or do you mean Big O(n^3)?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Ok heres an attempt to visualise whats going on. Untitled drawing.png
     
    asperatology likes this.
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Oh... So, each RPC call is broadcasted to each client, therefore the total execution of RPC_Call_1 is twice the amount of RPC_Call_2 calls, which is twice the amount of connected clients receiving the broadcasted RPC calls...

    Thanks. That visualization really helps me.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If RPC_Call_1 is always called from RPC_Call_2 and always on all clients then it really doesn't need to be an RPC in the first place. Just call the method in the first RPC.
     
    Zaladur likes this.