Skip to content

Commit a6b852f

Browse files
update
Delay in-game state for new connections to provide NGO time to start synchronization.
1 parent 3d4a803 commit a6b852f

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public struct NetcodeConnection
1313
internal Entity Entity;
1414
public int NetworkId;
1515

16+
internal float ConnectedTime;
17+
1618
public bool IsServer => World.IsServer();
1719
public void GoInGame()
1820
{
@@ -29,8 +31,12 @@ public void SendMessage<T>(T message) where T : unmanaged, IRpcCommand
2931
internal partial class UnifiedUpdateConnections : SystemBase
3032
{
3133
private List<NetcodeConnection> m_TempConnections = new List<NetcodeConnection>();
34+
35+
private Dictionary<int, NetcodeConnection> m_NewConnections = new Dictionary<int, NetcodeConnection>();
36+
3237
protected override void OnUpdate()
3338
{
39+
var isServer = World.IsServer();
3440
var commandBuffer = new EntityCommandBuffer(Allocator.Temp);
3541
foreach (var (networkId, connectionState, entity) in SystemAPI.Query<NetworkId, ConnectionState>().WithNone<NetworkStreamConnection>().WithEntityAccess())
3642
{
@@ -44,18 +50,40 @@ protected override void OnUpdate()
4450

4551
m_TempConnections.Clear();
4652

53+
4754
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<NetworkStreamConnection>().WithNone<NetworkStreamInGame>().WithEntityAccess())
4855
{
49-
commandBuffer.AddComponent<NetworkStreamInGame>(entity);
50-
commandBuffer.AddComponent(entity, default(ConnectionState));
51-
m_TempConnections.Add(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });
56+
// TODO-Unified: For new connections, we have a delay before the N4E in-game state for the client to provide time for the NGO side of the client to synchronize.
57+
// Note: Once both are using the same transport we should be able to get the transport id and determine the NGO assigned client-id and at that point once the
58+
// client has signaled that it has synchronized (or has been sent the synchronization data) we finalize the in-game connection state (or something along those lines).
59+
if (!m_NewConnections.ContainsKey(networkId.Value))
60+
{
61+
var newConnection = new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value, ConnectedTime = UnityEngine.Time.realtimeSinceStartup + 1.0f };
62+
m_NewConnections.Add(networkId.Value, newConnection);
63+
}
5264
}
5365

54-
foreach (var con in m_TempConnections)
66+
// If we have any pending connections
67+
if (m_NewConnections.Count > 0)
5568
{
56-
NetworkManager.OnNetCodeConnect?.Invoke(con);
69+
foreach (var entry in m_NewConnections)
70+
{
71+
// Check if the delay time has passed.
72+
if (entry.Value.ConnectedTime < UnityEngine.Time.realtimeSinceStartup)
73+
{
74+
// Set the connection in-game
75+
commandBuffer.AddComponent<NetworkStreamInGame>(entry.Value.Entity);
76+
commandBuffer.AddComponent(entry.Value.Entity, default(ConnectionState));
77+
NetworkManager.OnNetCodeConnect?.Invoke(entry.Value);
78+
m_TempConnections.Add(entry.Value);
79+
}
80+
}
81+
// Remove any connections that have "gone in-game".
82+
foreach (var connection in m_TempConnections)
83+
{
84+
m_NewConnections.Remove(connection.NetworkId);
85+
}
5786
}
58-
5987
m_TempConnections.Clear();
6088

6189
commandBuffer.Playback(EntityManager);

0 commit comments

Comments
 (0)