我正在尝试使用 RPC 来通知“LiteEntitySytem.PawnLogic”类中的服务器,设置已在客户端中完成。
我尝试从客户端调用名为 NotifyServerThatSetupHasCompleted() 的方法。
我希望服务器已收到通知。但服务器没有收到通知。这是我使用的代码
//Server
using LiteEntitySystem;
public class BaseNetworkNPC : PawnLogic
{
private bool hasSetupCompleted = false;
private static RemoteCall setupCompletedRPC;
public BaseNetworkNPC(EntityParams entityParams) : base(entityParams)
{
}
protected override void RegisterRPC(ref RPCRegistrator r)
{
base.RegisterRPC(ref r);
r.CreateRPCAction(this, SetupCompleted, ref setupCompletedRPC, ExecuteFlags.ExecuteOnServer);
Debug.WriteLine("Registered setupCompletedRPC.");
}
protected override void Update()
{
base.Update();
if (!hasSetupCompleted) return;
FSMTick();
}
private void FSMTick()
{
Debug.WriteLine("FSMTicking");
stateTimer += EntityManager.DeltaTimeF;
}
protected override void OnDestroy()
{
base.OnDestroy();
}
public void SetupCompleted()
{
Debug.WriteLine("Setup completed RPC triggered on the server.");
hasSetupCompleted = true;
}
public void NotifyServerThatSetupHasCompleted()
{
if (EntityManager.IsClient)
{
Debug.WriteLine("Client is notifying server that setup has completed.");
ExecuteRPC(in setupCompletedRPC);
}
}
}
//Client
internal class Client
{
public void OnPeerConnected(NetPeer peer)
{
Server = peer;
// Initialize ChatSystem with the connected server peer
//_chatController.Initialize(Server);
//I dont think this is needed because upon joining the API returns everything and the server knows where to place them
//NetworkHelpers.SendPacket(Server, _writer, _packetProcessor, PacketType.Serialized, new ServerRequestPacket { RequestType = RequestType.StarSystem }, DeliveryMethod.ReliableOrdered);
NetworkHelpers.SendPacket(
Server,
_writer,
_packetProcessor,
PacketType.Serialized,
new JoinPacket
{
username = LoginVariables.Email,
password = LoginVariables.Password,
token = LoginVariables.Token
},
DeliveryMethod.ReliableOrdered
);
EntityTypesMap<GameEntities> typesMap = new EntityTypesMap<GameEntities>()
.Register(GameEntities.NPC, e => new BaseNetworkNPC(e))
.Register(GameEntities.NPCController, e => new NPCController(e));
_entityManager = ClientEntityManager.Create<PlayerInputPacket>(
typesMap,
new LiteNetLibNetPeer(peer, true),
(byte)PacketType.EntitySystem,
60
);
_entityManager
.GetEntities<BaseNetworkNPC>()
.SubscribeToConstructed(CreateNPC, true);
}
private void CreateNPC(BaseNetworkNPC baseNetworkNPC)
{
baseNetworkNPC.NotifyServerThatSetupHasCompleted();
// after running baseNetworkNPC.NotifyServerThatSetupHasCompleted(); server state never changes and BaseNetworkNPC.SetupCompleted() is never called
}
}
RPC 目前仅适用于从服务器到客户端。 HumanController 中有 ClientRequests - 可以将信息从客户端发送到服务器。