Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Unity.Entities;
- using Unity.NetCode;
- using Unity.Networking.Transport;
- using Unity.Burst;
- using UnityEngine;
- [UpdateInGroup(typeof(ServerSimulationSystemGroup))]
- public class IntRpcReceiver : SystemBase
- {
- protected override void OnUpdate()
- {
- var system = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
- var buffer = system.CreateCommandBuffer();
- // On the server, go through ever entity with a 'IntRpc' that we
- // are _not_ sending
- Entities.WithNone<SendRpcCommandRequestComponent>().ForEach((Entity entity, in IntRpc request, in ReceiveRpcCommandRequestComponent requestSource) => {
- Debug.Log(request.message);
- // Once we process the message, delete it
- buffer.DestroyEntity(entity);
- }).WithoutBurst().Run();
- system.AddJobHandleForProducer(this.Dependency);
- }
- }
- [UpdateInGroup(typeof(ClientSimulationSystemGroup))]
- public class IntRpcSender : RpcCommandRequestSystem<IntRpc>
- {
- // This class doesn't need a body. The parent class takes
- // care of transmitting 'IntRpc's.
- }
- [BurstCompile]
- public struct IntRpc : IRpcCommand
- {
- public int message;
- // All this serialization stuff seems like boilerplate, but
- // I don't know how to get rid of it yet.
- public void Deserialize(ref DataStreamReader reader)
- {
- message = reader.ReadInt();
- }
- public void Serialize(ref DataStreamWriter writer)
- {
- writer.WriteInt(message);
- }
- [BurstCompile]
- private static void InvokeExecute(ref RpcExecutor.Parameters parameters)
- {
- RpcExecutor.ExecuteCreateRequestComponent<IntRpc>(ref parameters);
- }
- static PortableFunctionPointer<RpcExecutor.ExecuteDelegate>
- InvokeExecuteFunctionPointer =
- new PortableFunctionPointer<RpcExecutor.ExecuteDelegate>(InvokeExecute);
- public PortableFunctionPointer<RpcExecutor.ExecuteDelegate> CompileExecute()
- {
- return InvokeExecuteFunctionPointer;
- }
- }
- /* Sending the RPC:
- var request = EntityManager.CreateEntity();
- EntityManager.AddComponent<IntRpc>(request);
- EntityManager.AddComponent<SendRpcCommandRequestComponent>(request);
- EntityManager.SetComponentData(request, new SendRpcCommandRequestComponent {
- TargetConnection = // The entity to send the RPC to. Must have a 'NetworkIdComponent' component
- });
- EntityManager.SetComponentData(request, new IntRpc {
- message = 123, // The actual payload of the RPC
- });
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement