Hello! When connecting to the server, I try to send an RPC command to the server, but it does not work and the console displays “Cannot send RPC with no remote connection.” Why doesn’t it work?
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct ClientRequestGameEntrySystem : ISystem
{
private EntityQuery _pendingNetworkIdQuery;
public void OnCreate (ref SystemState state)
{
EntityQueryBuilder builder = new EntityQueryBuilder(Allocator.Temp).WithAll<ClientCreatePlayerRequest>().WithNone<NetworkStreamInGame>();
_pendingNetworkIdQuery = state.GetEntityQuery(builder);
state.RequireForUpdate(_pendingNetworkIdQuery);
}
public void OnUpdate(ref SystemState state)
{
ClientCreatePlayerRequest createRequest = SystemAPI.GetSingleton<ClientCreatePlayerRequest>();
EntityCommandBuffer commandBuffer = new EntityCommandBuffer(Allocator.Temp);
NativeArray<Entity> pendingNetworkingId = _pendingNetworkIdQuery.ToEntityArray(Allocator.Temp);
foreach(var networkID in pendingNetworkingId)
{
commandBuffer.AddComponent<NetworkStreamInGame>(networkID);
Entity requestEntity = commandBuffer.CreateEntity();
commandBuffer.AddComponent(requestEntity, new ServerCreatePlayerRequest { PlayerModelID = createRequest.PlayerModelID }) ;
commandBuffer.AddComponent(requestEntity, new SendRpcCommandRequest { TargetConnection = networkID });
}
commandBuffer.Playback(state.EntityManager);
commandBuffer.Dispose();
}
}
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct ServerRequestGameEntrySystem : ISystem
{
private EntityQuery _pendingNetworkIdQuery;
public void OnCreate(ref SystemState state)
{
EntityQueryBuilder builder = new EntityQueryBuilder(Allocator.Temp).WithAll<ServerCreatePlayerRequest, ReceiveRpcCommandRequest>();
_pendingNetworkIdQuery = state.GetEntityQuery(builder);
state.RequireForUpdate(_pendingNetworkIdQuery);
}
public void OnUpdate(ref SystemState state)
{
EntityCommandBuffer commandBuffer = new EntityCommandBuffer(Allocator.Temp);
foreach (var (createPlayerRequest, requestRPC, requestEntity) in SystemAPI.Query<ServerCreatePlayerRequest, ReceiveRpcCommandRequest>().WithEntityAccess())
{
commandBuffer.DestroyEntity(requestEntity);
commandBuffer.AddComponent<NetworkStreamInGame>(requestRPC.SourceConnection);
int clientId = SystemAPI.GetComponent<NetworkId>(requestRPC.SourceConnection).Value;
int modelId = createPlayerRequest.PlayerModelID;
Debug.Log("Player with id {clientId} created | Model id {modelId}");
}
commandBuffer.Playback(state.EntityManager);
commandBuffer.Dispose();
}
}