How to call a method across everyone in the network?

Hello help me I am new to multiplayer and networking. What I want to do is… to call a method say doXYZ from anyone(either host or client) in the network and the method should be called for everyone in the network (both host and clients).
But my problem is ServerRpc can only be used by clients, Client RPC is only used by server/host. I managed to write some working code but I think it is too redundant. All I want to do is call doXYZ.

public void ABC()
{
    if(!IsHost)
    {
        doXYZ();
        doXYZServerRpc();
    }
    else
    {
        doXYZClientRpc();
    }
}

[ServerRpc]
public void doXYZServerRpc()
{
    doXYZ();
}

[ClientRpc]
public void doXYZClientRpc()
{
    doXYZ();
}

Client calls “RunThisOnAllClientsServerRpc”.
Server runs “RunThisOnAllClientsServerRpc” and calls “ThisOnAllClientsClientRpc”.
All clients run “ThisOnAllClientsClientRpc”.

This is called message relay. You have to instruct the server via a RPC call to make a RPC call to all clients.

This should do what you want:

public void ABC()
{
    if(!IsServer)
    {
        doXYZServerRpc();
    }
}
[ServerRpc]
public void doXYZServerRpc()
{
    doXYZClientRpc();
}
[ClientRpc]
public void doXYZClientRpc()
{
    doXYZ();
}

Note that clients, including the calling client, run doXYZ() only after the server has instructed them, so they all stay in synch. You could run doXYZ() locally for the calling client but then you’d either have to not send this client doXYZ() or handle the case where it’s called twice. But best to start with the “wait for server Rpc” approach before doing client-side prediction that complicates things.

1 Like

that worked

that is a very nice way of solving my problem.
Thank you:)