I am trying to modify the NetCube sample into pong game but I am having some problems. It works fine with only 1 client, but increasing num clients to 2 gives me errors:
InvalidOperationException: Ghost receive system only supports a single connection.
InvalidOperationException: GetSingleton<Unity.NetCode.CommandTargetComponent>() requires that exactly one Unity.NetCode.CommandTargetComponent exists but there are 2.
The parts dealing with CommandTargetComponent have not changed much. In the GoInGameRPC i just changed the name of the input component.
[UpdateInGroup(typeof(ServerSimulationSystemGroup))]
public class ServerGoInGameRequestSystem : ComponentSystem {
protected override void OnCreate() {
RequireSingletonForUpdate<EnablePongGhostSendSystemComponent>();
}
protected override void OnUpdate() {
Entities.WithNone<SendRpcCommandRequestComponent>().ForEach((Entity reqEnt, ref GoInGameRequest req, ref ReceiveRpcCommandRequestComponent reqSrc) => {
PostUpdateCommands.AddComponent<NetworkStreamInGame>(reqSrc.SourceConnection);
UnityEngine.Debug.Log(String.Format("Server setting connection {0} to in game", EntityManager.GetComponentData<NetworkIdComponent>(reqSrc.SourceConnection).Value));
var ghostCollection = GetSingleton<GhostPrefabCollectionComponent>();
var ghostId = PongGhostSerializerCollection.FindGhostType<PlayerPaddleSnapshotData>();
var prefab = EntityManager.GetBuffer<GhostPrefabBuffer>(ghostCollection.serverPrefabs)[ghostId].Value;
var player = EntityManager.Instantiate(prefab);
EntityManager.SetComponentData(player, new ClientMovableComponent { PlayerId = EntityManager.GetComponentData<NetworkIdComponent>(reqSrc.SourceConnection).Value });
PostUpdateCommands.AddBuffer<ClientPlayerInput>(player);
PostUpdateCommands.SetComponent(reqSrc.SourceConnection, new CommandTargetComponent { targetEntity = player });
PostUpdateCommands.DestroyEntity(reqEnt);
});
}
}
In the system that deals with input i removed the horizontal component. I cannot really figure out whats wrong here.
[UpdateInGroup(typeof(ClientSimulationSystemGroup))]
public class ClientPaddleInputSystem : ComponentSystem {
protected override void OnCreate() {
RequireSingletonForUpdate<NetworkIdComponent>();
RequireSingletonForUpdate<EnablePongGhostReceiveSystemComponent>();
}
protected override void OnUpdate() {
/* Check if it already exists otherwise create it */
var localInput = GetSingleton<CommandTargetComponent>().targetEntity;
if (localInput == Entity.Null) {
var localPlayerId = GetSingleton<NetworkIdComponent>().Value;
Entities.WithNone<ClientPlayerInput>().ForEach((Entity ent, ref ClientMovableComponent paddle) => {
if (paddle.PlayerId == localPlayerId) {
PostUpdateCommands.AddBuffer<ClientPlayerInput>(ent);
PostUpdateCommands.SetComponent(GetSingletonEntity<CommandTargetComponent>(), new CommandTargetComponent { targetEntity = ent });
}
});
return;
}
var input = default(ClientPlayerInput);
input.tick = World.GetExistingSystem<ClientSimulationSystemGroup>().ServerTick;
if (Input.GetKey("s")) {
input.vertical -= 1;
}
if (Input.GetKey("w")) {
input.vertical += 1;
}
var inputBuffer = EntityManager.GetBuffer<ClientPlayerInput>(localInput);
inputBuffer.AddCommandData(input);
}
}
Any pointers would be appreciated.