#ObserverRpc - Best Practice

7 messages · Page 1 of 1 (latest)

nova wolf
#

What is the cleanest and best approach to call an ObserverRpc from a Client and do the logic locally aswell? Do I have to start with a ServerRpc and then call an ObserverRpc?
Using ObserverRpc(runLocally=true) directly on the client kinda works, but I should not call an ObserverRpc from a client right? As the warning I get when trying to do it tells me.

unborn tree
#

Yeah you can not call Observer or TargetRpc from a Client. You always need to go to the Server to ask him, if he can send it for you. RunLocally is good, when you have double logic to be executed, which should be run on the Server and on the client, so that you dont need to duplicate code

nova wolf
#

@unborn tree Thank you!
So how would you do something locally on the client first, then do the same on all the other clients?
Would you execute the code locally, send a ServerRpc and call a ObserverRpc, and executing the code again?

unborn tree
# nova wolf <@351847052680101899> Thank you! So how would you do something locally on the cl...

Like this:

public class Something : NetworkBehaviour {
  public void override OnStartClient() {
    base.OnStartClient();
    if(IsOwner) {
      ServerNotifyAll();
    }
  }
  [ServerRpc]
  void ServerNotifyAll() {
    /** ImaginaryMethodBlock
    */
    ObserverNotifyAll();
  }
  
  [ObserverRpc(RunLocally=true)]
  void ObserverNotifyAll() {
    ImaginaryMethodBlock();
    // do something for all 
  }
  void ImaginaryMethodBlock() {
    // Something what Server and Observer does when the Method stack "NotifyAll" is called
  }
}
nova wolf
#

But that runLocally only tells me it runs locally on the server right?

unborn tree
#

Correct, runLocally would be in this case also executed on the Server. Since he is on of the observers, the client who sends it, will be notified, you could exclude him from the ObserverRpc and do the logic which is executed in the ObserverRpc before you trigger the ServerRpc

nova wolf
#

@unborn tree Thanks man! 🙂