LiteEntitySystem icon indicating copy to clipboard operation
LiteEntitySystem copied to clipboard

Client to Server RPC

Open ltwlf opened this issue 10 months ago • 6 comments

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

ltwlf avatar Jan 28 '25 20:01 ltwlf

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.

RevenantX avatar Jan 29 '25 11:01 RevenantX

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 avatar Jan 29 '25 11:01 ltwlf

What do you suggest?

ltwlf avatar Jan 29 '25 11:01 ltwlf

@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)

RevenantX avatar Jan 30 '25 11:01 RevenantX

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?

ltwlf avatar Jan 30 '25 12:01 ltwlf

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

RevenantX avatar Jan 30 '25 12:01 RevenantX