Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ownership check in ClientRpc

Discussion in 'Netcode for GameObjects' started by NchiaComuFai, Mar 10, 2023.

  1. NchiaComuFai

    NchiaComuFai

    Joined:
    Mar 26, 2014
    Posts:
    4
    In this ClientRpc example:

    Code (CSharp):
    1. [ClientRpc]
    2.     private void DoSomethingClientRpc(int randomInteger, ClientRpcParams clientRpcParams = default)
    3.     {
    4.         if (IsOwner) return;
    5.  
    6.         // Run your client-side logic here!!
    7.         Debug.LogFormat("GameObject: {0} has received a randomInteger with value: {1}", gameObject.name, randomInteger);
    8.     }
    Why there is this check: if (IsOwner) return; ?

    Thank you
     
  2. Merlandox

    Merlandox

    Joined:
    Jul 10, 2022
    Posts:
    8
    This forum seems to have very little replies. I am not an expert, but i'll reply.

    The isOwner check will prevent the host from moving himself as ClientRpc is run on the server to the client. Without it, it'll move himself if he's a client+server machine.
     
  3. afavar

    afavar

    Joined:
    Jul 17, 2013
    Posts:
    66
    A ClientRpc is called by the server and it will run on all clients. There might be cases where you don't want to run the logic on the owner client. Imagine firing a gun and playing an audio clip. To make things responsive, you can play the audio on the local client as soon as the weapon fires. By using PlayFireAudioServerRpc and PlayFireAudioClientRpc, you can play the audio on all clients. Since you have already played the audio on the local client who fires the gun, you don't need to play it again on the owner with the PlayFireAudioClientRpc.
     
    RikuTheFuffs-U likes this.
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Literally what afavar said. You can remove that check if you don't need it.