Search Unity

Question Is a Server not supposed to run ServerRpc's?

Discussion in 'Netcode for GameObjects' started by CodeMonkeyYT, Mar 8, 2023.

  1. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    125
    Hey everyone,

    I just recently made a game with NGO and everything worked perfectly.

    But now I'm researching how to use Game Server Hosting which involves creating a Server rather than Host and I'm now noticing that on the Server the ServerRpcs don't run.

    What I mean by that is code running on the server calling a ServerRpc doesn't run. If I call a ServerRpc from a Client it does run on the Server.
    Is this intentional?

    Basically in the game I have several scenarios where some ServerRpc then calls another ServerRpc, and it works fine on a Host. But on a Server, if the Client first triggers the first ServerRpc then that first one will run on the Server, but then the server tries to call the second ServerRpc and that one does not run.

    Thanks!
     
    CodeSmile likes this.
  2. Evil-Otaku

    Evil-Otaku

    Joined:
    Oct 17, 2012
    Posts:
    72
    That's correct. Just as clients can call ClientRPCs, servers can not call ServerRPCs.

    On hosts what's happening is the host client is calling the serverRPC that is being run on the host server.
    There is a really good section in the docs about porting from client hosting to dedicated servers
    https://docs-multiplayer.unity3d.com/netcode/current/learn/porting-to-dgs
     
    RikuTheFuffs and CodeMonkeyYT like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,989
    I think the idea is that there is no point for the server to route method calls through the ServerRPC system when it could simply call a method:
    Code (CSharp):
    1. ServerOnlyMethod();
    So if there is also a ServerRPC method for the same thing, make that ServerRPC just call that same ServerOnlyMethod(). ;)
     
    CodeMonkeyYT likes this.
  4. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    125
    Oh I hadn't seen that page, thanks!

    Yeah that's what I ended up doing and it does work, not too difficult to refactor the code but it is annoying.
    It would be simpler if NGO just handled that automatically. If calling a ServerRpc from a Server just run that code normally instead of going through the RPC system.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,989
    Interestingly, for the host calling a ServerRPC that actually gets translated to a direct method call. Which is why you can get yourself into a stackoverflow quite easily if you have a Server-Client RPC cycle (like transfer data => request more data => transfer data => request more => etc) that would end up being one method calling the other infinitely if the host client initiates that message cycle.
     
    ADmr0 likes this.