Skip to content

Add SendTo.MeAndServer #3443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcusx2 opened this issue May 9, 2025 · 5 comments
Closed

Add SendTo.MeAndServer #3443

marcusx2 opened this issue May 9, 2025 · 5 comments
Labels
type:feature New feature, request or improvement

Comments

@marcusx2
Copy link

marcusx2 commented May 9, 2025

Is your feature request related to a problem? Please describe.
Currently, it's not possible to use the SendTo enum to send to local client and server.

Describe the solution you'd like
I want a syntax like [Rpc(SendTo.MeAndServer)] to easily send a request to local client and server.

Describe alternatives you've considered
There is a convoluted way to do this using rpcparams, it works and I'm using it as a workaround for now.

rpcParamsMeAndServer = new RpcParams { Send = new RpcSendParams { Target = RpcTarget.Group(new[] { NetworkManager.LocalClientId, NetworkManager.ServerClientId }, RpcTargetUse.Temp) } };

Additional context
The reason I want this is because it's extremely useful when paired with AnticipatedNetworkVariable. When using AnticipatedNetworkVariable, we typically need to run the code on the local client, call anticipate, and send an rpc to the server. Instead, I'm doing this

[Rpc(SendTo.SpecifiedInParams)]
private void MyMethod(int bla, RpcParams rpcParams = default)//pass rpcParamsMeAndServer 

Since calling Anticipate works on the client and server, effectively I can use the same code. It works like a charm, but would be easier to just have [Rpc(SendTo.MeAndServer)].

To summarize, this feature would be specifically useful for AnticipatedNetworkTransform. I don't see why not add it.

@marcusx2 marcusx2 added type:feature New feature, request or improvement stat:awaiting-triage Status - Awaiting triage from the Netcode team. stat:reply-needed Awaiting reply from Unity account labels May 9, 2025
@NoelStephensUnity
Copy link
Collaborator

@marcusx2

Have you tried anything like this?

        private ulong[] m_ServerAndMe;

        public void SendToMeAndServer()
        {
            // One way to do this
            SendToMeAndServerRpc(RpcTarget.Server);
            SendToMeAndServerRpc(RpcTarget.Me);

            // Another way to handle this
            if (m_ServerAndMe == null)
            {
                m_ServerAndMe = new ulong[] { NetworkManager.ServerClientId, NetworkManager.LocalClientId };
            }
            SendToMeAndServerRpc(RpcTarget.Group(m_ServerAndMe, RpcTargetUse.Temp));
        }

        [Rpc(SendTo.SpecifiedInParams)]
        private void SendToMeAndServerRpc(RpcParams rpcParams = default)
        {

        }

@github-actions github-actions bot added stat:awaiting-response Awaiting response from author. This label should be added manually. and removed stat:reply-needed Awaiting reply from Unity account labels May 14, 2025
@marcusx2
Copy link
Author

Yes, that works too. However, I still think having [Rpc(SendTo.MeAndServer)] would be nice. Not saying there aren't alternatives, but there is a valid use case to have it.

@github-actions github-actions bot added stat:reply-needed Awaiting reply from Unity account and removed stat:awaiting-response Awaiting response from author. This label should be added manually. labels May 14, 2025
@NoelStephensUnity
Copy link
Collaborator

NoelStephensUnity commented May 15, 2025

It really depends upon what you are trying to accomplish by invoking an Rpc locally and also sending the Rpc to the server using that approach.

As an example:

    public class SomeBehaviour : NetworkBehaviour
    {
        public void InvokeServerAndMe()
        {
            if (!IsServer)
            {
                InvokeServerSideRpc();
            }
            ///////////////////////////////
            // Add the the script you want to be invoked locally and on the server

            ///////////////////////////////
        }

        [Rpc(SendTo.Server)]
        public void InvokeServerSideRpc()
        {
            InvokeServerAndMe();
        }
    }

The above is the same as using an RPC with a SendTo.ServerAndMe target with the difference being that the above will be more performant (locally) than if there was a SendTo.ServerAndMe target... since the "Me" portion is just invoking the script directly and not having to traverse the additional RPC call-stack.

Since there are many ways to accomplish this using the existing API (with minimal script), we will not be adding a SendTo.ServerAndMe target. We do appreciate the feedback and welcome any suggested feature, but with the already long list of features that we have committed to it requires us to weigh the over-all value add that it will bring to all users and where engineering time will be allocated.

Again we value your feedback and hope this doesn't discourage you from providing future suggested feature requests.

@github-actions github-actions bot removed stat:awaiting-triage Status - Awaiting triage from the Netcode team. stat:reply-needed Awaiting reply from Unity account labels May 15, 2025
@marcusx2
Copy link
Author

If you are on the server yeah you could just use SendTo.Server. The idea was calling it from the client.

@NoelStephensUnity
Copy link
Collaborator

NoelStephensUnity commented May 15, 2025

If you are on the server yeah you could just use SendTo.Server. The idea was calling it from the client.

That is what this script does:
(I adjusted the InvokeServerSideRpc to be private to convey the general idea better)

    public class SomeBehaviour : NetworkBehaviour
    {
        public void InvokeServerAndMe()
        {
            if (!IsServer)
            {
                InvokeServerSideRpc();
            }
            ///////////////////////////////
            // Add the the script you want to be invoked locally and on the server

            ///////////////////////////////
        }

        [Rpc(SendTo.Server)]
        private void InvokeServerSideRpc()
        {
            InvokeServerAndMe();
        }
    }

When SomeBehaviour.InvokeServerAndMe is invoked on a client, it will send the server an RPC and it invokes the script after that.
When SomeBehaviour.InvokeServerAndMe is invoked on a server, it will just invoke the script and not invoke the RPC.

Effectively that is a SendTo.ServerAndMe targeted script.

(You can add whatever parameters you might need to both methods)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature, request or improvement
Projects
None yet
Development

No branches or pull requests

2 participants