Skip to content

Commit a0e87b3

Browse files
committed
test[NetworkList]: Adding manual test for NetworkList
1 parent 1cf158e commit a0e87b3

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

testproject/Assets/Scripts/Testing/ManualNetworkVariableTest.cs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using UnityEngine;
33
using MLAPI.NetworkVariable;
4+
using MLAPI.NetworkVariable.Collections;
45

56
namespace MLAPI
67
{
@@ -10,7 +11,12 @@ namespace MLAPI
1011
[AddComponentMenu("MLAPI/ManualNetworkVariableTest")]
1112
public class ManualNetworkVariableTest : NetworkBehaviour
1213
{
13-
private NetworkVariable<int> m_TestVar;
14+
// testing NetworkList
15+
private NetworkList<string> m_TestList = new NetworkList<string>();
16+
private bool m_GotNetworkList = false;
17+
18+
// testing NetworkVariable, especially ticks
19+
private NetworkVariable<int> m_TestVar = new NetworkVariable<int>();
1420
private int m_MinDelta = 0;
1521
private int m_MaxDelta = 0;
1622
private int m_LastRemoteTick = 0;
@@ -22,25 +28,39 @@ public class ManualNetworkVariableTest : NetworkBehaviour
2228

2329
void Start()
2430
{
25-
m_TestVar.OnValueChanged = ValueChanged;
31+
m_TestVar.OnValueChanged += ValueChanged;
2632
m_TestVar.Settings.WritePermission = NetworkVariablePermission.Everyone;
2733

34+
m_TestList.OnListChanged += ListChanged;
35+
m_TestList.Settings.WritePermission = NetworkVariablePermission.OwnerOnly;
36+
2837
if (IsOwner)
2938
{
3039
m_TestVar.Value = 0;
40+
Debug.Log("We'll be sending " + MyMessage());
3141
}
3242
}
3343

34-
void Awake()
35-
{
36-
Debug.Log("Awake");
37-
}
38-
3944
private void FixedUpdate()
4045
{
4146
if (IsOwner)
4247
{
4348
m_TestVar.Value = m_TestVar.Value + 1;
49+
m_TestList.Add(MyMessage());
50+
}
51+
}
52+
53+
private string MyMessage()
54+
{
55+
return "Message from " + NetworkObjectId;
56+
}
57+
58+
private void ListChanged(NetworkListEvent<string> listEvent)
59+
{
60+
if (!IsOwner && !m_GotNetworkList)
61+
{
62+
Debug.Log("Received: " + listEvent.Value);
63+
m_GotNetworkList = true;
4464
}
4565
}
4666

@@ -84,19 +104,25 @@ private void ValueChanged(int before, int after)
84104
{
85105
// Let's be reasonable and allow a 5 tick difference
86106
// that could be due to timing difference, lag, queueing
87-
if (m_Problems == "" && Math.Abs(m_MaxDelta - m_MinDelta) < 5)
107+
108+
if (!m_GotNetworkList)
109+
{
110+
m_Problems += "Didn't receive any NetworkList updates from other machines";
111+
}
112+
113+
if (Math.Abs(m_MaxDelta - m_MinDelta) > 5)
114+
{
115+
m_Problems += "Delta range: " + m_MinDelta + " + " + m_MaxDelta + "\n";
116+
}
117+
118+
if (m_Problems == "")
88119
{
89120
Debug.Log("**** TEST PASSED ****");
90121
}
91122
else
92123
{
93124
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-
}
125+
Debug.Log(m_Problems);
100126
}
101127
enabled = false;
102128
}

0 commit comments

Comments
 (0)