Skip to content

Commit 1817f62

Browse files
authored
fix: Moving tick writing out of NetworkVariable and into NetworkBehav… (#590)
* fix: Moving tick writing out of NetworkVariable and into NetworkBehaviour. Allows proper handling of INetworkVariable that are not plain NetworkVariable. Forces extension of the interface. issues/573, MTT-532 * test[NetworkList]: Adding manual test for NetworkList * test[NetworkList]: Adding manual test for NetworkDictionary and NetworkSet
1 parent 36607e7 commit 1817f62

File tree

7 files changed

+125
-26
lines changed

7 files changed

+125
-26
lines changed

com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,10 @@ private void NetworkVariableUpdate(ulong clientId)
621621
{
622622
writtenAny = true;
623623

624+
// write the network tick at which this NetworkVariable was modified remotely
625+
// this will allow lag-compensation
626+
writer.WriteUInt16Packed(NetworkVariableFields[k].RemoteTick);
627+
624628
if (NetworkManager.Singleton.NetworkConfig.EnsureNetworkVariableLengthSafety)
625629
{
626630
using (var varBuffer = PooledNetworkBuffer.Get())

com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkDictionary.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,15 @@ private void HandleAddDictionaryEvent(NetworkDictionaryEvent<TKey, TValue> dicti
531531
m_DirtyEvents.Add(dictionaryEvent);
532532
}
533533
}
534+
535+
public ushort RemoteTick
536+
{
537+
get
538+
{
539+
// todo: implement proper network tick for NetworkDictionary
540+
return NetworkTickSystem.NoTick;
541+
}
542+
}
534543
}
535544

536545
/// <summary>
@@ -586,4 +595,4 @@ public enum EventType
586595
/// </summary>
587596
public TValue Value;
588597
}
589-
}
598+
}

com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkList.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,15 @@ private void HandleAddListEvent(NetworkListEvent<T> listEvent)
531531
m_DirtyEvents.Add(listEvent);
532532
}
533533
}
534+
535+
public ushort RemoteTick
536+
{
537+
get
538+
{
539+
// todo: implement proper network tick for NetworkList
540+
return NetworkTickSystem.NoTick;
541+
}
542+
}
534543
}
535544

536545
/// <summary>
@@ -590,4 +599,4 @@ public enum EventType
590599
/// </summary>
591600
public int Index;
592601
}
593-
}
602+
}

com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkSet.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ public bool Remove(T item)
496496

497497
/// <inheritdoc />
498498
public bool IsReadOnly => m_Set.IsReadOnly;
499+
500+
public ushort RemoteTick
501+
{
502+
get
503+
{
504+
// todo: implement proper network tick for NetworkSet
505+
return NetworkTickSystem.NoTick;
506+
}
507+
}
499508
}
500509

501510
/// <summary>
@@ -536,4 +545,4 @@ public enum EventType
536545
public T Value;
537546
}
538547
}
539-
#endif
548+
#endif

com.unity.multiplayer.mlapi/Runtime/NetworkVariable/INetworkVariable.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,10 @@ public interface INetworkVariable
7373
/// </summary>
7474
/// <param name="behaviour">The behaviour the container behaves to</param>
7575
void SetNetworkBehaviour(NetworkBehaviour behaviour);
76+
77+
/// <summary>
78+
/// Accessor for the RemoteTick stored in the networkVariable, list, set or dictionary
79+
/// </summary>
80+
ushort RemoteTick { get; }
7681
}
77-
}
82+
}

com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,6 @@ public bool CanClientRead(ulong clientId)
145145
/// <param name="stream">The stream to write the value to</param>
146146
public void WriteDelta(Stream stream)
147147
{
148-
using (var writer = PooledNetworkWriter.Get(stream))
149-
{
150-
// write the network tick at which this NetworkVariable was modified remotely
151-
// this will allow lag-compensation
152-
// todo: this is currently only done on delta updates. Consider whether it should be done in WriteField
153-
writer.WriteUInt16Packed(RemoteTick);
154-
}
155-
156148
WriteField(stream);
157149
}
158150

testproject/Assets/Scripts/Testing/ManualNetworkVariableTest.cs

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using UnityEngine;
34
using MLAPI.NetworkVariable;
5+
using MLAPI.NetworkVariable.Collections;
46

57
namespace MLAPI
68
{
@@ -10,7 +12,20 @@ namespace MLAPI
1012
[AddComponentMenu("MLAPI/ManualNetworkVariableTest")]
1113
public class ManualNetworkVariableTest : NetworkBehaviour
1214
{
13-
private NetworkVariable<int> m_TestVar;
15+
// testing NetworkList
16+
private NetworkList<string> m_TestList = new NetworkList<string>();
17+
private bool m_GotNetworkList = false;
18+
19+
// testing NetworkSet
20+
private NetworkSet<string> m_TestSet = new NetworkSet<string>();
21+
private bool m_GotNetworkSet = false;
22+
23+
// testing NetworkDictionary
24+
private NetworkDictionary<int, string> m_TestDictionary = new NetworkDictionary<int, string>();
25+
private bool m_GotNetworkDictionary = false;
26+
27+
// testing NetworkVariable, especially ticks
28+
private NetworkVariable<int> m_TestVar = new NetworkVariable<int>();
1429
private int m_MinDelta = 0;
1530
private int m_MaxDelta = 0;
1631
private int m_LastRemoteTick = 0;
@@ -22,25 +37,65 @@ public class ManualNetworkVariableTest : NetworkBehaviour
2237

2338
void Start()
2439
{
25-
m_TestVar.OnValueChanged = ValueChanged;
40+
m_TestVar.OnValueChanged += ValueChanged;
2641
m_TestVar.Settings.WritePermission = NetworkVariablePermission.Everyone;
2742

43+
m_TestList.OnListChanged += ListChanged;
44+
m_TestList.Settings.WritePermission = NetworkVariablePermission.OwnerOnly;
45+
46+
m_TestSet.OnSetChanged += SetChanged;
47+
m_TestSet.Settings.WritePermission = NetworkVariablePermission.OwnerOnly;
48+
49+
m_TestDictionary.OnDictionaryChanged += DictionaryChanged;
50+
m_TestDictionary.Settings.WritePermission = NetworkVariablePermission.OwnerOnly;
51+
2852
if (IsOwner)
2953
{
3054
m_TestVar.Value = 0;
55+
Debug.Log("We'll be sending " + MyMessage());
3156
}
3257
}
3358

34-
void Awake()
35-
{
36-
Debug.Log("Awake");
37-
}
38-
3959
private void FixedUpdate()
4060
{
4161
if (IsOwner)
4262
{
4363
m_TestVar.Value = m_TestVar.Value + 1;
64+
m_TestList.Add(MyMessage());
65+
((ICollection<string>)m_TestSet).Add(MyMessage());
66+
m_TestDictionary[0] = MyMessage();
67+
}
68+
}
69+
70+
private string MyMessage()
71+
{
72+
return "Message from " + NetworkObjectId;
73+
}
74+
75+
private void ListChanged(NetworkListEvent<string> listEvent)
76+
{
77+
if (!IsOwner && !m_GotNetworkList)
78+
{
79+
Debug.Log("Received: " + listEvent.Value);
80+
m_GotNetworkList = true;
81+
}
82+
}
83+
84+
private void SetChanged(NetworkSetEvent<string> setEvent)
85+
{
86+
if (!IsOwner && !m_GotNetworkSet)
87+
{
88+
Debug.Log("Received: " + setEvent.Value);
89+
m_GotNetworkSet = true;
90+
}
91+
}
92+
93+
private void DictionaryChanged(NetworkDictionaryEvent<int, string> dictionaryEvent)
94+
{
95+
if (!IsOwner && !m_GotNetworkSet)
96+
{
97+
Debug.Log("Received: " + dictionaryEvent.Key + ":" + dictionaryEvent.Value);
98+
m_GotNetworkDictionary = true;
4499
}
45100
}
46101

@@ -84,19 +139,35 @@ private void ValueChanged(int before, int after)
84139
{
85140
// Let's be reasonable and allow a 5 tick difference
86141
// that could be due to timing difference, lag, queueing
87-
if (m_Problems == "" && Math.Abs(m_MaxDelta - m_MinDelta) < 5)
142+
143+
if (!m_GotNetworkList)
144+
{
145+
m_Problems += "Didn't receive any NetworkList updates from other machines";
146+
}
147+
148+
if (!m_GotNetworkSet)
149+
{
150+
m_Problems += "Didn't receive any NetworkSet updates from other machines";
151+
}
152+
153+
if (!m_GotNetworkDictionary)
154+
{
155+
m_Problems += "Didn't receive any NetworkDictionary updates from other machines";
156+
}
157+
158+
if (Math.Abs(m_MaxDelta - m_MinDelta) > 5)
159+
{
160+
m_Problems += "Delta range: " + m_MinDelta + " + " + m_MaxDelta + "\n";
161+
}
162+
163+
if (m_Problems == "")
88164
{
89165
Debug.Log("**** TEST PASSED ****");
90166
}
91167
else
92168
{
93169
Debug.Log("**** TEST FAILED ****");
94-
Debug.Log($"Delta range: {m_MinDelta}, {m_MaxDelta}");
95-
96-
if (m_Problems != "")
97-
{
98-
Debug.Log(m_Problems);
99-
}
170+
Debug.Log(m_Problems);
100171
}
101172
enabled = false;
102173
}

0 commit comments

Comments
 (0)