Skip to content

Client to Server RPC #29

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

ltwlf
Copy link

@ltwlf ltwlf commented Jan 28, 2025

Hey guys,

Thank you for the excellent work on this library! I’m using it for a Unity XR project. XR-real-time requirements differ from those of typical games, so I needed to make a few changes.

Here’s what I’ve done in step 1:

  • Implemented a client-to-server RPC system. If ExecuteFlags.ExecuteOnServer is set but code is running on the client, the client will send the RPC to the server.
  • Added support for RemoteCallNetSerializable (with compression on the to-do list).
using LiteEntitySystem;
using LiteNetLib.Utils;
using UnityEngine;

namespace Entities
{

    public class GreetingData : INetSerializable
    {
        public string Greeting { get; set; }

        public void Serialize(NetDataWriter writer)
        {
            writer.Put(Greeting);
        }

        public void Deserialize(NetDataReader reader)
        {
            Greeting = reader.GetString();
        }
    }

    public class TestEntity : EntityLogic
    {
        private static RemoteCallNetSerializable<GreetingData> _sendGreeting = new();

        public TestEntity (EntityParams entityParams) : base(entityParams) { }

        protected override void RegisterRPC(ref RPCRegistrator r)
        {
            base.RegisterRPC(ref r);
            r.CreateRPCAction(this, OnReceiveGreeting, ref _sendGreeting, ExecuteFlags.ExecuteOnServer);
        }

        private void OnReceiveGreeting(GreetingData data)
        {
            Debug.Log($"Received Greeting = {data.Greeting}");
        }

        public void SendGreetingToServer()
        {
            if (IsServer) return;

            ExecuteRPC(_sendGreeting, new GreetingData
            {
                Greeting = "Hello from the client!"
            });
        }
    }
}

I’m not entirely sure if I can keep everything in sync long-term, or if you’re open to contributions.
I’d love to hear any feedback you might have!

Best, Christian

@RevenantX
Copy link
Owner

RevenantX commented Jan 29, 2025

Hi. There is already similar mechanism inside HumanControllerLoigc - SendRequest
And SubscribeToClientRequestStruct/SubscribeToClientRequest
I will not call this like RPC because it also uses similar technique using ReliableOrdered channel which can be a bit unsynchronized with input sending that uses unreliable.

@ltwlf
Copy link
Author

ltwlf commented Jan 29, 2025

Yes, I saw it. It is tied to the HumanController. I considered to replace or unify it, but for compatibility reason I started with a separate implementation first.
In my other PR I made some SyncVar changes.
My plan is to bring both together and allow to set sync vars when the client is an owner. I also have request and release ownership calls in my EntityLogic (no PR).
You are generally interested in this stuff or should I keep it in my fork? It is hard to keep it synced.

@ltwlf
Copy link
Author

ltwlf commented Jan 29, 2025

What do you suggest?

@ltwlf ltwlf force-pushed the client2server-rpc branch from 5f6794b to 51c50bb Compare January 29, 2025 13:50
@RevenantX
Copy link
Owner

@ltwlf i planned to add more complete Client->Server RPCs. But for stability of game logic and correct prediction/and confirmation they should be included into input packets. And they possibly will be a bit unreliable (when input on server skipped because of long network delay)

@ltwlf
Copy link
Author

ltwlf commented Jan 30, 2025

I understand. I actually would need both reliable and unreliable data channels. In XR, I have to send a large volume of transform data from the client and synchronize it with other clients, and it’s acceptable if some of that data is lost. For the XR Rig/Controllers I user HumanController inputs. And previously, when a user moved an object, I sent that information via unreliable packets, but the routing to all the relevant entities became too cumbersome.

So, you go ahead and implement this yourself, and I'll cancel my PR?

@RevenantX
Copy link
Owner

RevenantX commented Jan 30, 2025

I need some time to think, currently i'm a bit busy because of daily work. I will look closer on weekends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants