Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Calling client RPC from client?

Discussion in 'Netcode for GameObjects' started by KadaXuanwu, Jul 8, 2022.

  1. KadaXuanwu

    KadaXuanwu

    Joined:
    Oct 26, 2019
    Posts:
    9
    Hello everyone,

    I'm working on a multiplayer game where I want to synchronize the usage of abilities (like player X used ability X). It's peer to peer with Relay. If a client uses an ability, he could call a ServerRpc, which then informs all clients through a ClientRpc. But that's very slow, and I thought it would be better if the client could inform all other clients directly. This way the information wouldn't have to travel two ways (client -> host, then host -> all other clients), but only one direct way to all the clients.

    That's why I'm looking for a way to call a ClientRpc from a client.
    But as far as I know, that's not possible. (or is it?)

    Is there a better way to do it?
    Network variables don't seem right to me for that problem, since it's just a one time event (activate ability X for player X), and not a persistent state.

    I didn't find any helpful information in the docs
    https://docs-multiplayer.unity3d.com/netcode/current/learn/rpcvnetvar
     
  2. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    111
    As far as I know,
    Which makes sense since Netcode has a Server authority design.

    On the other hand, do you really want to give the Client the ability to control the game flow? Remember, never trust the Client.
     
  3. KadaXuanwu

    KadaXuanwu

    Joined:
    Oct 26, 2019
    Posts:
    9
    As far as I understand, you recommend me to make a ServerRpc which then calls a ClientRpc to inform all other clients. I was hoping that there would be a "faster" or more direct way of telling the other clients about the event, because latency is a key factor in my game. On the other hand, you are also right about not trusting the clients...
     
  4. maxkcy

    maxkcy

    Joined:
    Oct 11, 2021
    Posts:
    62
    I want to call the ClientRpc to execute locally beforing sending a ServerRpc to call the clientRpc on all clients instances, excluding the one that just called it and sent the ServerRpc.

    One workaround is to create a local method that will be encapsulated in the ClientRpc. But instead of calling the ClietRpc, just call this method, then send the ServerRpc.

    I am not willing to write up my script to test wheter this is possible, because it will take me a while to self test this, but I will come back with an answer soon. Althought from the sources I have read, there is not any info provided about calling ClientRpc on the client to execute on the local instance.
     
    Last edited: Apr 10, 2023