Skip to content

Commit 083bb63

Browse files
Jesse OlmerNoelStephensUnity
andauthored
chore: Better NetworkBehaviour index out of range message (#2295)
Changes the error message to be more easily actionable for users Co-authored-by: Noel Stephens <[email protected]>
1 parent 8770f9d commit 083bb63

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,20 @@ internal NetworkBehaviour GetNetworkBehaviourAtOrderIndex(ushort index)
10511051
{
10521052
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
10531053
{
1054-
NetworkLog.LogError($"Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
1054+
NetworkLog.LogError($"{nameof(NetworkBehaviour)} index {index} was out of bounds for {name}. NetworkBehaviours must be the same, and in the same order, between server and client.");
1055+
}
1056+
1057+
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
1058+
{
1059+
var currentKnownChildren = new System.Text.StringBuilder();
1060+
currentKnownChildren.Append($"Known child {nameof(NetworkBehaviour)}s:");
1061+
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
1062+
{
1063+
var childNetworkBehaviour = ChildNetworkBehaviours[i];
1064+
currentKnownChildren.Append($" [{i}] {childNetworkBehaviour.__getTypeName()}");
1065+
currentKnownChildren.Append(i < ChildNetworkBehaviours.Count - 1 ? "," : ".");
1066+
}
1067+
NetworkLog.LogInfo(currentKnownChildren.ToString());
10551068
}
10561069

10571070
return null;

com.unity.netcode.gameobjects/Tests/Editor/NetworkObjectTests.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.RegularExpressions;
12
using NUnit.Framework;
23
using UnityEngine;
34
using UnityEngine.TestTools;
@@ -31,19 +32,17 @@ public void NetworkManagerOverrideTest()
3132
}
3233

3334
[Test]
34-
public void GetBehaviourIndexNone()
35+
[TestCase(0)]
36+
[TestCase(1)]
37+
[TestCase(2)]
38+
public void GetBehaviourIndexNone(int index)
3539
{
3640
var gameObject = new GameObject(nameof(GetBehaviourIndexNone));
3741
var networkObject = gameObject.AddComponent<NetworkObject>();
3842

39-
// TODO: Maybe not hardcode message?
40-
LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
41-
LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
42-
LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
43+
LogAssert.Expect(LogType.Error, new Regex(".*out of bounds.*"));
4344

44-
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(0), Is.Null);
45-
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(1), Is.Null);
46-
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(2), Is.Null);
45+
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex((ushort)index), Is.Null);
4746

4847
// Cleanup
4948
Object.DestroyImmediate(gameObject);
@@ -56,13 +55,10 @@ public void GetBehaviourIndexOne()
5655
var networkObject = gameObject.AddComponent<NetworkObject>();
5756
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
5857

59-
// TODO: Maybe not hardcode message?
60-
LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
61-
LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
58+
LogAssert.Expect(LogType.Error, new Regex(".*out of bounds.*"));
6259

6360
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(0), Is.EqualTo(networkBehaviour));
6461
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(1), Is.Null);
65-
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(2), Is.Null);
6662

6763
// Cleanup
6864
Object.DestroyImmediate(gameObject);

0 commit comments

Comments
 (0)